diff --git a/source/lib/sysdep/os/win/wutil.cpp b/source/lib/sysdep/os/win/wutil.cpp index 167598b6cc..705119fcbd 100644 --- a/source/lib/sysdep/os/win/wutil.cpp +++ b/source/lib/sysdep/os/win/wutil.cpp @@ -20,10 +20,6 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/* - * various Windows-specific utilities - */ - #include "precompiled.h" #include "lib/sysdep/os/win/wutil.h" @@ -40,6 +36,7 @@ #include // SHGetFolderPath #include +#include WINIT_REGISTER_EARLY_INIT(wutil_Init); @@ -409,31 +406,21 @@ HMODULE wutil_LibModuleHandle() static HWND hAppWindow; -static BOOL CALLBACK FindAppWindowByPid(HWND hWnd, LPARAM UNUSED(lParam)) +void wutil_SetAppWindow(SDL_Window* window) { - DWORD pid; - DWORD tid = GetWindowThreadProcessId(hWnd, &pid); - UNUSED2(tid); + SDL_SysWMinfo wmInfo; + SDL_VERSION(&wmInfo.version); + if (SDL_GetWindowWMInfo(window, &wmInfo) && wmInfo.subsystem == SDL_SYSWM_WINDOWS) + hAppWindow = wmInfo.info.win.window; +} - if(pid == GetCurrentProcessId()) - { - char windowName[100]; - GetWindowTextA(hWnd, windowName, 99); - if (strcmp(windowName, main_window_name) == 0) - hAppWindow = hWnd; - } - - return TRUE; // keep calling +void wutil_SetAppWindow(void* hwnd) +{ + hAppWindow = reinterpret_cast(hwnd); } HWND wutil_AppWindow() { - if(!hAppWindow) - { - WARN_IF_FALSE(EnumWindows(FindAppWindowByPid, 0)); - // (hAppWindow may still be 0 if we haven't created a window yet) - } - return hAppWindow; } diff --git a/source/lib/sysdep/os/win/wutil.h b/source/lib/sysdep/os/win/wutil.h index 6716372e5d..c93e70517d 100644 --- a/source/lib/sysdep/os/win/wutil.h +++ b/source/lib/sysdep/os/win/wutil.h @@ -201,6 +201,8 @@ extern HMODULE wutil_LibModuleHandle(); **/ extern HWND wutil_AppWindow(); +extern void wutil_SetAppWindow(void* hwnd); + extern void wutil_EnableHiDPIOnWindows(); #endif // #ifndef INCLUDED_WUTIL diff --git a/source/ps/VideoMode.cpp b/source/ps/VideoMode.cpp index d4035c386b..7642cde319 100644 --- a/source/ps/VideoMode.cpp +++ b/source/ps/VideoMode.cpp @@ -48,6 +48,10 @@ int DEFAULT_FULLSCREEN_H = 768; } // anonymous namespace #if OS_WIN +// We can't include wutil directly because GL headers conflict with Windows +// until we use a proper GL loader. +extern void wutil_SetAppWindow(SDL_Window* window); + // After a proper HiDPI integration we should switch to manifest until // SDL has an implemented HiDPI on Windows. extern void wutil_EnableHiDPIOnWindows(); @@ -164,6 +168,11 @@ bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen) else SDL_SetWindowGrab(m_Window, SDL_FALSE); +#if OS_WIN + // We need to set the window for an error dialog. + wutil_SetAppWindow(m_Window); +#endif + m_IsFullscreen = fullscreen; g_xres = m_CurrentW; diff --git a/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.cpp b/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.cpp index 799ec73a6c..57d54ee268 100644 --- a/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.cpp +++ b/source/tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.cpp @@ -589,6 +589,10 @@ ScenarioEditor::ScenarioEditor(wxWindow* parent) // Send setup messages to game engine: + // We need to set the window HWND for the error dialog instead of iteraing + // through all open windows on Windows. + POST_MESSAGE(InitAppWindow, (GetHandle())); + POST_MESSAGE(InitSDL, ()); POST_MESSAGE(SetCanvas, (static_cast(canvas), diff --git a/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp index 79aa03ae3f..d969deff5a 100644 --- a/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp +++ b/source/tools/atlas/GameInterface/Handlers/GraphicsSetupHandlers.cpp @@ -41,13 +41,19 @@ #include "ps/GameSetup/GameSetup.h" #include "renderer/Renderer.h" -static InputProcessor g_Input; +#if OS_WIN +// We don't include wutil header directly to prevent including Windows headers. +extern void wutil_SetAppWindow(void* hwnd); +#endif + +namespace AtlasMessage +{ + +InputProcessor g_Input; // This keeps track of the last in-game user input. // It is used to throttle FPS to save CPU & GPU. -static double last_user_activity; - -namespace AtlasMessage { +double last_user_activity; // see comment in GameLoop.cpp about ah_display_error before using INIT_HAVE_DISPLAY_ERROR const int g_InitFlags = INIT_HAVE_VMODE|INIT_NO_GUI; @@ -73,6 +79,15 @@ MESSAGEHANDLER(Init) g_VideoMode.InitNonSDL(); } +MESSAGEHANDLER(InitAppWindow) +{ +#if OS_WIN + wutil_SetAppWindow(msg->handle); +#else + UNUSED2(msg); +#endif +} + MESSAGEHANDLER(InitSDL) { UNUSED2(msg); @@ -254,4 +269,4 @@ MESSAGEHANDLER(RenderStyle) g_Renderer.SetOverlayRenderMode(msg->wireframe ? EDGED_FACES : SOLID); } -} +} // namespace AtlasMessage diff --git a/source/tools/atlas/GameInterface/Messages.h b/source/tools/atlas/GameInterface/Messages.h index f20806d416..b82f30bf3a 100644 --- a/source/tools/atlas/GameInterface/Messages.h +++ b/source/tools/atlas/GameInterface/Messages.h @@ -37,6 +37,10 @@ // Initialise some engine code. Must be called before anything else. MESSAGE(Init, ); +MESSAGE(InitAppWindow, + ((void*, handle)) // Atlas Window handle. + ); + // Initialise SDL-related code. Must be called before SetCanvas and InitGraphics. MESSAGE(InitSDL, );