Skip to content

Commit

Permalink
Restore window before closing (#453)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Spodi authored Dec 16, 2024
1 parent efcf668 commit 37f5aad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
13 changes: 7 additions & 6 deletions src/graphic/Fast3D/gfx_dxgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ void GetMonitorHzPeriod(std::tuple<HMONITOR, RECT, BOOL> 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;
Expand Down Expand Up @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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;
}
Expand Down
34 changes: 23 additions & 11 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,24 @@ 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) {
case WM_GETDPISCALEDSIZE:
// 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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 37f5aad

Please sign in to comment.