Chapter 3 – The SDL Event Loop

In SDL, input and responses are handled with events. Here’s the definition of SDL_Event

typedef union{
  Uint8 type;
  SDL_ActiveEvent active;
  SDL_KeyboardEvent key;
  SDL_MouseMotionEvent motion;
  SDL_MouseButtonEvent button;
  SDL_JoyAxisEvent jaxis;
  SDL_JoyBallEvent jball;
  SDL_JoyHatEvent jhat;
  SDL_JoyButtonEvent jbutton;
  SDL_ResizeEvent resize;
  SDL_ExposeEvent expose;
  SDL_QuitEvent quit;
  SDL_UserEvent user;
  SDL_SywWMEvent syswm;
} SDL_Event;

Every event in the SDL_Event union holds its specific information. For example, SDL_KeyboardEvent holds the key code of the key pressed. The type member tells you which kind of event has been received. For example, a key-press, a mouse-click, a timer tick etc.

Now event event has it’s own event type which is stored in the type member. For example an SDL_QuitEvent has the type SDL_QUIT and an SDL_KeyboardEvent has the types SDL_KEYDOWN and SDL_KEYUP. (You know, this could be nice work-around for RTTI in C).

In an SDL application, you have to constantly loop through the event stack and respond to every new event. Something like this:

while(RUNNING)
{
    while(SDL_PollEvent(EVENT))
    {
         /* handle the events */
    }
}

Now let’s rewrite our sample program from the previous tutorial and add an event loop to it.

#include <SDL/SDL.h>

int main(int argc, char* argv[])
{
    /* A pointer to the video surface */
    SDL_Surface* screen = NULL;

    /* Initialize SDL's video sub-system */
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        fprintf(stderr, SDL_GetError());

    /* Quit SDL on exit */
    atexit(SDL_Quit);

    /* Create a double buffered window 640 pixels wide and 480 pixels long */
    screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);

    /* Error check */
    if (!screen) fprintf(stderr, SDL_GetError());

    /* An SDL_Event */
    SDL_Event event;

    /* A bool to check if the program has exited */
    int quit = 0;

    /* While the program is running */
    while (!quit)
    {
        /* Check for new events */
        while(SDL_PollEvent(&event))
        {
            /* If a quit event has been sent */
            if (event.type == SDL_QUIT)
            {
                /* Quit the application */
                quit = 1;
            }
        }
    }

    /* Destroy the video surface */
    SDL_FreeSurface(screen);

    return 0;
}

Now the window can be dragged around on the screen and can be closed by pressing the ‘x’ button at the top. It has become interactive. Now for the walkthrough.

    /* An SDL_Event */
    SDL_Event event;

Here we declare an SDL_Event in order to store the value received by the SDL_PollEvent() function. This variable will be used extensively throughout our game loop.

    /* A bool to check if the program has exited */
    int quit = 0;

A variable to check if the program has been closed by the user. It’s value has been initially set to 0 (i.e false), and will be set to 1 (true) when an SDL_QuitEvent gets sent. This is to ensure that the program only exits when the window is closed down.

    /* While the program is running */
    while (!quit)
    {

This keeps the program looping and handling events until the window is closed by the user. Once quit becomes true the loop ends and the program exits.

        /* Check for new events */
        while(SDL_PollEvent(&event))
        {

This constantly checks for new events and stores them in the event variable we declared earlier. We add handlers for the events inside the loop. We check which event has been sent by the type member I discussed earlier. For a complete list of event types, see here.

            /* If a quit event has been sent */
            if (event.type == SDL_QUIT)
            {
                /* Quit the application */
                quit = 1;
            }

This is the handler for an SDL_QuitEvent. It sets quit to true in order to end the while loop so that the program exits.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s