Workaround some Windows OpenGL drivers mishandling wglMakeCurrent().
authorRyan C. Gordon <icculus@icculus.org>
Wed, 31 Jul 2013 11:00:23 -0400
changeset 7553 f0091f8eae33
parent 7552 463f78515a07
child 7554 4b653ef6050d
Workaround some Windows OpenGL drivers mishandling wglMakeCurrent(). You should be able to do wglMakeCurrent(NULL, NULL), according to MSDN, since the first parameter is ignored if the second is NULL, but this causes problems on some drivers, so make sure we always have a valid HDC in the first parameter (and exit early if we already have no current context in the NULL, NULL case).
src/video/windows/SDL_windowsopengl.c
--- a/src/video/windows/SDL_windowsopengl.c	Wed Jul 31 10:04:59 2013 -0300
+++ b/src/video/windows/SDL_windowsopengl.c	Wed Jul 31 11:00:23 2013 -0400
@@ -22,6 +22,7 @@
 
 #if SDL_VIDEO_DRIVER_WINDOWS
 
+#include "SDL_assert.h"
 #include "SDL_windowsvideo.h"
 
 /* WGL implementation of SDL OpenGL support */
@@ -407,7 +408,7 @@
                                                     &matching);
         }
 
-        _this->gl_data->wglMakeCurrent(NULL, NULL);
+        _this->gl_data->wglMakeCurrent(hdc, NULL);
         _this->gl_data->wglDeleteContext(hglrc);
     }
     ReleaseDC(hwnd, hdc);
@@ -622,11 +623,22 @@
         return SDL_SetError("OpenGL not initialized");
     }
 
-    if (window) {
-        hdc = ((SDL_WindowData *) window->driverdata)->hdc;
-    } else {
-        hdc = NULL;
+    /* sanity check that higher level handled this. */
+    SDL_assert(window || (!window && !context));
+
+    /* Some Windows drivers freak out if hdc is NULL, even when context is
+       NULL, against spec. Since hdc is _supposed_ to be ignored if context
+       is NULL, we either use the current GL window, or do nothing if we
+       already have no current context. */
+    if (!window) {
+        window = SDL_GL_GetCurrentWindow();
+        if (!window) {
+            SDL_assert(SDL_GL_GetCurrentContext() == NULL);
+            return 0;  /* already done. */
+        }
     }
+
+    hdc = ((SDL_WindowData *) window->driverdata)->hdc;
     if (!_this->gl_data->wglMakeCurrent(hdc, (HGLRC) context)) {
         return WIN_SetError("wglMakeCurrent()");
     }