From 0ae4eb4d26e4fe857ace987bee310334859689e9 Mon Sep 17 00:00:00 2001 From: Doug Binks Date: Sun, 12 Jul 2020 12:21:18 +0100 Subject: [PATCH] Improved User Context Win32 implementation _glfwCreateContextWGL and _glfwPlatformCreateUserContext now use new function _glfwCreateContextForDCWGL. --- src/wgl_context.c | 112 +++++++++++++++++++++++++--------------------- src/wgl_context.h | 2 + 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/wgl_context.c b/src/wgl_context.c index 16bd167c..5a57aed7 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -538,46 +538,17 @@ void _glfwTerminateWGL(void) attribs[index++] = v; \ } -// Create the OpenGL or OpenGL ES context +// Create the OpenGL or OpenGL ES context for the given HDC // -GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, - const _GLFWctxconfig* ctxconfig, - const _GLFWfbconfig* fbconfig) +GLFWbool _glfwCreateContextForDCWGL(HDC dc, const _GLFWctxconfig* ctxconfig, HGLRC* context) { int attribs[40]; - int pixelFormat; - PIXELFORMATDESCRIPTOR pfd; HGLRC share = NULL; + *context = NULL; if (ctxconfig->share) share = ctxconfig->share->context.wgl.handle; - window->context.wgl.dc = GetDC(window->win32.handle); - if (!window->context.wgl.dc) - { - _glfwInputError(GLFW_PLATFORM_ERROR, - "WGL: Failed to retrieve DC for window"); - return GLFW_FALSE; - } - - pixelFormat = choosePixelFormat(window, ctxconfig, fbconfig); - if (!pixelFormat) - return GLFW_FALSE; - - if (!DescribePixelFormat(window->context.wgl.dc, - pixelFormat, sizeof(pfd), &pfd)) - { - _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, - "WGL: Failed to retrieve PFD for selected pixel format"); - return GLFW_FALSE; - } - - if (!SetPixelFormat(window->context.wgl.dc, pixelFormat, &pfd)) - { - _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, - "WGL: Failed to set selected pixel format"); - return GLFW_FALSE; - } if (ctxconfig->client == GLFW_OPENGL_API) { @@ -692,9 +663,9 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, setAttrib(0, 0); - window->context.wgl.handle = - wglCreateContextAttribsARB(window->context.wgl.dc, share, attribs); - if (!window->context.wgl.handle) + *context = + wglCreateContextAttribsARB(dc, share, attribs); + if (!(*context)) { const DWORD error = GetLastError(); @@ -744,8 +715,8 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, } else { - window->context.wgl.handle = wglCreateContext(window->context.wgl.dc); - if (!window->context.wgl.handle) + *context = wglCreateContext(dc); + if (!(*context) ) { _glfwInputErrorWin32(GLFW_VERSION_UNAVAILABLE, "WGL: Failed to create OpenGL context"); @@ -754,7 +725,7 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, if (share) { - if (!wglShareLists(share, window->context.wgl.handle)) + if (!wglShareLists(share, *context)) { _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, "WGL: Failed to enable sharing with specified OpenGL context"); @@ -763,6 +734,50 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, } } + return GLFW_TRUE; +} + +// Create the OpenGL or OpenGL ES context +// +GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, + const _GLFWctxconfig* ctxconfig, + const _GLFWfbconfig* fbconfig) +{ + int pixelFormat; + PIXELFORMATDESCRIPTOR pfd; + + window->context.wgl.dc = GetDC(window->win32.handle); + if (!window->context.wgl.dc) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "WGL: Failed to retrieve DC for window"); + return GLFW_FALSE; + } + + pixelFormat = choosePixelFormat(window, ctxconfig, fbconfig); + if (!pixelFormat) + return GLFW_FALSE; + + if (!DescribePixelFormat(window->context.wgl.dc, + pixelFormat, sizeof(pfd), &pfd)) + { + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, + "WGL: Failed to retrieve PFD for selected pixel format"); + return GLFW_FALSE; + } + + if (!SetPixelFormat(window->context.wgl.dc, pixelFormat, &pfd)) + { + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, + "WGL: Failed to set selected pixel format"); + return GLFW_FALSE; + } + + if(!_glfwCreateContextForDCWGL( window->context.wgl.dc, ctxconfig, &window->context.wgl.handle )) + { + return GLFW_FALSE; + } + window->context.makeCurrent = makeContextCurrentWGL; window->context.swapBuffers = swapBuffersWGL; window->context.swapInterval = swapIntervalWGL; @@ -783,25 +798,22 @@ GLFWbool _glfwCreateContextWGL(_GLFWwindow* window, _GLFWusercontext* _glfwPlatformCreateUserContext(_GLFWwindow* window) { _GLFWusercontext* context; + _GLFWctxconfig ctxconfig; + context = calloc(1, sizeof(_GLFWusercontext)); - - context->handle = wglCreateContext(window->context.wgl.dc); context->window = window; - if (!context->handle) - { - _glfwInputErrorWin32(GLFW_VERSION_UNAVAILABLE, - "WGL: Failed to create user OpenGL context"); - free(context); - return GLFW_FALSE; - } - if (!wglShareLists(window->context.wgl.handle,context->handle)) + ctxconfig = _glfw.hints.context; + ctxconfig.share = window; + + if (!_glfwCreateContextForDCWGL(window->context.wgl.dc, &ctxconfig, &context->handle)) { _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, - "WGL: Failed to enable sharing with window OpenGL context and user context"); + "WGL: Failed to create user OpenGL context"); free(context); - return GLFW_FALSE; + return NULL; } + return context; } diff --git a/src/wgl_context.h b/src/wgl_context.h index 6cb6d050..abf97559 100644 --- a/src/wgl_context.h +++ b/src/wgl_context.h @@ -151,6 +151,8 @@ typedef struct _GLFWlibraryWGL } _GLFWlibraryWGL; +// WGL-specific user context data +// typedef struct _GLFWusercontext { _GLFWwindow* window;