From 37f5aadcfad762b1b0244f52a476dcf23be5df5e Mon Sep 17 00:00:00 2001 From: Spodi Date: Mon, 16 Dec 2024 15:47:19 +0100 Subject: [PATCH] Restore window before closing (#453) * Restore window before closing This looks a bit funny, but prevents saving the size of an maximized or minimized window. Also quitting should always call gfx_dxgi_close() or gfx_dxgi_close() now, no matter how you quit (except outright killing the process I guess). * Use SW_NORMAL instead of SW_RESTORE SW_NORMAL also works if the window was minimized in a maximized state * Untangle SDL_WINDOWEVENT --- src/graphic/Fast3D/gfx_dxgi.cpp | 13 +++++++------ src/graphic/Fast3D/gfx_sdl2.cpp | 34 ++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/graphic/Fast3D/gfx_dxgi.cpp b/src/graphic/Fast3D/gfx_dxgi.cpp index 540e532..250a927 100644 --- a/src/graphic/Fast3D/gfx_dxgi.cpp +++ b/src/graphic/Fast3D/gfx_dxgi.cpp @@ -301,6 +301,11 @@ void GetMonitorHzPeriod(std::tuple Monitor, double& Freque } } +static void gfx_dxgi_close() { + ShowWindow(dxgi.h_wnd, SW_NORMAL); // Restore window before closing, so normal window pos and size is saved + dxgi.is_running = false; +} + static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_param, LPARAM l_param) { char fileName[256]; Ship::WindowEvent event_impl; @@ -329,7 +334,7 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par } break; case WM_CLOSE: - dxgi.is_running = false; + gfx_dxgi_close(); break; case WM_DPICHANGED: { RECT* const prcNewWindow = (RECT*)l_param; @@ -344,7 +349,7 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par case WM_ENDSESSION: // This hopefully gives the game a chance to shut down, before windows kills it. if (w_param == TRUE) { - dxgi.is_running = false; + gfx_dxgi_close(); } break; case WM_ACTIVATEAPP: @@ -481,10 +486,6 @@ void gfx_dxgi_init(const char* game_name, const char* gfx_api_name, bool start_i DragAcceptFiles(dxgi.h_wnd, TRUE); } -static void gfx_dxgi_close() { - dxgi.is_running = false; -} - static void gfx_dxgi_set_fullscreen_changed_callback(void (*on_fullscreen_changed)(bool is_now_fullscreen)) { dxgi.on_fullscreen_changed = on_fullscreen_changed; } diff --git a/src/graphic/Fast3D/gfx_sdl2.cpp b/src/graphic/Fast3D/gfx_sdl2.cpp index f41aa37..e8d3316 100644 --- a/src/graphic/Fast3D/gfx_sdl2.cpp +++ b/src/graphic/Fast3D/gfx_sdl2.cpp @@ -284,6 +284,11 @@ static int target_fps = 60; #define FRAME_INTERVAL_US_NUMERATOR 1000000 #define FRAME_INTERVAL_US_DENOMINATOR (target_fps) +static void gfx_sdl_close(void) { + SDL_RestoreWindow(wnd); // Restore window before closing, so normal window pos and size is saved + is_running = false; +} + #ifdef _WIN32 static LRESULT CALLBACK gfx_sdl_wnd_proc(HWND h_wnd, UINT message, WPARAM w_param, LPARAM l_param) { switch (message) { @@ -291,6 +296,12 @@ static LRESULT CALLBACK gfx_sdl_wnd_proc(HWND h_wnd, UINT message, WPARAM w_para // Something is wrong with SDLs original implementation of WM_GETDPISCALEDSIZE, so pass it to the default // system window procedure instead. return DefWindowProc(h_wnd, message, w_param, l_param); + case WM_ENDSESSION: + // Apparently SDL2 does not handle this + if (w_param == TRUE) { + gfx_sdl_close(); + } + break; default: // Pass anything else to SDLs original window procedure. return CallWindowProc((WNDPROC)SDL_WndProc, h_wnd, message, w_param, l_param); @@ -420,10 +431,6 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s } } -static void gfx_sdl_close() { - is_running = false; -} - static void gfx_sdl_set_fullscreen_changed_callback(void (*on_fullscreen_changed)(bool is_now_fullscreen)) { on_fullscreen_changed_callback = on_fullscreen_changed; } @@ -533,12 +540,17 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) { break; #endif case SDL_WINDOWEVENT: - if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - SDL_GL_GetDrawableSize(wnd, &window_width, &window_height); - } else if (event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(wnd)) { - // We listen specifically for main window close because closing main window - // on macOS does not trigger SDL_Quit. - is_running = false; + switch (event.window.event) { + case SDL_WINDOWEVENT_SIZE_CHANGED: + SDL_GL_GetDrawableSize(wnd, &window_width, &window_height); + break; + case SDL_WINDOWEVENT_CLOSE: + if (event.window.windowID == SDL_GetWindowID(wnd)) { + // We listen specifically for main window close because closing main window + // on macOS does not trigger SDL_Quit. + gfx_sdl_close(); + } + break; } break; case SDL_DROPFILE: @@ -547,7 +559,7 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) { Ship::Context::GetInstance()->GetConsoleVariables()->Save(); break; case SDL_QUIT: - is_running = false; + gfx_sdl_close(); break; } }