Resize SDL2 window?
Solution 1
You may look at the wiki doc: SDL_SetWindowSize
Solution 2
I believe that that you could use the SDL_WINDOW_RESIZABLE flag in SDL_CreateWindow to make the window resizable.
Solution 3
To resize a window in SDL, first set it with the flag SDL_WINDOW_RESIZABLE
, then detect the resizing window event in a switch and finally call the following methods SDL_SetWindowSize(m_window, windowWidth, windowHeight)
and glViewport(0, 0, windowWidth, windowHeight)
.
In the switch
, use the flag SDL_WINDOWEVENT_RESIZED
if you want only the final size of the window or SDL_WINDOWEVENT_SIZE_CHANGED
if you want all the sizes between the first and the final.
To finish, update your own camera with the new window width and height.
m_window = SDL_CreateWindow("INCEPTION",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
m_windowWidth, m_windowHeight,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
switch (m_event.type) {
case SDL_WINDOWEVENT:
if (m_event.window.event == SDL_WINDOWEVENT_RESIZED) {
logFileStderr("MESSAGE:Resizing window...\n");
resizeWindow(m_event.window.data1, m_event.window.data2);
}
break;
default:
break;
}
void InceptionServices::resizeWindow(int windowWidth, int windowHeight) {
logFileStderr("MESSAGE: Window width, height ... %d, %d\n", windowWidth, windowHeight);
m_camera->resizeWindow(windowWidth, windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
}
Solution 4
Window = SDL_CreateWindow(
"Test",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
ScreenSizeX,
ScreenSizeY,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);
Use this function call

TakingItCasual
Programming hobbyist. Currently have the most experience with Python, LaTeX, and Kotlin.
Updated on January 05, 2021Comments
-
TakingItCasual almost 2 years
Just made the jump from SDL1.2 to SDL2, been converting my code but couldn't figure out how to resize the window. Here's the code I have now:
SDL_DestroyWindow(Window); Window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenSizeX, ScreenSizeY, SDL_WINDOW_SHOWN); screen = SDL_GetWindowSurface(Window);
Which as you can see just destroys the window and creates a new one. Sloppy but it works. What I want is to just resize the window, is it possible?
-
HolyBlackCat over 3 yearsWhy call
SDL_SetWindowSize
with the current size of your window? -
LastBlow over 3 years
SDL_SetWindowSize
is called invoid InceptionServices::resizeWindow(int windowWidth, int windowHeight)
with the size of the resized windowm_event.window.data1
andm_event.window.data2
given by SDL in the switch. -
HolyBlackCat over 3 yearsYes, but the window will be resized automatically. You don't need to call
SDL_SetWindowSize
for it to be resized. -
LastBlow over 3 yearsIt made sense to me for completeness, I guess, but I agree that SDL resizes the window automatically, so I just removed the line
SDL_SetWindowSize(m_window, windowWidth, windowHeight);
.