SDL Not Responding

Aug 3, 2010 at 4:49am
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <SDL/SDL.h>

using namespace std;

int main(int argc, char* args[])
{
    SDL_Surface* map = NULL;
    SDL_Surface* screen = NULL;

    SDL_Init( SDL_INIT_EVERYTHING );

    screen = SDL_SetVideoMode( 630, 627, 32, SDL_SWSURFACE );
    map = SDL_LoadBMP( "MapBMP.bmp" );

    SDL_BlitSurface( map, NULL, screen, NULL );
    SDL_Flip( screen );

    SDL_FreeSurface( map );
    SDL_Delay( 10000 );
    SDL_Quit();
    return 0;
}


Thanks, Tyler Petresky
Aug 3, 2010 at 5:15am
The program goes unresponsive because it's not responding, it's sleeping.

Sleeping for extraordinarily long periods is a bad idea for this very reason.

In order to properly respond to the OS, you need to run the message pump. In SDL this is accomplished with the Event system.

Replace this

 
    SDL_Delay( 10000 );


with this:
1
2
3
4
5
6
7
8
SDL_Event evt;
bool programrunning = true;
while(programrunning)
{
  SDL_WaitEvent(&evt);
  if(evt.type == SDL_QUIT)
    programrunning = false;
}
Aug 3, 2010 at 5:19am
THANK YOU!! haha. that fixed it, but may I ask, what exactly is this doing?
Aug 3, 2010 at 5:58am
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.
Aug 3, 2010 at 6:04am
okay, so what if i wanted the program to not crash, but still be able to do things. just change the SDL_QUIT to something else?
Aug 3, 2010 at 8:22am
Yes, though you should probably be handing the SDL_QUIT event in addition to any other events you want to handle.
Aug 3, 2010 at 2:58pm
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.
Topic archived. No new replies allowed.