From e1e9e47d95c822593dd5aa07e88cd8a75dc9d4c1 Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 20 Mar 2023 20:31:31 -0700 Subject: [PATCH] Always update scaling before rendering This fixes the following bug that only occurs on Wayland: If the game is configured to be fullscreened and in stretch mode, on startup, it won't be in stretch mode. It will appear to be in letterbox mode, but the game still thinks it's in stretch mode. This is because during the ResizeScreen() call on startup, for whatever reason, the window size will be reported to be the default size (640 by 480) instead of the screen resolution of the monitor, as one would expect from being in fullscreen. It seems like when the game queries the window size, the window isn't actually in fullscreen at that time, even though this is after fullscreen has been set to true. To fix this, I decided to always update the logical size before SDL_RenderPresent() is called. To make this neater, I put the scaling code in its own function named UpdateScaling(). This bug has existed since 2.3 and does not occur on X11. I tested this on GNOME Wayland, and for testing it on X11, I used Openbox in a Xephyr session while running VVVVVV with SDL_VIDEODRIVER=x11. --- desktop_version/src/Screen.cpp | 60 ++++++++++++++++++---------------- desktop_version/src/Screen.h | 1 + 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/desktop_version/src/Screen.cpp b/desktop_version/src/Screen.cpp index 5d89a5bb..5d0fbd0e 100644 --- a/desktop_version/src/Screen.cpp +++ b/desktop_version/src/Screen.cpp @@ -170,33 +170,6 @@ void Screen::ResizeScreen(int x, int y) SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); } } - if (scalingMode == SCALING_STRETCH) - { - int winX, winY; - GetWindowSize(&winX, &winY); - int result = SDL_RenderSetLogicalSize(m_renderer, winX, winY); - if (result != 0) - { - vlog_error("Error: could not set logical size: %s", SDL_GetError()); - return; - } - result = SDL_RenderSetIntegerScale(m_renderer, SDL_FALSE); - if (result != 0) - { - vlog_error("Error: could not set scale: %s", SDL_GetError()); - return; - } - } - else - { - SDL_RenderSetLogicalSize(m_renderer, SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS); - int result = SDL_RenderSetIntegerScale(m_renderer, (SDL_bool) (scalingMode == SCALING_INTEGER)); - if (result != 0) - { - vlog_error("Error: could not set scale: %s", SDL_GetError()); - return; - } - } } void Screen::ResizeToNearestMultiple(void) @@ -267,8 +240,39 @@ void Screen::GetWindowSize(int* x, int* y) } } +void Screen::UpdateScaling(void) +{ + int width; + int height; + if (scalingMode == SCALING_STRETCH) + { + GetWindowSize(&width, &height); + } + else + { + width = SCREEN_WIDTH_PIXELS; + height = SCREEN_HEIGHT_PIXELS; + } + int result = SDL_RenderSetLogicalSize(m_renderer, width, height); + if (result != 0) + { + vlog_error("Error: could not set logical size: %s", SDL_GetError()); + return; + } + + result = SDL_RenderSetIntegerScale(m_renderer, (SDL_bool) (scalingMode == SCALING_INTEGER)); + if (result != 0) + { + vlog_error("Error: could not set scale: %s", SDL_GetError()); + } +} + void Screen::RenderPresent(void) { + /* In certain cases, the window size might mismatch with the logical size. + * So it's better to just always call this. */ + UpdateScaling(); + SDL_RenderPresent(m_renderer); graphics.clear(); } @@ -288,7 +292,7 @@ void Screen::toggleFullScreen(void) void Screen::toggleScalingMode(void) { scalingMode = (scalingMode + 1) % NUM_SCALING_MODES; - ResizeScreen(-1, -1); + UpdateScaling(); } void Screen::toggleLinearFilter(void) diff --git a/desktop_version/src/Screen.h b/desktop_version/src/Screen.h index 02284a59..dfb4bd48 100644 --- a/desktop_version/src/Screen.h +++ b/desktop_version/src/Screen.h @@ -19,6 +19,7 @@ public: void ResizeToNearestMultiple(void); void GetWindowSize(int* x, int* y); + void UpdateScaling(void); void RenderPresent(void); void toggleFullScreen(void);