18#if defined(WITH_COCOA) || defined(DOXYGEN_API)
24#define GL_SILENCE_DEPRECATION
39#import <OpenGL/OpenGL.h>
51 static void *dl =
nullptr;
54 dl = dlopen(
"/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
57 return reinterpret_cast<OGLProc
>(dlsym(dl, proc));
62 CGLContextObj _context;
65@property (
class)
bool allowSoftware;
66+ (CGLPixelFormatObj)defaultPixelFormat;
68- (instancetype)initWithContext:(CGLContextObj)context;
73static bool _allowSoftware;
76 return _allowSoftware;
78+ (void)setAllowSoftware:(
bool)newVal
80 _allowSoftware = newVal;
83- (instancetype)initWithContext:(CGLContextObj)context
85 if (self = [ super init ]) {
86 self->_context = context;
89 self.magnificationFilter = kCAFilterNearest;
94+ (CGLPixelFormatObj)defaultPixelFormat
96 CGLPixelFormatAttribute attribs[] = {
97 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
98 kCGLPFAColorSize, (CGLPixelFormatAttribute)24,
99 kCGLPFAAlphaSize, (CGLPixelFormatAttribute)0,
100 kCGLPFADepthSize, (CGLPixelFormatAttribute)0,
102 kCGLPFAAllowOfflineRenderers,
103 kCGLPFASupportsAutomaticGraphicsSwitching,
105 _allowSoftware ? (CGLPixelFormatAttribute)0 : kCGLPFAAccelerated,
106 (CGLPixelFormatAttribute)0
109 CGLPixelFormatObj pxfmt =
nullptr;
110 GLint numPixelFormats;
111 CGLChoosePixelFormat(attribs, &pxfmt, &numPixelFormats);
116- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask
121- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pf
124 CGLCreateContext(pf, self->_context, &ctx);
127 CGLSetCurrentContext(ctx);
133- (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(nullable const CVTimeStamp *)ts
135 CGLSetCurrentContext(ctx);
140 [
super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts ];
145- (instancetype)initWithFrame:(NSRect)frameRect context:(CGLContextObj)context;
150- (instancetype)initWithFrame:(NSRect)frameRect context:(CGLContextObj)context
152 if (self = [ super initWithFrame:frameRect ]) {
154 self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
157 CALayer *l = [ [
OTTD_CGLLayer alloc ] initWithContext:context ];
159 self.wantsLayer = YES;
165- (BOOL)acceptsFirstResponder
175- (void)viewDidChangeBackingProperties
177 self.layer.contentsScale =
_allow_hidpi_window && [
self.window respondsToSelector:@selector(backingScaleFactor) ] ? [
self.window backingScaleFactor ] : 1.0f;
192 if (bpp != 8 && bpp != 32) {
194 return "The cocoa OpenGL subdriver only supports 8 and 32 bpp.";
209 bool fullscreen = _fullscreen;
212 return "Could not create window";
233 CGLSetCurrentContext(this->gl_context);
235 CGLReleaseContext(this->gl_context);
249 CGLSetCurrentContext(this->gl_context);
253std::optional<std::string_view> VideoDriver_CocoaOpenGL::AllocateContext(
bool allow_software)
257 CGLPixelFormatObj pxfmt = [
OTTD_CGLLayer defaultPixelFormat ];
258 if (pxfmt ==
nullptr)
return "No suitable pixel format found";
260 CGLCreateContext(pxfmt,
nullptr, &this->gl_context);
261 CGLDestroyPixelFormat(pxfmt);
263 if (this->gl_context ==
nullptr)
return "Can't create a rendering context";
265 CGLSetCurrentContext(this->gl_context);
272 return [ [
OTTD_CGLLayerView alloc ] initWithFrame:this->cocoaview.bounds context:this->gl_context ];
281 CGLSetCurrentContext(this->gl_context);
282 NSRect frame = [
this->cocoaview getRealRect:[
this->cocoaview frame ] ];
293 CGLSetCurrentContext(this->gl_context);
302 CGLSetCurrentContext(this->gl_context);
307 _screen.dst_ptr =
nullptr;
319 CGLSetCurrentContext(this->gl_context);
327 [
this->cocoaview.subviews[0].layer setNeedsDisplay ];
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
How all blitters should look like.
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...
@ Blitter
The blitter takes care of the palette animation.
void Paint()
Render video buffer to the screen.
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
static std::optional< std::string_view > Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
void ClearCursorCache()
Queue a request for cursor cache clear.
static OpenGLBackend * Get()
Get singleton instance of this class.
void DrawMouseCursor()
Draw mouse cursor on screen.
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
static void Destroy()
Free resources and destroy singleton back-end class.
uint8_t * anim_buffer
Animation buffer from OpenGL back-end.
void * GetVideoPointer() override
Get a pointer to the video buffer.
std::string driver_info
Information string about selected driver.
void PopulateSystemSprites() override
Populate all sprites in cache.
void ReleaseVideoPointer() override
Hand video buffer back to the drawing backend.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
std::string_view GetName() const override
Get the name of this driver.
void Paint() override
Paint the window.
NSView * AllocateDrawView() override
Allocate the view to show the game on.
void ClearSystemSprites() override
Clear all cached sprites.
void Stop() override
Stop Cocoa video driver.
void AllocateBackingStore(bool force=false) override
Resize the window.
bool ToggleFullscreen(bool fullscreen) override
Toggle between windowed and full screen mode for cocoa display driver.
void UpdateVideoModes()
Update the video mode.
std::optional< std::string_view > Initialize()
Common driver initialization.
OTTD_CocoaWindow * window
Pointer to window object.
bool MakeWindow(int width, int height)
Build window and view with a given size.
void PopulateSystemSprites() override
Populate all sprites in cache.
void ClearSystemSprites() override
Clear all cached sprites.
Rect dirty_rect
Region of the screen that needs redrawing.
void Stop() override
Stop Cocoa video driver.
bool buffer_locked
Video buffer was locked by the main thread.
bool setup
Window is currently being created.
The Cocoa OpenGL video driver.
static FVideoDriver_CocoaOpenGL iFVideoDriver_CocoaOpenGL
Storage for instance of the FVideoDriver_CocoaOpenGL class.
static Palette _local_palette
Current palette to use for drawing.
static OGLProc GetOGLProcAddressCallback(const char *proc)
Platform-specific callback to get an OpenGL function pointer.
OS interface for the cocoa video driver.
bool _allow_hidpi_window
Storage for allow_hidpi setting. If true renders OTTD in native resolution.
Functions related to debugging.
bool GetDriverParamBool(const StringList &parm, std::string_view name)
Get a boolean parameter the list of parameters.
Dimension _cur_resolution
The current resolution.
Factory to 'query' all available blitters.
Types for recording game performance data.
@ PFE_VIDEO
Speed of painting drawn video buffer.
Functions related to the gfx engine.
void GameSizeChanged()
Size of the application screen changed.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Functions related to MacOS support.
Includes of mac os specific headers wich contain objective c.
OpenGL video driver support.
@ Stop
Go to the depot and stop there.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition of base types and functions in a cross-platform compatible way.
std::vector< std::string > StringList
Type for a list of strings.