OKAY!! I haven't used SDL in a while and I decided to whip up a small, simple program to make sure it's still working. Whenever I run the program it works fine EXCEPT for whenever I try to move the window, the program goes unresponsive. It also changes the way my cursor looks.
It's all in SDL's documentation. Read the section on events.
Basically:
- events are when something happens in the machine that impacts the program. For example, events occur whenever a key is pressed, or a gamepad button is pressed, or the mouse is moved/clicked, or the window resized, etc, etc (SDL lists them all in the docs).
- SDL_WaitEvent sleeps until a new event becomes available. Once the event happens, our 'evt' structure is filled with the details about the event.
- evt.type is different things depending on the event (mouse move, click, key press, etc). A quit event happens when the user is trying to exit the program.
- throw that in a loop and your program will be constantly sleeping, with occasional interruptions to handle different events.
- in my above code, I'm ignoring all events except the SDL_QUIT event (they are ignored by getting them via SDL_WaitEvent and then simply not doing anything with them). The SDL_QUIT event changes a var to escape the loop so the program can exit.
If you want the program to be constantly active, then you don't use SDL_WaitEvent since that sleeps until an event becomes available.
You'd want SDL_PollEvent:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while(programrunning)
{
// keep looping and processing events here until there are no more events to process
while( SDL_PollEvent(&evt) )
{
switch(evt.type)
{
case SDL_QUIT: programrunning = false; break;
/* process other events you want to handle here */
}
}
// put code you want to run constantly here. This code will keep looping, and will take the occasional break only
// to process pending events
}
Note that this code does not sleep at all, so your program will consume 100% CPU time. There's also no telling how fast the program will be running since different computers will run at different speeds, so if this is for a game or something, the game will be running as fast as the computer can handle it.
Generally you use a mix of timing functions (SDL_GetTicks()) and sleeping functions (SDL_Delay()) to make your game run at a fixed speed on any computer.