Skip to content

Commit 596445a

Browse files
committed
gfxlib2/darwin: mimic the initialization of the X11 driver more closely
1 parent c8ddf45 commit 596445a

File tree

1 file changed

+64
-38
lines changed

1 file changed

+64
-38
lines changed

src/gfxlib2/darwin/gfx_driver_opengl_cocoa.m

+64-38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#ifdef DISABLE_X11
2+
13
#include "../fb_gfx.h"
24
#include "../fb_gfx_gl.h"
35
#include "fb_gfx_cocoa.h"
@@ -20,25 +22,21 @@
2022

2123
/* GFXDRIVER */
2224
const GFXDRIVER fb_gfxDriverCocoaOpenGL = {
23-
"Cocoa OpenGL", /* char *name; */
24-
driver_init, /* int (*init)(char *title, int w, int h, int depth, int
25-
refresh_rate, int flags); */
26-
driver_exit, /* void (*exit)(void); */
27-
fb_hCocoaLock, /* void (*lock)(void); */
28-
fb_hCocoaUnlock, /* void (*unlock)(void); */
29-
fb_hGL_SetPalette, /* void (*set_palette)(int index, int r, int g, int b);
30-
*/
31-
fb_hCocoaWaitVSync, /* void (*wait_vsync)(void); */
32-
fb_hCocoaGetMouse, /* int (*get_mouse)(int *x, int *y, int *z, int *buttons,
33-
int *clip); */
34-
fb_hCocoaSetMouse, /* void (*set_mouse)(int x, int y, int cursor, int clip);
35-
*/
25+
"Cocoa OpenGL", /* char *name; */
26+
driver_init, /* int (*init)(char *title, int w, int h, int depth, int refresh_rate, int flags); */
27+
driver_exit, /* void (*exit)(void); */
28+
fb_hCocoaLock, /* void (*lock)(void); */
29+
fb_hCocoaUnlock, /* void (*unlock)(void); */
30+
fb_hGL_SetPalette, /* void (*set_palette)(int index, int r, int g, int b); */
31+
fb_hCocoaWaitVSync, /* void (*wait_vsync)(void); */
32+
fb_hCocoaGetMouse, /* int (*get_mouse)(int *x, int *y, int *z, int *buttons, int *clip); */
33+
fb_hCocoaSetMouse, /* void (*set_mouse)(int x, int y, int cursor, int clip); */
3634
fb_hCocoaSetWindowTitle, /* void (*set_window_title)(char *title); */
3735
fb_hCocoaSetWindowPos, /* int (*set_window_pos)(int x, int y); */
3836
fb_hCocoaFetchModes, /* int *(*fetch_modes)(void); */
3937
driver_flip, /* void (*flip)(void); */
40-
driver_poll_events, /* void (*poll_events)(void); */
41-
NULL /* void (*update)(void); */
38+
driver_poll_events, /* void (*poll_events)(void); */
39+
NULL, /* void (*update)(void); */
4240
};
4341

4442
static dispatch_semaphore_t vsyncSema = NULL;
@@ -251,40 +249,48 @@ static int driver_init(char *title, int w, int h, int depth, int refresh_rate,
251249
driver.frame = NSMakeRect(0, 0, w, h);
252250

253251
fb_hGL_NormalizeParameters(flags);
254-
NSOpenGLPixelFormatAttribute attrs[32] = {0};
255-
NSOpenGLPixelFormatAttribute* attr = attrs;
256-
*attr++ = NSOpenGLPFADoubleBuffer;
257-
*attr++ = NSOpenGLPFAAccelerated;
258-
*attr++ = NSOpenGLPFAOpenGLProfile;
259-
*attr++ = NSOpenGLProfileVersionLegacy;
260-
*attr++ = NSOpenGLPFAColorSize;
261-
*attr++ = __fb_gl_params.color_bits;
262-
*attr++ = NSOpenGLPFAAlphaSize;
263-
*attr++ = __fb_gl_params.color_alpha_bits;
264-
*attr++ = NSOpenGLPFADepthSize;
265-
*attr++ = depth;
266-
*attr++ = NSOpenGLPFAStencilSize;
267-
*attr++ = __fb_gl_params.stencil_bits;
268-
*attr++ = NSOpenGLPFAAccumSize;
269-
*attr++ = __fb_gl_params.accum_bits;
270-
if (flags & HAS_MULTISAMPLE) {
271-
*attr++ = NSOpenGLPFAMultisample;
272-
*attr++ = NSOpenGLPFASampleBuffers;
273-
*attr++ = 1;
274-
*attr++ = NSOpenGLPFASamples;
275-
*attr++ = __fb_gl_params.num_samples;
252+
NSOpenGLPixelFormatAttribute attribs[32] = {0};
253+
NSOpenGLPixelFormatAttribute* attrib = attribs;
254+
NSOpenGLPixelFormatAttribute* samples_attrib = NULL;
255+
256+
*attrib++ = NSOpenGLPFADoubleBuffer;
257+
*attrib++ = NSOpenGLPFAAccelerated;
258+
*attrib++ = NSOpenGLPFAOpenGLProfile;
259+
*attrib++ = NSOpenGLProfileVersionLegacy;
260+
*attrib++ = NSOpenGLPFAColorSize;
261+
*attrib++ = __fb_gl_params.color_bits;
262+
*attrib++ = NSOpenGLPFAAlphaSize;
263+
*attrib++ = __fb_gl_params.color_alpha_bits;
264+
*attrib++ = NSOpenGLPFADepthSize;
265+
*attrib++ = depth;
266+
if (__fb_gl_params.stencil_bits > 0) {
267+
*attrib++ = NSOpenGLPFAStencilSize;
268+
*attrib++ = __fb_gl_params.stencil_bits;
269+
}
270+
if (__fb_gl_params.accum_bits > 0) {
271+
*attrib++ = NSOpenGLPFAAccumSize;
272+
*attrib++ = __fb_gl_params.accum_bits;
273+
}
274+
if (__fb_gl_params.num_samples > 0) {
275+
*attrib++ = NSOpenGLPFAMultisample;
276+
*attrib++ = NSOpenGLPFASampleBuffers;
277+
*attrib++ = 1;
278+
*attrib++ = NSOpenGLPFASamples;
279+
*attrib++ = __fb_gl_params.num_samples;
280+
samples_attrib = attrib;
276281
}
277282

278283
if (!gl_lib) gl_lib = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current", RTLD_LAZY);
279284
if (!gl_lib) gl_lib = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
280285
if (!gl_lib) return -1;
281286

282-
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
287+
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
283288
driver.context = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil];
284289
if (!driver.context) return -1;
285290
driver.view = [[OpenGLView alloc] initWithFrame:driver.frame];
286291
if (!driver.view) return -1;
287292
[driver.view setOpenGLContext:driver.context];
293+
[driver.view setWantsBestResolutionOpenGLSurface:YES];
288294

289295
driver.window =
290296
[[OpenGLWindow alloc] initWithContentRect:driver.frame
@@ -312,12 +318,30 @@ static int driver_init(char *title, int w, int h, int depth, int refresh_rate,
312318

313319
[driver.context makeCurrentContext];
314320

321+
__fb_gl_params.mode_2d = __fb_gl_params.init_mode_2d;
322+
if (__fb_gl_params.init_scale>=1){
323+
__fb_gl_params.scale = __fb_gl_params.init_scale;
324+
}
325+
326+
if (__fb_gl_params.scale>1){
327+
free(__fb_gfx->dirty);
328+
__fb_gfx->dirty = (char *)calloc(1, __fb_gfx->h * __fb_gfx->scanline_size * __fb_gl_params.scale);
329+
}
330+
315331
if (fb_hGL_Init(gl_lib, NULL)) return -1;
316332

333+
if ((samples_attrib) && (*samples_attrib > 0))
334+
__fb_gl.Enable(GL_MULTISAMPLE_ARB);
335+
336+
if (__fb_gl_params.mode_2d != DRIVER_OGL_2D_NONE)
337+
fb_hGL_ScreenCreate();
338+
317339
CGDirectDisplayID mainDisplay = CGMainDisplayID();
318340
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(mainDisplay);
319341
__fb_gfx->refresh_rate = round(CGDisplayModeGetRefreshRate(mode));
320342
CFRelease(mode);
343+
344+
[driver.view createDisplayLink];
321345
}
322346
return 0;
323347
}
@@ -464,3 +488,5 @@ int fb_hCocoaScreenInfo(ssize_t *width, ssize_t *height, ssize_t *depth, ssize_t
464488
void* fb_hGL_GetProcAddress(const char *name) {
465489
return dlsym(gl_lib, name);
466490
}
491+
492+
#endif

0 commit comments

Comments
 (0)