From 0284c3e7eee92708184d7c5ac8507acbeb03824a Mon Sep 17 00:00:00 2001 From: "J?rgen P. Tjern?" 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 @@ 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 @@ 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 @@ - (void)rightMouseDown:(NSEvent *)theEvent 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 @@ - (void)rightMouseDown:(NSEvent *)theEvent 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 @@ - (void)rightMouseDown:(NSEvent *)theEvent } [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];