13

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?

Community
  • 1
  • 1
TakingItCasual
  • 771
  • 1
  • 7
  • 22

4 Answers4

30

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

novice
  • 400
  • 1
  • 6
  • 13
16

You may look at the wiki doc: SDL_SetWindowSize

Jarod42
  • 203,559
  • 14
  • 181
  • 302
12

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);
}
LastBlow
  • 617
  • 9
  • 16
  • Why call `SDL_SetWindowSize` with the current size of your window? – HolyBlackCat Apr 06 '19 at 08:04
  • `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. – LastBlow Apr 07 '19 at 20:18
  • 3
    Yes, but the window will be resized automatically. You don't need to call `SDL_SetWindowSize` for it to be resized. – HolyBlackCat Apr 07 '19 at 20:19
  • 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);`. – LastBlow Apr 07 '19 at 20:31
2
Window = SDL_CreateWindow(
  "Test",
  SDL_WINDOWPOS_UNDEFINED,
  SDL_WINDOWPOS_UNDEFINED,
  ScreenSizeX,
  ScreenSizeY,
  SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);

Use this function call

taylorthurlow
  • 2,953
  • 3
  • 28
  • 42
Tonia Sanzo
  • 335
  • 2
  • 10