Skip to content

Instantly share code, notes, and snippets.

@anunyin
Last active January 22, 2019 15:53
Show Gist options
  • Save anunyin/6604014 to your computer and use it in GitHub Desktop.
Save anunyin/6604014 to your computer and use it in GitHub Desktop.
SDL 2: Changing Resolution at Runtime
// windowHandle is a pointer to an SDL_Window object, acquired and stored from your original call to SDL_CreateWindow
SDL_DestroyWindow(windowHandle);
// You'll probably want to keep the window title and window position unchanged
// by caching those values before you destroy the previous window.
windowHandle = SDL_CreateWindow("Window Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, newWidth, newHeight, SDL_WINDOW_OPENGL);
// windowContext is an SDL_GLContext object, acquired and stored from your initial SDL_GL_CreateContext call.
SDL_GL_MakeCurrent(windowHandle, windowContext);
// Again, keeping in mind that resolution and window size are separate things --
// It's possible for the resolution to be lower than the window size (e.g. fullscreen windowed).
// In that case, you don't want to set your viewport size to be smaller than the actual window display.
// You can get the correct size for the viewport by polling window size from SDL.
int w, h;
SDL_GetWindowSize(&w, &h);
glViewport(w, h);
// There seems to be a bug where if relative mouse mouse is enabled when you create a new window,
// the window still reports that it has input & mouse focus, but it doesn't send any events for
// mouse motion or mouse button clicks. You can fix this by toggling relative mouse mode.
if (SDL_GetRelativeMouseMode())
{
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_SetRelativeMouseMode(SDL_TRUE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment