set window fullscreen display mode to current desktop mode before going fullscreen

This commit is contained in:
ohsqueezy 2023-12-05 15:28:36 -05:00
parent a6a110141d
commit 5235535cc2
2 changed files with 33 additions and 2 deletions

View File

@ -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<SDL_Window*>(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<SDL_Window*>(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<SDL_Window*>(window()), &mode) != 0)
{
sb::Log::sdl_error("Error setting window's fullscreen display mode");
}
}
sb::Log::log("fullscreen requested");
SDL_SetWindowFullscreen(const_cast<SDL_Window*>(window()), SDL_WINDOW_FULLSCREEN);
}

View File

@ -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;
};