Example: Resizable Windows

Here is an example for resizeable windows in SDL. Basically, make a regular SDL program, but make sure in SDL_SetVideoMode that you include SDL_RESIZABLE as one of the flags. Then handle the event SDL_VIDEORESIZE, where your event.resize holds the data w and h, which are the new window height. SDL_Quit() and SDL_SetVideoMode() your new window to it's size;

Note: When making a real program, you should never have an event loop with no call to SDL_WaitEvent or some other mechanism to wait when the program has nothing to do.

#include <iostream>
#include <stdlib.h>
#include <SDL/SDL.h>

int main(int argc, char* argv[]){ //Our main program
        SDL_Surface *screen;
        SDL_Event event; //Events
        bool done = false; //Not done before we've started...

        if(SDL_Init(SDL_INIT_VIDEO) < 0){ //Could we start SDL_VIDEO?
                std::cerr << "Couldn't init SDL"; //Nope, output to stderr and quit

        atexit(SDL_Quit); //Now that we're enabled, make sure we cleanup

        screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_RESIZABLE); //Create a 640x480x32 resizable window

        if(!screen){ //Couldn't create window?
                std::cerr << "Couldn't create screen"; //Output to stderr and quit

        while(!done){ //While program isn't done
                while(SDL_PollEvent(&event)){ //Poll events
                        switch(event.type){ //Check event type
                        case SDL_QUIT: //User hit the X (or equivelent)
                                done = true; //Make the loop end
                                break; //We handled the event
                        case SDL_VIDEORESIZE: //User resized window
                                screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32,
                                        SDL_HWSURFACE | SDL_RESIZABLE); // Create new window
                                break; //Event handled, fetch next :)
                        } //Finished with current event
                } //Done with all events for now
        } //Program done, exited



Resizable_Windows (last edited 2008-12-12 01:44:50 by KenBull)