Skip to content

Commit

Permalink
Restore window before closing
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Spodi committed Oct 10, 2024
1 parent 57a5994 commit 7593f3e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 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 @@ -299,6 +299,11 @@ void GetMonitorHzPeriod(std::tuple<HMONITOR, RECT, BOOL> Monitor, double& Freque
}
}

static void gfx_dxgi_close() {
ShowWindow(dxgi.h_wnd, SW_RESTORE); // 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 @@ -327,7 +332,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 @@ -342,7 +347,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 @@ -451,10 +456,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
19 changes: 13 additions & 6 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,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 @@ -418,10 +429,6 @@ static void gfx_sdl_init(const char* game_name, const char* gfx_api_name, bool s
}
}

static void gfx_sdl_close(void) {
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 @@ -501,7 +508,7 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) {
} 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;
gfx_sdl_close();
}
break;
case SDL_DROPFILE:
Expand All @@ -510,7 +517,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 7593f3e

Please sign in to comment.