Originally Published: Monday, 25 June 2001 Author: Josh Boudreau
Published to: develop_articles_tutorials/Development Tutorials Page: 4/4 - [Printable]

Programming Linux Games: An Introduction to SDL Development

Welcome to Game Week on Linux.com! All this week we'll be featuring and playing games. The game week feature article by Linux.com writer Josh "bitwize" Boudreau is a succinct introduction to programming with the SDL (Simple DirectMedia Layer) library, essential to understanding Linux game development. So all you game programmers or would be game programmers: read up!

  << Page 4 of 4  

Event Handling

One of the best features of SDL is its ability to handle user input and events. SDL handles events by sending and receiving event messages through a queue. When your application receives a new event it is placed at the end of the queue. One of the data types we will see is the SDL_Event structure. This structure holds information on the type of event and its values.

To read new events from the queue we will be using the SDL_PollEvent() function, which takes the first event from the queue and stores it into a SDL_Event structure. It's also possible to send events with the SDL_PushEvent function, which lets us define custom events which we can send to the event queue. Most application use a switch statement to perform different functions depending on which type of event they have received. Here's an example of event handling using the SDL_PollEvent() function.

SDL_Event event;
SDL_Event tmp_event;

while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
printf("User Hit ESC, quitting...\n");
tmp_event.type = SDL_QUIT;
SDL_PushEvent(&tmp_event); break;

case SDLK_RIGHT:
move_player(right);
break;

case SDLK_LEFT:
move_player(left);
break;

default:
printf("User hit an unhandled key.\n");
break;
}
break;

case SDL_QUIT:
exit(0);
break;

default:
break;
}

In the above example, the code enters a while loop and doesn't return until we have read and processed every event in the queue. When an event is read we do a switch on its type and perform different operations depending on the value. I also showed an example of using SDL_PushEvent() when the user hits the escape key. When the escape key is hit, the program fills in tmp_event with an SDL_QUIT message and then sends it to the queue. When our program reads this SDL_QUIT message it will exit.

Time Synchronization

The timer subsystem of SDL can be used to synchronize your game at a constant speed or frame rate. SDL provides a timer in millisecond resolution. SDL_GetTicks() is used to get the number of milliseconds since the application has started and SDL_Delay() is used to pause for the specified time in milliseconds. Implementing synchronization is quite easy and here's an example showing how it works.

#define INTERVAL 30

unsigned int time_left(void)
{
static unsigned int next_time = 0;
unsigned int now;

now = SDL_GetTicks();
if(next_time <= now)
{
next_time = now+INTERVAL;
return 0;
}
return next_time-now;
}

while(game_is_running)
{
DoGameStuff();
SDL_Delay(time_left());
}

The synchronization is done in a way similar to a countdown timer, where the time_left() function calculates the time it took our program to process the game loop and if there's time left it pauses for that amount of time. If we we're to do only SDL_Delay() with a fixed amount of time the game loop might take longer on some iterations and it would not be synchronized to a constant speed.

Conclusion

This article only scratched the surface of a few features of SDL and if you are serious about developing applications with SDL you should read the API documentation at www.libsdl.org which describes all the functions. To get information on a specific function you can also read the man pages by typing man functionname at your shell prompt.

SDL Web Site: www.libsdl.org





  << Page 4 of 4