From 7849e997f61f39d7504a88c5b96fe01b81d7a60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20P=2E=20Tjern=C3=B8?= Date: Mon, 22 Apr 2013 12:07:16 -0700 Subject: [PATCH] Mac no longer loses OpenGL context when window is hidden. This fixes an issue that would arise when you minimize / order out an OpenGL window on Mac, where the window would lose it's window device. Without a window device, you cannot render to the window. It does so by making two changes: - Windows are no longer "oneShot" (which caused their window device to get destroyed when they're minified or ordered out) - Windows are no longer "deferred" (which caused the OS to defer window device creation until the window is shown, which meant that we couldn't properly makeCurrent to it) Thanks to http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html --- src/video/cocoa/SDL_cocoaopengl.m | 19 +++++++------------ src/video/cocoa/SDL_cocoawindow.m | 11 +++++++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/video/cocoa/SDL_cocoaopengl.m b/src/video/cocoa/SDL_cocoaopengl.m index a1686219b..4fc478330 100644 --- a/src/video/cocoa/SDL_cocoaopengl.m +++ b/src/video/cocoa/SDL_cocoaopengl.m @@ -238,16 +238,14 @@ Cocoa_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata; NSOpenGLContext *nscontext = (NSOpenGLContext *)context; - if (window->flags & SDL_WINDOW_SHOWN) { #ifndef FULLSCREEN_TOGGLEABLE - if (window->flags & SDL_WINDOW_FULLSCREEN) { - [nscontext setFullScreen]; - } else + if (window->flags & SDL_WINDOW_FULLSCREEN) { + [nscontext setFullScreen]; + } else #endif - { - [nscontext setView:[windowdata->nswindow contentView]]; - [nscontext update]; - } + { + [nscontext setView:[windowdata->nswindow contentView]]; + [nscontext update]; } [nscontext makeCurrentContext]; } else { @@ -310,10 +308,7 @@ Cocoa_GL_SwapWindow(_THIS, SDL_Window * window) pool = [[NSAutoreleasePool alloc] init]; /* FIXME: Do we need to get the context for the window? */ - nscontext = [NSOpenGLContext currentContext]; - if (nscontext != nil) { - [nscontext flushBuffer]; - } + [[NSOpenGLContext currentContext] flushBuffer]; [pool release]; } diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m index 47dc1052e..0ebb3c94a 100644 --- a/src/video/cocoa/SDL_cocoawindow.m +++ b/src/video/cocoa/SDL_cocoawindow.m @@ -591,6 +591,11 @@ SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created SDL_SetKeyboardFocus(data->window); } + /* Prevents the window's "window device" from being destroyed when it is + * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html + */ + [nswindow setOneShot:NO]; + /* All done! */ [pool release]; window->driverdata = data; @@ -633,7 +638,7 @@ Cocoa_CreateWindow(_THIS, SDL_Window * window) rect.origin.y -= screenRect.origin.y; } } - nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES screen:screen]; + nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:NO screen:screen]; // Create a default view for this window rect = [nswindow contentRectForFrameRect:[nswindow frame]]; @@ -856,8 +861,10 @@ Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style) } [data->listener close]; - data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:YES screen:[nswindow screen]]; + data->nswindow = [[SDLWindow alloc] initWithContentRect:[[nswindow contentView] frame] styleMask:style backing:NSBackingStoreBuffered defer:NO screen:[nswindow screen]]; [data->nswindow setContentView:[nswindow contentView]]; + /* See comment in SetupWindowData. */ + [data->nswindow setOneShot:NO]; [data->listener listen:data]; [nswindow close];