Workaround some Windows OpenGL drivers mishandling wglMakeCurrent().
authorRyan C. Gordon <icculus@icculus.org>
Wed, 31 Jul 2013 11:00:23 -0400
changeset 7553f0091f8eae33
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
     1.1 --- a/src/video/windows/SDL_windowsopengl.c	Wed Jul 31 10:04:59 2013 -0300
     1.2 +++ b/src/video/windows/SDL_windowsopengl.c	Wed Jul 31 11:00:23 2013 -0400
     1.3 @@ -22,6 +22,7 @@
     1.4  
     1.5  #if SDL_VIDEO_DRIVER_WINDOWS
     1.6  
     1.7 +#include "SDL_assert.h"
     1.8  #include "SDL_windowsvideo.h"
     1.9  
    1.10  /* WGL implementation of SDL OpenGL support */
    1.11 @@ -407,7 +408,7 @@
    1.12                                                      &matching);
    1.13          }
    1.14  
    1.15 -        _this->gl_data->wglMakeCurrent(NULL, NULL);
    1.16 +        _this->gl_data->wglMakeCurrent(hdc, NULL);
    1.17          _this->gl_data->wglDeleteContext(hglrc);
    1.18      }
    1.19      ReleaseDC(hwnd, hdc);
    1.20 @@ -622,11 +623,22 @@
    1.21          return SDL_SetError("OpenGL not initialized");
    1.22      }
    1.23  
    1.24 -    if (window) {
    1.25 -        hdc = ((SDL_WindowData *) window->driverdata)->hdc;
    1.26 -    } else {
    1.27 -        hdc = NULL;
    1.28 +    /* sanity check that higher level handled this. */
    1.29 +    SDL_assert(window || (!window && !context));
    1.30 +
    1.31 +    /* Some Windows drivers freak out if hdc is NULL, even when context is
    1.32 +       NULL, against spec. Since hdc is _supposed_ to be ignored if context
    1.33 +       is NULL, we either use the current GL window, or do nothing if we
    1.34 +       already have no current context. */
    1.35 +    if (!window) {
    1.36 +        window = SDL_GL_GetCurrentWindow();
    1.37 +        if (!window) {
    1.38 +            SDL_assert(SDL_GL_GetCurrentContext() == NULL);
    1.39 +            return 0;  /* already done. */
    1.40 +        }
    1.41      }
    1.42 +
    1.43 +    hdc = ((SDL_WindowData *) window->driverdata)->hdc;
    1.44      if (!_this->gl_data->wglMakeCurrent(hdc, (HGLRC) context)) {
    1.45          return WIN_SetError("wglMakeCurrent()");
    1.46      }