9
votes

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?

4

4 Answers

25
votes

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

12
votes

You may look at the wiki doc: SDL_SetWindowSize

4
votes

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);
}
1
votes
Window = SDL_CreateWindow(
  "Test",
  SDL_WINDOWPOS_UNDEFINED,
  SDL_WINDOWPOS_UNDEFINED,
  ScreenSizeX,
  ScreenSizeY,
  SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);

Use this function call