Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize SDL2 window?

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?

like image 823
TakingItCasual Avatar asked Dec 23 '13 00:12

TakingItCasual


2 Answers

You may look at the wiki doc: SDL_SetWindowSize

like image 193
Jarod42 Avatar answered Oct 15 '22 05:10

Jarod42


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);
}
like image 34
LastBlow Avatar answered Oct 15 '22 06:10

LastBlow