From 78ddfad54330619585b6819ce548fd8124ebfde2 Mon Sep 17 00:00:00 2001 From: mdietsch Date: Wed, 3 Aug 2016 10:58:49 +0200 Subject: [PATCH] Platform: avoid registering WNDCLASS more than once. Fixes an issue when creating the context more than once in a single application. Also changed the API calls to be explicitly "W" and added proper error reporting to this part of code. --- .../Platform/WindowlessWglApplication.cpp | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Magnum/Platform/WindowlessWglApplication.cpp b/src/Magnum/Platform/WindowlessWglApplication.cpp index 4a0da7fc6..6e07abfec 100644 --- a/src/Magnum/Platform/WindowlessWglApplication.cpp +++ b/src/Magnum/Platform/WindowlessWglApplication.cpp @@ -46,20 +46,29 @@ namespace Magnum { namespace Platform { WindowlessWglContext::WindowlessWglContext(const Configuration& configuration, Context* const magnumContext) { + /* Register the window class (if not yet done) */ + WNDCLASSW wc; + if(!GetClassInfoW(GetModuleHandleW(nullptr), L"Magnum Windowless Application", &wc)) { + wc = WNDCLASSW{ + 0, + DefWindowProcW, + 0, + 0, + GetModuleHandleW(nullptr), + nullptr, + nullptr, + HBRUSH(COLOR_BACKGROUND), + nullptr, + L"Magnum Windowless Application" + }; + + if(!RegisterClassW(&wc)) { + Error() << "Platform::WindowlessWglContext: cannot create window class:" << GetLastError(); + return; + } + } + /* Create the window */ - const WNDCLASS wc{ - 0, - DefWindowProc, - 0, - 0, - GetModuleHandle(nullptr), - nullptr, - nullptr, - HBRUSH(COLOR_BACKGROUND), - nullptr, - L"Magnum Windowless Application" - }; - if(!RegisterClass(&wc)) return; _window = CreateWindowW(wc.lpszClassName, L"Magnum Windowless Application", WS_OVERLAPPEDWINDOW, 0, 0, 32, 32, 0, 0, wc.hInstance, 0);