Skip to content

Instantly share code, notes, and snippets.

@Cheeseness
Last active May 4, 2023 23:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Cheeseness/8a49668d0a43278b72b2 to your computer and use it in GitHub Desktop.
Save Cheeseness/8a49668d0a43278b72b2 to your computer and use it in GitHub Desktop.
C++ SDL Fullscreen Toggle
/*
* A function for toggling windowed mode in a C++ SDL app. Fullscreen windows will appear on whichever screen the window was on.
*
* Could be used in conjunction with SDL_GetDisplayName(int displayIndex)
* and SDL_GetNumVideoDisplays(void) to programmatically force fullscreen onto a particular display
*/
void toggleWindowed()
{
//Grab the mouse so that we don't end up with unexpected movement when the dimensions/position of the window changes.
SDL_SetRelativeMouseMode(SDL_TRUE);
windowed = !windowed;
if (windowed)
{
int i = SDL_GetWindowDisplayIndex(win);
screenWidth = windowedWidth;
screenHeight = windowedHeight;
SDL_SetWindowFullscreen(win, 0);
}
else
{
int i = SDL_GetWindowDisplayIndex(win);
SDL_Rect j;
SDL_GetDisplayBounds(i, &j);
screenWidth = j.w;
screenHeight = j.h;
SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
recalculateResolution(); //This function sets appropriate font sizes/UI positions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment