From 5235535cc2d3e251ed40d8df626c0b1275806877 Mon Sep 17 00:00:00 2001 From: frank Date: Tue, 5 Dec 2023 15:28:36 -0500 Subject: [PATCH] set window fullscreen display mode to current desktop mode before going fullscreen --- src/Display.cpp | 26 ++++++++++++++++++++++++-- src/Display.hpp | 9 +++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Display.cpp b/src/Display.cpp index e921273..69f9269 100644 --- a/src/Display.cpp +++ b/src/Display.cpp @@ -184,8 +184,6 @@ void sb::Display::respond(SDL_Event& event) } } -/* Use SDL window flags to determine if fullscreen is on or off and use SDL fullscreen - * function to toggle the fullscreen state */ void sb::Display::toggle_fullscreen() const { if (SDL_GetWindowFlags(const_cast(window())) & SDL_WINDOW_FULLSCREEN) @@ -195,6 +193,30 @@ void sb::Display::toggle_fullscreen() const } else { + /* Get the index of the display currently displaying the game window */ + int display_index; + if ((display_index = SDL_GetWindowDisplayIndex(const_cast(window()))) < 0) + { + display_index = 0; + sb::Log::sdl_error("Error getting current display index of window, defaulting to index 0."); + } + + /* Get the display mode (including the resolution) of the display currently displaying the window */ + SDL_DisplayMode mode; + if (SDL_GetDesktopDisplayMode(display_index, &mode) != 0) + { + sb::Log::sdl_error("Error getting display mode"); + } + else + { + /* Set the window's fullscreen display mode to the same as the desktop to use the same resolution */ + sb::Log::log("Setting window's fullscreen display mode to current desktop display mode"); + if (SDL_SetWindowDisplayMode(const_cast(window()), &mode) != 0) + { + sb::Log::sdl_error("Error setting window's fullscreen display mode"); + } + } + sb::Log::log("fullscreen requested"); SDL_SetWindowFullscreen(const_cast(window()), SDL_WINDOW_FULLSCREEN); } diff --git a/src/Display.hpp b/src/Display.hpp index 1991259..1b8e7f9 100644 --- a/src/Display.hpp +++ b/src/Display.hpp @@ -51,6 +51,15 @@ namespace sb */ void respond(SDL_Event& event); + /*! + * Query SDL window flags to determine if fullscreen is on or off and use SDL's fullscreen function to toggle the fullscreen + * state to the opposite state. + * + * Before calling SDL's fullscreen function, this function queries the display index to find out which display (for example, + * which monitor in a dual-screen system) is displaying the window. It then sets the window's fullscreen display mode to the + * current desktop display mode of that display. This ensures that the fullscreen will succeed smoothly, without a resolution + * change, but it also requires that the game is adaptable to dynamic window sizing. + */ void toggle_fullscreen() const; };