OpenTTD Source 20261112-master-gbcbec5e63f
cocoa_v.mm
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
17
18#if defined(WITH_COCOA) || defined(DOXYGEN_API)
19
20#include "../../stdafx.h"
22
24#include "../../openttd.h"
25#include "../../debug.h"
26#include "../../error_func.h"
29#include "cocoa_v.h"
30#include "cocoa_wnd.h"
33#include "../../gfx_func.h"
34#include "../../thread.h"
36#include "../../progress.h"
37#include "../../settings_type.h"
38#include "../../window_func.h"
39#include "../../window_gui.h"
40
41#import <sys/param.h> /* for MAXPATHLEN */
42#import <sys/time.h> /* gettimeofday */
43
45static Palette _local_palette;
46extern bool _tab_is_down;
47
48
51 { 640, 480 },
52 { 800, 600 },
53 { 1024, 768 },
54 { 1152, 864 },
55 { 1280, 800 },
56 { 1280, 960 },
57 { 1280, 1024 },
58 { 1400, 1050 },
59 { 1600, 1200 },
60 { 1680, 1050 },
61 { 1920, 1200 },
62 { 2560, 1440 }
63};
64
65
73{
74 this->setup = false;
75 this->buffer_locked = false;
76
77 this->refresh_sys_sprites = true;
78
79 this->window = nil;
80 this->cocoaview = nil;
81 this->delegate = nil;
82
83 this->colour_space = nullptr;
84
85 this->dirty_rect = {};
86}
87
90{
91 if (!_cocoa_video_started) return;
92
94
95 /* Release window mode resources */
96 if (this->window != nil) [ this->window close ];
97 [ this->cocoaview release ];
98 [ this->delegate release ];
99
100 CGColorSpaceRelease(this->colour_space);
101
102 _cocoa_video_started = false;
103}
104
109std::optional<std::string_view> VideoDriver_Cocoa::Initialize()
110{
111 if (_cocoa_video_started) return "Already started";
113
114 /* Don't create a window or enter fullscreen if we're just going to show a dialog. */
115 if (!CocoaSetupApplication()) return std::nullopt;
116
117 this->UpdateAutoResolution();
119
120 return std::nullopt;
121}
122
130void VideoDriver_Cocoa::MakeDirty(int left, int top, int width, int height)
131{
132 Rect r = {left, top, left + width, top + height};
133 this->dirty_rect = BoundingRect(this->dirty_rect, r);
134}
135
140{
141 /* Restart game loop if it was already running (e.g. after bootstrapping),
142 * otherwise this call is a no-op. */
143 [ [ NSNotificationCenter defaultCenter ] postNotificationName:OTTDMainLaunchGameEngine object:nil ];
144
145 /* Start the main event loop. */
146 [ NSApp run ];
147}
148
156{
157 NSSize screen_size = [ [ NSScreen mainScreen ] frame ].size;
158 w = std::min(w, (int)screen_size.width);
159 h = std::min(h, (int)screen_size.height);
160
161 NSRect contentRect = NSMakeRect(0, 0, w, h);
162 [ this->window setContentSize:contentRect.size ];
163
164 /* Ensure frame height - title bar height >= view height */
165 float content_height = [ this->window contentRectForFrameRect:[ this->window frame ] ].size.height;
166 contentRect.size.height = Clamp(h, 0, (int)content_height);
167
168 if (this->cocoaview != nil) {
169 h = (int)contentRect.size.height;
170 [ this->cocoaview setFrameSize:contentRect.size ];
171 }
172
173 [ (OTTD_CocoaWindow *)this->window center ];
174 this->AllocateBackingStore();
175
176 return true;
177}
178
185{
186 if (this->IsFullscreen() == full_screen) return true;
187
188 if ([ this->window respondsToSelector:@selector(toggleFullScreen:) ]) {
189 [ this->window performSelector:@selector(toggleFullScreen:) withObject:this->window ];
190
191 /* Hide the menu bar and the dock */
192 [ NSMenu setMenuBarVisible:!full_screen ];
193
194 this->UpdateVideoModes();
195 InvalidateWindowClassesData(WindowClass::GameOptions, 3);
196 return true;
197 }
198
199 return false;
200}
201
206
208{
209 if (this->refresh_sys_sprites && this->window != nil) {
210 [ this->window refreshSystemSprites ];
211 this->refresh_sys_sprites = false;
212 }
213}
214
220{
221 this->AllocateBackingStore(true);
222 return true;
223}
224
229{
230 [ [ this->cocoaview inputContext ] performSelectorOnMainThread:@selector(discardMarkedText) withObject:nil waitUntilDone:[ NSThread isMainThread ] ];
231 /* Clear any marked string from the current edit box. */
232 HandleTextInput({}, true);
233}
234
240{
241 std::vector<int> rates{};
242
243 std::array<CGDirectDisplayID, 16> displays;
244
245 uint32_t count = 0;
246 CGGetActiveDisplayList(displays.size(), displays.data(), &count);
247
248 for (uint32_t i = 0; i < count; i++) {
249 CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displays[i]);
250 int rate = static_cast<int>(CGDisplayModeGetRefreshRate(mode));
251 if (rate > 0) rates.push_back(rate);
252 CGDisplayModeRelease(mode);
253 }
254
255 return rates;
256}
257
263{
264 NSRect frame = [ [ NSScreen mainScreen ] frame ];
265 return { static_cast<uint>(NSWidth(frame)), static_cast<uint>(NSHeight(frame)) };
266}
267
273{
274 if (this->buffer_locked) return false;
275 this->buffer_locked = true;
276
277 _screen.dst_ptr = this->GetVideoPointer();
278 assert(_screen.dst_ptr != nullptr);
279
280 return true;
281}
282
285{
286 if (_screen.dst_ptr != nullptr) {
287 /* Hand video buffer back to the drawing backend. */
288 this->ReleaseVideoPointer();
289 _screen.dst_ptr = nullptr;
290 }
291
292 this->buffer_locked = false;
293}
294
300{
301 return this->window != nil && ([ this->window styleMask ] & NSWindowStyleMaskFullScreen) != 0;
302}
303
308{
309 /* Store old window size if we entered fullscreen mode. */
310 bool fullscreen = this->IsFullscreen();
311 if (fullscreen && !_fullscreen) this->orig_res = _cur_resolution;
312 _fullscreen = fullscreen;
313
315
317
318 /* We need to store the window size as non-Retina size in
319 * the config file to get same windows size on next start. */
320 _cur_resolution.width = [ this->cocoaview frame ].size.width;
321 _cur_resolution.height = [ this->cocoaview frame ].size.height;
322}
323
328{
329 _resolutions.clear();
330
331 if (this->IsFullscreen()) {
332 /* Full screen, there is only one possible resolution. */
333 NSSize screen = [ [ this->window screen ] frame ].size;
334 _resolutions.emplace_back((uint)screen.width, (uint)screen.height);
335 } else {
336 /* Windowed; offer a selection of common window sizes up until the
337 * maximum usable screen space. This excludes the menu and dock areas. */
338 NSSize maxSize = [ [ NSScreen mainScreen] visibleFrame ].size;
339 for (const auto &d : _default_resolutions) {
340 if (d.width < maxSize.width && d.height < maxSize.height) _resolutions.push_back(d);
341 }
342 _resolutions.emplace_back((uint)maxSize.width, (uint)maxSize.height);
343 }
344}
345
352bool VideoDriver_Cocoa::MakeWindow(int width, int height)
353{
354 this->setup = true;
355
356 /* Limit window size to screen frame. */
357 NSSize screen_size = [ [ NSScreen mainScreen ] frame ].size;
358 if (width > screen_size.width) width = screen_size.width;
359 if (height > screen_size.height) height = screen_size.height;
360
361 NSRect contentRect = NSMakeRect(0, 0, width, height);
362
363 /* Create main window. */
364 unsigned int style = NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable;
365 this->window = [ [ OTTD_CocoaWindow alloc ] initWithContentRect:contentRect styleMask:style backing:NSBackingStoreBuffered defer:NO driver:this ];
366 if (this->window == nil) {
367 Debug(Facility::Driver, Severity::Critical, "Could not create the Cocoa window.");
368 this->setup = false;
369 return false;
370 }
371
372 /* Add built in full-screen support when available (OS X 10.7 and higher)
373 * This code actually compiles for 10.5 and later, but only makes sense in conjunction
374 * with the quartz fullscreen support as found only in 10.7 and later. */
375 if ([ this->window respondsToSelector:@selector(toggleFullScreen:) ]) {
376 NSWindowCollectionBehavior behavior = [ this->window collectionBehavior ];
377 behavior |= NSWindowCollectionBehaviorFullScreenPrimary;
378 [ this->window setCollectionBehavior:behavior ];
379
380 NSButton *fullscreenButton = [ this->window standardWindowButton:NSWindowZoomButton ];
381 [ fullscreenButton setAction:@selector(toggleFullScreen:) ];
382 [ fullscreenButton setTarget:this->window ];
383 }
384
385 this->delegate = [ [ OTTD_CocoaWindowDelegate alloc ] initWithDriver:this ];
386 [ this->window setDelegate:this->delegate ];
387
388 [ this->window center ];
389 [ this->window makeKeyAndOrderFront:nil ];
390
391 /* Create wrapper view for input and event handling. */
392 NSRect view_frame = [ this->window contentRectForFrameRect:[ this->window frame ] ];
393 this->cocoaview = [ [ OTTD_CocoaView alloc ] initWithFrame:view_frame ];
394 if (this->cocoaview == nil) {
395 Debug(Facility::Driver, Severity::Critical, "Could not create the event wrapper view.");
396 this->setup = false;
397 return false;
398 }
399 [ this->cocoaview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable ];
400
401 /* Create content view. */
402 NSView *draw_view = this->AllocateDrawView();
403 if (draw_view == nil) {
404 Debug(Facility::Driver, Severity::Critical, "Could not create the drawing view.");
405 this->setup = false;
406 return false;
407 }
408 [ draw_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable ];
409
410 /* Create view chain: window -> input wrapper view -> content view. */
411 [ this->window setContentView:this->cocoaview ];
412 [ this->cocoaview addSubview:draw_view ];
413 [ this->window makeFirstResponder:this->cocoaview ];
414 [ draw_view release ];
415
416 [ this->window setColorSpace:[ NSColorSpace sRGBColorSpace ] ];
417 CGColorSpaceRelease(this->colour_space);
418 this->colour_space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
419 if (this->colour_space == nullptr) this->colour_space = CGColorSpaceCreateDeviceRGB();
420 if (this->colour_space == nullptr) FatalError("Could not get a valid colour space for drawing.");
421
422 this->setup = false;
423
424 return true;
425}
426
427
433{
434 NSEventMask mask = NSEventMaskAny;
435 NSEvent *event = [ NSApp nextEventMatchingMask:mask untilDate:[ NSDate distantPast ] inMode:NSDefaultRunLoopMode dequeue:YES ];
436
437 if (event == nil) return false;
438
439 [ NSApp sendEvent:event ];
440
441 return true;
442}
443
445{
446 NSUInteger cur_mods = [ NSEvent modifierFlags ];
447
448 bool old_ctrl_pressed = IsCtrlPressed();
449
450 _input_pressed.Set(ControlInput::Ctrl, (cur_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSEventModifierFlagControl : NSEventModifierFlagCommand)) != 0);
451 _input_pressed.Set(ControlInput::Shift, (cur_mods & NSEventModifierFlagShift) != 0);
452
454
455 if (old_ctrl_pressed != IsCtrlPressed()) HandleCtrlChanged();
456}
457
460{
461 this->StartGameThread();
462
463 for (;;) {
464 @autoreleasepool {
465 if (_exit_game) {
466 /* Restore saved resolution if in fullscreen mode. */
467 if (this->IsFullscreen()) _cur_resolution = this->orig_res;
468 break;
469 }
470
471 this->Tick();
472 this->SleepTillNextTick();
473 }
474 }
475
476 this->StopGameThread();
477}
478
479
481@interface OTTD_QuartzView : NSView {
483}
484- (instancetype)initWithFrame:(NSRect)frameRect andDriver:(VideoDriver_CocoaQuartz *)drv;
485@end
486
487@implementation OTTD_QuartzView
488
495- (instancetype)initWithFrame:(NSRect)frameRect andDriver:(VideoDriver_CocoaQuartz *)drv
496{
497 if (self = [ super initWithFrame:frameRect ]) {
498 self->driver = drv;
499
500 /* We manage our content updates ourselves. */
501 self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
502 self.wantsLayer = YES;
503
504 self.layer.magnificationFilter = kCAFilterNearest;
505 self.layer.opaque = YES;
506 }
507 return self;
508}
509
515{
516 return NO;
517}
518
523- (BOOL)isOpaque
524{
525 return YES;
526}
527
533{
534 return YES;
535}
536
539{
540 if (driver->cgcontext == nullptr) return;
541
542 /* Set layer contents to our backing buffer, which avoids needless copying. */
543 CGImageRef fullImage = CGBitmapContextCreateImage(driver->cgcontext);
544 self.layer.contents = (__bridge id)fullImage;
545 CGImageRelease(fullImage);
546}
547
550{
551 [ super viewDidChangeBackingProperties ];
552
553 self.layer.contentsScale = [ driver->cocoaview getContentsScale ];
554}
555
556@end
557
560
567static void ClearWindowBuffer(uint32_t *buffer, uint32_t pitch, uint32_t height)
568{
569 uint32_t fill = Colour(0, 0, 0).data;
570 for (uint32_t y = 0; y < height; y++) {
571 for (uint32_t x = 0; x < pitch; x++) {
572 buffer[y * pitch + x] = fill;
573 }
574 }
575}
576
577VideoDriver_CocoaQuartz::VideoDriver_CocoaQuartz()
578{
579 this->window_width = 0;
580 this->window_height = 0;
581 this->window_pitch = 0;
582 this->buffer_depth = 0;
583 this->window_buffer = nullptr;
584 this->pixel_buffer = nullptr;
585
586 this->cgcontext = nullptr;
587}
588
589std::optional<std::string_view> VideoDriver_CocoaQuartz::Start(const StringList &param)
590{
591 auto err = this->Initialize();
592 if (err) return err;
593
595 if (bpp != 8 && bpp != 32) {
596 Stop();
597 return "The cocoa quartz subdriver only supports 8 and 32 bpp.";
598 }
599
600 bool fullscreen = _fullscreen;
601 if (!this->MakeWindow(_cur_resolution.width, _cur_resolution.height)) {
602 Stop();
603 return "Could not create window";
604 }
605
606 this->AllocateBackingStore(true);
607
608 if (fullscreen) this->ToggleFullscreen(fullscreen);
609
610 this->GameSizeChanged();
611 this->UpdateVideoModes();
612
613 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
614
615 return std::nullopt;
616
617}
618
620{
622
623 CGContextRelease(this->cgcontext);
624
625 this->window_buffer.reset();
626 this->pixel_buffer.reset();
627}
628
630{
631 return [ [ OTTD_QuartzView alloc ] initWithFrame:[ this->cocoaview bounds ] andDriver:this ];
632}
633
634void VideoDriver_CocoaQuartz::AllocateBackingStore([[maybe_unused]] bool force)
635{
636 if (this->window == nil || this->cocoaview == nil || this->setup) return;
637
638 this->UpdatePalette(0, 256);
639
640 NSRect newframe = [ this->cocoaview getRealRect:[ this->cocoaview frame ] ];
641
642 this->window_width = (int)newframe.size.width;
643 this->window_height = (int)newframe.size.height;
644 this->window_pitch = Align(this->window_width, 16 / sizeof(uint32_t)); // Quartz likes lines that are multiple of 16-byte.
646
647 /* Create Core Graphics Context */
648 this->window_buffer = std::make_unique<uint32_t[]>(this->window_pitch * this->window_height);
649 /* Initialize with opaque black. */
650 ClearWindowBuffer(this->window_buffer.get(), this->window_pitch, this->window_height);
651
652 CGContextRelease(this->cgcontext);
653 this->cgcontext = CGBitmapContextCreate(
654 this->window_buffer.get(), // data
655 this->window_width, // width
656 this->window_height, // height
657 8, // bits per component
658 this->window_pitch * 4, // bytes per row
659 this->colour_space, // colour space
660 kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host
661 );
662
663 assert(this->cgcontext != nullptr);
664 CGContextSetShouldAntialias(this->cgcontext, FALSE);
665 CGContextSetAllowsAntialiasing(this->cgcontext, FALSE);
666 CGContextSetInterpolationQuality(this->cgcontext, kCGInterpolationNone);
667
668 if (this->buffer_depth == 8) {
669 this->pixel_buffer = std::make_unique<uint8_t[]>(this->window_width * this->window_height);
670 } else {
671 this->pixel_buffer.reset();
672 }
673
674 /* Tell the game that the resolution has changed */
675 _screen.width = this->window_width;
676 _screen.height = this->window_height;
677 _screen.pitch = this->buffer_depth == 8 ? this->window_width : this->window_pitch;
678 _screen.dst_ptr = this->GetVideoPointer();
679
680 /* Redraw screen */
681 this->MakeDirty(0, 0, _screen.width, _screen.height);
682 this->GameSizeChanged();
683}
684
693void VideoDriver_CocoaQuartz::BlitIndexedToView32(int left, int top, int right, int bottom)
694{
695 const uint32_t *pal = this->palette;
696 const uint8_t *src = this->pixel_buffer.get();
697 uint32_t *dst = this->window_buffer.get();
698 uint width = this->window_width;
699 uint pitch = this->window_pitch;
700
701 for (int y = top; y < bottom; y++) {
702 for (int x = left; x < right; x++) {
703 dst[y * pitch + x] = pal[src[y * width + x]];
704 }
705 }
706}
707
713void VideoDriver_CocoaQuartz::UpdatePalette(uint first_colour, uint num_colours)
714{
715 if (this->buffer_depth != 8) return;
716
717 for (uint i = first_colour; i < first_colour + num_colours; i++) {
718 uint32_t clr = 0xff000000;
719 clr |= (uint32_t)_local_palette.palette[i].r << 16;
720 clr |= (uint32_t)_local_palette.palette[i].g << 8;
721 clr |= (uint32_t)_local_palette.palette[i].b;
722 this->palette[i] = clr;
723 }
724
725 this->MakeDirty(0, 0, _screen.width, _screen.height);
726}
727
729{
730 if (!CopyPalette(_local_palette)) return;
731
733
734 switch (blitter->UsePaletteAnimation()) {
736 this->UpdatePalette(_local_palette.first_dirty, _local_palette.count_dirty);
737 break;
738
741 break;
742
744 break;
745
746 default:
747 NOT_REACHED();
748 }
749}
750
753{
755
756 /* Check if we need to do anything */
757 if (IsEmptyRect(this->dirty_rect) || [ this->window isMiniaturized ]) return;
758
759 /* We only need to blit in indexed mode since in 32bpp mode the game draws directly to the image. */
760 if (this->buffer_depth == 8) {
762 this->dirty_rect.left,
763 this->dirty_rect.top,
764 this->dirty_rect.right,
765 this->dirty_rect.bottom
766 );
767 }
768
769 NSRect dirtyrect;
770 dirtyrect.origin.x = this->dirty_rect.left;
771 dirtyrect.origin.y = this->window_height - this->dirty_rect.bottom;
772 dirtyrect.size.width = this->dirty_rect.right - this->dirty_rect.left;
773 dirtyrect.size.height = this->dirty_rect.bottom - this->dirty_rect.top;
774
775 /* Notify OS X that we have new content to show. */
776 [ this->cocoaview setNeedsDisplayInRect:[ this->cocoaview getVirtualRect:dirtyrect ] ];
777
778 /* Tell the OS to get our contents to screen as soon as possible. */
779 [ CATransaction flush ];
780
781 this->dirty_rect = {};
782}
783
784#endif /* WITH_COCOA or DOXYGEN_API */
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:139
How all blitters should look like.
Definition base.hpp:28
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
virtual void PaletteAnimate(const Palette &palette)=0
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
@ None
No palette animation.
Definition base.hpp:50
@ Blitter
The blitter takes care of the palette animation.
Definition base.hpp:52
@ VideoBackend
Palette animation should be done by video backend (8bpp only!).
Definition base.hpp:51
virtual void PostResize()
Post resize event.
Definition base.hpp:210
RAII class for measuring simple elements of performance.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition cocoa_v.mm:589
CGContextRef cgcontext
Context reference for Quartz subdriver.
Definition cocoa_v.h:118
uint32_t palette[256]
Colour Palette.
Definition cocoa_v.h:112
int buffer_depth
Colour depth of used frame buffer.
Definition cocoa_v.h:104
int window_width
Current window width in pixel.
Definition cocoa_v.h:108
void CheckPaletteAnim() override
Process any pending palette animation.
Definition cocoa_v.mm:728
std::unique_ptr< uint32_t[]> window_buffer
Colour translation from palette to screen.
Definition cocoa_v.h:106
NSView * AllocateDrawView() override
Allocate the view to show the game on.
Definition cocoa_v.mm:629
void AllocateBackingStore(bool force=false) override
Resize the window.
Definition cocoa_v.mm:634
void BlitIndexedToView32(int left, int top, int right, int bottom)
This function copies 8bpp pixels from the screen buffer in 32bpp windowed mode.
Definition cocoa_v.mm:693
std::unique_ptr< uint8_t[]> pixel_buffer
used for direct pixel access
Definition cocoa_v.h:105
void UpdatePalette(uint first_colour, uint num_colours)
Update the palette.
Definition cocoa_v.mm:713
void Stop() override
Stop Cocoa video driver.
Definition cocoa_v.mm:619
void Paint() override
Draw window.
Definition cocoa_v.mm:752
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition cocoa_v.h:135
int window_height
Current window height in pixel.
Definition cocoa_v.h:109
void GameSizeChanged()
Handle a change of the display area.
Definition cocoa_v.mm:307
bool ToggleFullscreen(bool fullscreen) override
Toggle between windowed and full screen mode for cocoa display driver.
Definition cocoa_v.mm:184
void UpdateVideoModes()
Update the video mode.
Definition cocoa_v.mm:327
std::vector< int > GetListOfMonitorRefreshRates() override
Get refresh rates of all connected monitors.
Definition cocoa_v.mm:239
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition cocoa_v.mm:262
std::optional< std::string_view > Initialize()
Common driver initialization.
Definition cocoa_v.mm:109
bool refresh_sys_sprites
System sprites need refreshing.
Definition cocoa_v.h:26
void MainLoop() override
Start the main programme loop when using a cocoa video driver.
Definition cocoa_v.mm:139
OTTD_CocoaWindow * window
Pointer to window object.
Definition cocoa_v.h:31
OTTD_CocoaView * cocoaview
Pointer to view object.
Definition cocoa_v.h:32
virtual void ReleaseVideoPointer()
Hand video buffer back to the drawing backend.
Definition cocoa_v.h:96
VideoDriver_Cocoa(bool uses_hardware_acceleration=false, bool supports_animation_buffer=false)
Create the video driver.
Definition cocoa_v.mm:71
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
OTTD_CocoaWindowDelegate * delegate
Window delegate object.
Definition cocoa_v.h:35
bool IsFullscreen()
Are we in fullscreen mode?
Definition cocoa_v.mm:299
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition cocoa_v.mm:444
bool MakeWindow(int width, int height)
Build window and view with a given size.
Definition cocoa_v.mm:352
bool PollEvent() override
Poll and handle a single event from the OS.
Definition cocoa_v.mm:432
void MakeDirty(int left, int top, int width, int height) override
Set dirty a rectangle managed by a cocoa video subdriver.
Definition cocoa_v.mm:130
void PopulateSystemSprites() override
Populate all sprites in cache.
Definition cocoa_v.mm:207
virtual NSView * AllocateDrawView()=0
Allocate the view to show the game on.
bool LockVideoBuffer() override
Lock video buffer for drawing if it isn't already mapped.
Definition cocoa_v.mm:272
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition cocoa_v.mm:228
void ClearSystemSprites() override
Clear all cached sprites.
Definition cocoa_v.mm:202
Rect dirty_rect
Region of the screen that needs redrawing.
Definition cocoa_v.h:67
void UnlockVideoBuffer() override
Unlock video buffer.
Definition cocoa_v.mm:284
void Stop() override
Stop Cocoa video driver.
Definition cocoa_v.mm:89
bool buffer_locked
Video buffer was locked by the main thread.
Definition cocoa_v.h:68
virtual void AllocateBackingStore(bool force=false)=0
Resize the window.
Dimension orig_res
Saved window size for non-fullscreen mode.
Definition cocoa_v.h:25
bool setup
Window is currently being created.
Definition cocoa_v.h:29
bool ChangeResolution(int w, int h) override
Change the resolution when using a cocoa video driver.
Definition cocoa_v.mm:155
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition cocoa_v.mm:219
CGColorSpaceRef colour_space
Window colour space.
Definition cocoa_v.h:33
void MainLoopReal()
Main game loop.
Definition cocoa_v.mm:459
bool fast_forward_key_pressed
The fast-forward key is being pressed.
void Tick()
Give the video-driver a tick.
const bool supports_animation_buffer
Whether this driver supports hardware accelerated animation.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void StartGameThread()
Start the loop for game-tick.
void StopGameThread()
Stop the loop for the game-tick.
VideoDriver(bool uses_hardware_acceleration=false, bool supports_animation_buffer=false)
Create the video driver.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
const bool uses_hardware_acceleration
Whether hardware acceleration is used or not.
bool is_game_threaded
Whether the game is allowed to use threads; cleared in the driver implementation constructors if requ...
static Palette _local_palette
Current palette to use for drawing.
Definition cocoa_ogl.mm:42
The Cocoa video driver.
bool _cocoa_video_started
Is the Cocoa video driver running.
Definition cocoa_v.mm:44
static FVideoDriver_CocoaQuartz iFVideoDriver_CocoaQuartz
Register the cocoa video driver.
Definition cocoa_v.mm:559
static const Dimension _default_resolutions[]
List of common display/window sizes.
Definition cocoa_v.mm:50
static void ClearWindowBuffer(uint32_t *buffer, uint32_t pitch, uint32_t height)
Clear buffer to opaque black.
Definition cocoa_v.mm:567
bool _tab_is_down
Is tab button pressed.
Definition cocoa_wnd.mm:77
OS interface for the cocoa video driver.
bool CocoaSetupApplication()
Startup the application.
Definition cocoa_wnd.mm:333
void CocoaExitApplication()
Deregister app delegate.
Definition cocoa_wnd.mm:369
NSString * OTTDMainLaunchGameEngine
Name of notification observer used to restart the game loop if necessary.
Definition cocoa_wnd.mm:75
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Driver
Driver message facility.
Definition debug_type.h:29
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
bool GetDriverParamBool(const StringList &parm, std::string_view name)
Get a boolean parameter the list of parameters.
Definition driver.cpp:67
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:28
Dimension _cur_resolution
The current resolution.
Definition driver.cpp:29
Error reporting related functions.
Factory to 'query' all available blitters.
Types for recording game performance data.
@ Video
Speed of painting drawn video buffer.
Rect BoundingRect(const Rect &r1, const Rect &r2)
Compute the bounding rectangle around two rectangles.
Geometry functions.
bool IsEmptyRect(const Rect &r)
Check if a rectangle is empty.
ControlInputs _input_pressed
Control inputs that are currently pressed.
Definition gfx.cpp:34
Functions related to the gfx engine.
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition window.cpp:2771
bool IsCtrlPressed()
Test if the ctrl key is pressed.
Definition gfx_func.h:82
void HandleTextInput(std::string_view str, bool marked=false, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Handle text input.
Definition window.cpp:2807
@ Ctrl
Ctrl modifier is pressed.
Definition gfx_type.h:449
@ Shift
Shift modifier is pressed.
Definition gfx_type.h:450
Subclass of NSView to support mouse awareness and text input.
Definition cocoa_wnd.h:39
Delegate for our NSWindow to send ask for quit on close.
Definition cocoa_wnd.h:47
Subclass of NSWindow to cater our special needs.
Definition cocoa_wnd.h:28
Subclass of OTTD_CocoaView to fix Quartz rendering.
Definition cocoa_v.mm:481
BOOL isOpaque()
Specifies whether the view is opaque.
Definition cocoa_v.mm:523
void updateLayer()
Updates the layer based on driver data.
Definition cocoa_v.mm:538
BOOL wantsUpdateLayer()
Specifies whether the view wants updates for layer.
Definition cocoa_v.mm:532
BOOL acceptsFirstResponder()
Specifies whether the view accepts first responder.
Definition cocoa_v.mm:514
VideoDriver_CocoaQuartz * driver
The driver to fix rendering for.
Definition cocoa_v.mm:482
void viewDidChangeBackingProperties()
Updates members with new values after changes in driver.
Definition cocoa_v.mm:549
Functions related to MacOS support.
Includes of mac os specific headers which contain objective c.
Integer math functions.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition math_func.hpp:37
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
Some generic types.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:230
Functions related to modal progress.
Pseudo random number generator.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:61
Dimensions (a width and height) of a rectangle in 2D.
int first_dirty
The first dirty element.
Definition gfx_type.h:422
Specification of a rectangle with absolute coordinates of all edges.
Base of all threads.
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3395
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.