From c85294e0b2b5a5469b087e24c1c273cb0c8f4628 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 31 Aug 2014 14:17:31 +0200 Subject: [PATCH] Window class cleanup. --- src/win32_init.c | 9 ++--- src/win32_platform.h | 9 ++--- src/win32_window.c | 92 +++++++++++++++++++++++--------------------- 3 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/win32_init.c b/src/win32_init.c index 1b551a79..51fe08ee 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -211,6 +211,9 @@ int _glfwPlatformInit(void) _control87(MCW_EM, MCW_EM); #endif + if (!_glfwRegisterWindowClass()) + return GL_FALSE; + if (!_glfwInitContextAPI()) return GL_FALSE; @@ -222,11 +225,7 @@ int _glfwPlatformInit(void) void _glfwPlatformTerminate(void) { - if (_glfw.win32.classAtom) - { - UnregisterClassW(_GLFW_WNDCLASSNAME, GetModuleHandleW(NULL)); - _glfw.win32.classAtom = 0; - } + _glfwUnregisterWindowClass(); // Restore previous foreground lock timeout system setting SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, diff --git a/src/win32_platform.h b/src/win32_platform.h index 3040e85d..953e6970 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -130,10 +130,6 @@ typedef HRESULT (WINAPI * DWMISCOMPOSITIONENABLED_T)(BOOL*); #define _glfw_DwmIsCompositionEnabled _glfw.win32.dwmapi.DwmIsCompositionEnabled -// We use versioned window class names in order not to cause conflicts -// between applications using different versions of GLFW -#define _GLFW_WNDCLASSNAME L"GLFW30" - #define _GLFW_RECREATION_NOT_NEEDED 0 #define _GLFW_RECREATION_REQUIRED 1 #define _GLFW_RECREATION_IMPOSSIBLE 2 @@ -188,7 +184,6 @@ typedef struct _GLFWwindowWin32 //------------------------------------------------------------------------ typedef struct _GLFWlibraryWin32 { - ATOM classAtom; DWORD foregroundLockTimeout; char* clipboardString; @@ -255,6 +250,10 @@ typedef struct _GLFWtimeWin32 // Prototypes for platform specific internal functions //======================================================================== +// Window class +GLboolean _glfwRegisterWindowClass(void); +void _glfwUnregisterWindowClass(void); + // Desktop compositing BOOL _glfwIsCompositionEnabled(void); diff --git a/src/win32_window.c b/src/win32_window.c index 57e17660..a75ede97 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -34,6 +34,8 @@ #define _GLFW_KEY_INVALID -2 +#define _GLFW_WNDCLASSNAME L"GLFW30" + // Updates the cursor clip rect // @@ -838,43 +840,6 @@ static void getFullWindowSize(_GLFWwindow* window, *fullHeight = rect.bottom - rect.top; } -// Registers the GLFW window class -// -static ATOM registerWindowClass(void) -{ - WNDCLASSW wc; - ATOM classAtom; - - // Set window class parameters - wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; - wc.lpfnWndProc = (WNDPROC) windowProc; - wc.cbClsExtra = 0; // No extra class data - wc.cbWndExtra = sizeof(void*) + sizeof(int); // Make room for one pointer - wc.hInstance = GetModuleHandleW(NULL); - wc.hCursor = LoadCursorW(NULL, IDC_ARROW); - wc.hbrBackground = NULL; // No background - wc.lpszMenuName = NULL; // No menu - wc.lpszClassName = _GLFW_WNDCLASSNAME; - - // Load user-provided icon if available - wc.hIcon = LoadIconW(GetModuleHandleW(NULL), L"GLFW_ICON"); - if (!wc.hIcon) - { - // No user-provided icon found, load default icon - wc.hIcon = LoadIconW(NULL, IDI_WINLOGO); - } - - classAtom = RegisterClassW(&wc); - if (!classAtom) - { - _glfwInputError(GLFW_PLATFORM_ERROR, - "Win32: Failed to register window class"); - return 0; - } - - return classAtom; -} - // Creates the GLFW window and rendering context // static int createWindow(_GLFWwindow* window, @@ -986,6 +951,52 @@ static void destroyWindow(_GLFWwindow* window) } +////////////////////////////////////////////////////////////////////////// +////// GLFW internal API ////// +////////////////////////////////////////////////////////////////////////// + +// Registers the GLFW window class +// +GLboolean _glfwRegisterWindowClass(void) +{ + WNDCLASSW wc; + + wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; + wc.lpfnWndProc = (WNDPROC) windowProc; + wc.cbClsExtra = 0; // No extra class data + wc.cbWndExtra = sizeof(void*) + sizeof(int); // Make room for one pointer + wc.hInstance = GetModuleHandleW(NULL); + wc.hCursor = LoadCursorW(NULL, IDC_ARROW); + wc.hbrBackground = NULL; // No background + wc.lpszMenuName = NULL; // No menu + wc.lpszClassName = _GLFW_WNDCLASSNAME; + + // Load user-provided icon if available + wc.hIcon = LoadIconW(GetModuleHandleW(NULL), L"GLFW_ICON"); + if (!wc.hIcon) + { + // No user-provided icon found, load default icon + wc.hIcon = LoadIconW(NULL, IDI_WINLOGO); + } + + if (!RegisterClassW(&wc)) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "Win32: Failed to register window class"); + return GL_FALSE; + } + + return GL_TRUE; +} + +// Unregisters the GLFW window class +// +void _glfwUnregisterWindowClass(void) +{ + UnregisterClassW(_GLFW_WNDCLASSNAME, GetModuleHandleW(NULL)); +} + + ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// @@ -997,13 +1008,6 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, { int status; - if (!_glfw.win32.classAtom) - { - _glfw.win32.classAtom = registerWindowClass(); - if (!_glfw.win32.classAtom) - return GL_FALSE; - } - if (!createWindow(window, wndconfig, ctxconfig, fbconfig)) return GL_FALSE;