From 7e681bd491c6acaee5e6ab04f5119bce6a32fef8 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 31 Jul 2013 11:00:23 -0400 Subject: [PATCH] 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 | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/video/windows/SDL_windowsopengl.c b/src/video/windows/SDL_windowsopengl.c index 583220d83..f33f5c358 100644 --- a/src/video/windows/SDL_windowsopengl.c +++ b/src/video/windows/SDL_windowsopengl.c @@ -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 @@ WIN_GL_ChoosePixelFormatARB(_THIS, int *iAttribs, float *fAttribs) &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 @@ WIN_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) 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()"); }