From 54990638fd0ca2c5a375ab14b1a0735877230763 Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 20 Mar 2023 20:59:37 -0700 Subject: [PATCH] Persist windowed mode size through fullscreen mode Previously, the game would not store the size of the window itself, and would always call SDL_GetRendererOutputSize() (via Screen::GetWindowSize()) to figure out the size of the window. The only problem is, this would return the size of the whole monitor if the game was in fullscreen mode. And the only place where the original windowed mode size was stored would be in SDL itself, but that wouldn't persist after the game was closed. So, if you exited the game while in fullscreen mode, then your window size would get set to the size of your monitor (1920 by 1080 in my case). Then when you opened the game and toggled fullscreen off, it would go back to the default window size, which is 640 by 480. This is made worse, however, if you were in forced fullscreen mode when you previously exited the game in windowed mode. In that case, the game saves the size of 1920 by 1080, but doesn't save that you were in fullscreen mode, so opening the game not in forced fullscreen mode would result in you having a 1920 by 1080 window, but in windowed mode. Meaning that not even fullscreening and unfullscreening would put the game window back to normal size. The solution, of course, is to just store the window size ourselves, like any other screen setting, and only use GetWindowSize() if needed. And just to make things clear, I've also renamed the GetWindowSize() function to GetScreenSize(), because if it was named "window" it could lead one to think that it would always return the size of the screen in windowed mode, when in fact it returns the size of the screen whatever mode it is in - fullscreen size if in fullscreen mode and window size if in windowed mode. And doing this also fixes the FIXME above Screen::isForcedFullscreen(). --- desktop_version/src/Editor.cpp | 8 ++++---- desktop_version/src/Screen.cpp | 32 +++++++++++++------------------- desktop_version/src/Screen.h | 4 +++- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/desktop_version/src/Editor.cpp b/desktop_version/src/Editor.cpp index 729ed588..818530e0 100644 --- a/desktop_version/src/Editor.cpp +++ b/desktop_version/src/Editor.cpp @@ -2073,10 +2073,10 @@ void editorinput(void) ed.tiley=(game.my - (game.my%8))/8; if (gameScreen.scalingMode == SCALING_STRETCH) { // In this mode specifically, we have to fix the mouse coordinates - int winwidth, winheight; - gameScreen.GetWindowSize(&winwidth, &winheight); - ed.tilex = ed.tilex * 320 / winwidth; - ed.tiley = ed.tiley * 240 / winheight; + int screenwidth, screenheight; + gameScreen.GetScreenSize(&screenwidth, &screenheight); + ed.tilex = ed.tilex * 320 / screenwidth; + ed.tiley = ed.tiley * 240 / screenheight; } bool up_pressed = key.isDown(SDLK_UP) || key.isDown(SDL_CONTROLLER_BUTTON_DPAD_UP); diff --git a/desktop_version/src/Screen.cpp b/desktop_version/src/Screen.cpp index 5d0fbd0e..6921c183 100644 --- a/desktop_version/src/Screen.cpp +++ b/desktop_version/src/Screen.cpp @@ -29,9 +29,12 @@ void Screen::init(const struct ScreenSettings* settings) { m_window = NULL; m_renderer = NULL; + windowWidth = settings->windowWidth; + windowHeight = settings->windowHeight; isWindowed = !settings->fullscreen; scalingMode = settings->scalingMode; isFiltered = settings->linearFilter; + badSignalEffect = settings->badSignal; vsync = settings->useVsync; SDL_SetHintWithPriority( @@ -86,9 +89,7 @@ void Screen::init(const struct ScreenSettings* settings) LoadIcon(); - badSignalEffect = settings->badSignal; - - ResizeScreen(settings->windowWidth, settings->windowHeight); + ResizeScreen(windowWidth, windowHeight); } void Screen::destroy(void) @@ -100,11 +101,8 @@ void Screen::destroy(void) void Screen::GetSettings(struct ScreenSettings* settings) { - int width, height; - GetWindowSize(&width, &height); - - settings->windowWidth = width; - settings->windowHeight = height; + settings->windowWidth = windowWidth; + settings->windowHeight = windowHeight; settings->fullscreen = !isWindowed; settings->useVsync = vsync; @@ -136,13 +134,11 @@ void Screen::LoadIcon(void) void Screen::ResizeScreen(int x, int y) { - static int resX = SCREEN_WIDTH_PIXELS; - static int resY = SCREEN_HEIGHT_PIXELS; if (x != -1 && y != -1) { // This is a user resize! - resX = x; - resY = y; + windowWidth = x; + windowHeight = y; } if (!isWindowed || isForcedFullscreen()) @@ -166,7 +162,7 @@ void Screen::ResizeScreen(int x, int y) } if (x != -1 && y != -1) { - SDL_SetWindowSize(m_window, resX, resY); + SDL_SetWindowSize(m_window, windowWidth, windowHeight); SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); } } @@ -175,7 +171,7 @@ void Screen::ResizeScreen(int x, int y) void Screen::ResizeToNearestMultiple(void) { int w, h; - GetWindowSize(&w, &h); + GetScreenSize(&w, &h); // Check aspect ratio first bool using_width; @@ -229,7 +225,7 @@ void Screen::ResizeToNearestMultiple(void) } } -void Screen::GetWindowSize(int* x, int* y) +void Screen::GetScreenSize(int* x, int* y) { if (SDL_GetRendererOutputSize(m_renderer, x, y) != 0) { @@ -246,7 +242,7 @@ void Screen::UpdateScaling(void) int height; if (scalingMode == SCALING_STRETCH) { - GetWindowSize(&width, &height); + GetScreenSize(&width, &height); } else { @@ -280,7 +276,7 @@ void Screen::RenderPresent(void) void Screen::toggleFullScreen(void) { isWindowed = !isWindowed; - ResizeScreen(-1, -1); + ResizeScreen(windowWidth, windowHeight); if (game.currentmenuname == Menu::graphicoptions) { @@ -348,8 +344,6 @@ void Screen::recacheTextures(void) } } -/* FIXME: Launching in forced fullscreen then exiting and relaunching in normal - * mode will result in the window having fullscreen size but being windowed. */ bool Screen::isForcedFullscreen(void) { /* This is just a check to see if we're on a desktop or tenfoot setup. diff --git a/desktop_version/src/Screen.h b/desktop_version/src/Screen.h index dfb4bd48..7573ef8f 100644 --- a/desktop_version/src/Screen.h +++ b/desktop_version/src/Screen.h @@ -17,7 +17,7 @@ public: void ResizeScreen(int x, int y); void ResizeToNearestMultiple(void); - void GetWindowSize(int* x, int* y); + void GetScreenSize(int* x, int* y); void UpdateScaling(void); void RenderPresent(void); @@ -31,6 +31,8 @@ public: bool isForcedFullscreen(void); + int windowWidth; + int windowHeight; bool isWindowed; bool isFiltered; bool badSignalEffect;