From 0121c75a370e5213b93d2d5862770e1b2455f6bd Mon Sep 17 00:00:00 2001 From: Misa Date: Sun, 4 Feb 2024 17:53:57 -0800 Subject: [PATCH] Fix resize-to-nearest could be larger than desktop This fixes the possibility of the "resize to nearest" graphics option resizing the game window to be bigger than the resolution of the user's desktop monitor. To fix this, just subtract multiples of 320x240 until the chosen multiple is smaller than the dimensions of the desktop. Discord user Dzhake discovered this issue. --- desktop_version/src/Screen.cpp | 36 ++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/desktop_version/src/Screen.cpp b/desktop_version/src/Screen.cpp index aaa78297..cedf3115 100644 --- a/desktop_version/src/Screen.cpp +++ b/desktop_version/src/Screen.cpp @@ -179,6 +179,25 @@ void Screen::ResizeScreen(int x, int y) } } +static void constrain_to_desktop(int display_index, int* width, int* height) +{ + SDL_DisplayMode display_mode = {}; + int success = SDL_GetDesktopDisplayMode(display_index, &display_mode); + if (success != 0) + { + vlog_error("Could not get desktop display mode: %s", SDL_GetError()); + return; + } + + while ((*width > display_mode.w || *height > display_mode.h) + && *width > SCREEN_WIDTH_PIXELS && *height > SCREEN_HEIGHT_PIXELS) + { + // We are too big, take away one multiple + *width -= SCREEN_WIDTH_PIXELS; + *height -= SCREEN_HEIGHT_PIXELS; + } +} + void Screen::ResizeToNearestMultiple(void) { int w, h; @@ -228,12 +247,25 @@ void Screen::ResizeToNearestMultiple(void) if (using_width) { - ResizeScreen(final_dimension, final_dimension / 4 * 3); + w = final_dimension; + h = final_dimension / 4 * 3; } else { - ResizeScreen(final_dimension * 4 / 3, final_dimension); + w = final_dimension * 4 / 3; + h = final_dimension; } + + windowDisplay = SDL_GetWindowDisplayIndex(m_window); + if (windowDisplay < 0) + { + vlog_error("Could not get display index: %s", SDL_GetError()); + windowDisplay = 0; + } + + constrain_to_desktop(windowDisplay, &w, &h); + + ResizeScreen(w, h); } void Screen::GetScreenSize(int* x, int* y)