Resize SDL2 window?
Asked Answered
C

4

16

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?

Causey answered 23/12, 2013 at 0:40 Comment(0)
V
18

You may look at the wiki doc: SDL_SetWindowSize

Vandusen answered 23/12, 2013 at 1:54 Comment(0)
P
30

I believe that that you could use the SDL_WINDOW_RESIZABLE flag in SDL_CreateWindow to make the window resizable.

Pule answered 28/11, 2014 at 19:21 Comment(0)
V
18

You may look at the wiki doc: SDL_SetWindowSize

Vandusen answered 23/12, 2013 at 1:54 Comment(0)
P
14

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 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);
}
Poteat answered 6/4, 2019 at 7:26 Comment(4)
Why call SDL_SetWindowSize with the current size of your window?Pyrethrin
SDL_SetWindowSize is called in void InceptionServices::resizeWindow(int windowWidth, int windowHeight) with the size of the resized window m_event.window.data1 and m_event.window.data2 given by SDL in the switch.Poteat
Yes, but the window will be resized automatically. You don't need to call SDL_SetWindowSize for it to be resized.Pyrethrin
It 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);.Poteat
C
2
Window = SDL_CreateWindow(
  "Test",
  SDL_WINDOWPOS_UNDEFINED,
  SDL_WINDOWPOS_UNDEFINED,
  ScreenSizeX,
  ScreenSizeY,
  SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);

Use this function call

Coen answered 4/1, 2021 at 18:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.