oreilly.comSafari Books Online.Conferences.


Retro Gaming Hacks, Part 1: Clone Pong, Using Only SDL (and Your Brain)
Pages: 1, 2, 3, 4

Many tutorials tend to leave error checking code out, as it clutters the presentation of the code. I left the error checking in, because many headaches can be avoided by knowing what went wrong where.

The code we have written thus far will display a game window, then loop forever (or until the mailed fist of SIGINT comes crashing down, prompted by a judicious Ctrl-C). This is progress, but it is still pretty boring from the perspective of a potential player (my perspective is that of a potential poet, as you can see from my abuse of alliteration). Let's add some window dressing (no pun intended, until I thought about it and realized that it was an extremely clever pun) in the form of a title bar caption and hiding the mouse cursor when over our window. Add this just beneath the SDL_SetVideoMode() bit:

  // Set window caption and turn off the mouse cursor
  SDL_WM_SetCaption( "SDL Pong", "SDL Pong" );
  SDL_ShowCursor( SDL_DISABLE );

Finally, let's provide users of the game with a way to exit (though why they might want such a facility is beyond me). We will accomplish this by adding an event handling loop inside the main loop. As most games are event-driven (a controller movement is an event; a packet arriving from the network may trigger an event; an external windowing system action may trigger an event; and so on), SDL will happily collect events on your behalf and queue them up, waiting for you to deal with them. To do just that, call SDL_PollEvent(), which grabs the first event SDL has for us. But first, we will need a variable of the SDL_Event type to pass as the sole parameter to SDL_PollEvent(). Right before the declaration of the SDL_Surface variable, add the line shown in bold:

  // Declare variables
  SDL_Event    event;  // SDL events
  SDL_Surface *screen; // main game window

and inside the main loop, add the lines shown in bold:

  // Main loop
  while (1) {

    // (Re)draw the screen

    // Handle events (keyboard input, mainly)
    while (SDL_PollEvent( &event )) {

      if (event.type == SDL_QUIT ||
          (event.type == SDL_KEYDOWN &&
           event.key.keysym.sym == SDLK_ESCAPE))
        return cleanUp( 0 );

    } // while (handling events)

    // Move sprites about

This handles two events, namely SDL_QUIT, which is generated when the user quits the program externally (usually by simply clicking on the X in the main window's title bar), and the SDL_KEYDOWN event, which is generated every time a key on the keyboard is pressed (in case you were wondering, yes, an SDL_KEYUP event occurs when said key, or any key for that matter, is released). We only care about the Esc key, of course, which is the de facto standard for quitting SDL games. That is what the event.key.keysym.sym == SDLK_ESCAPE code is all about; for a complete list of key symbolic names (keysyms), check out the SDLKey page on the SDL library documentation site.

Once the program catches an event that tells it to quit, it does so simply by returning the return value of cleanUp() with an error code of 0 to indicate a normal exit. Get used to this idiom, as you will see it again when we get to parsing command-line arguments in the next two articles.

The last thing to be done before compiling and testing this iteration of SDL Pong is adding a small delay at the end of the main loop. For now, this is just to be kind to the CPU and let some other task run, but a little while later, it will control the speed of our game. To wait for a bit, we use the SDL_Delay() function, which takes as its only parameter the number of milliseconds to wait. Add this code right before the end of the main loop:

    // Give the CPU a break
    SDL_Delay( 30 );

Thirty milliseconds (0.03 seconds) may not seem like long to you, but to a modern CPU, it is time enough to read the paper and walk the dog.

OK, that's it! Your "game" now at least does something that is perceivable to the naked eye of a non-coder (such as your grandmother--unless she happens to be, say, Admiral Grace Hopper, in which case she could code you under the table any day!). Run gcc again to re-compile it (don't forget this step after every change to your code, or you might spend a lot of time wondering why your latest bug fix did not work):

gcc -g -Wall -I/usr/include/SDL -o sdl-pong sdl-pong.c -lSDL

and then run the game:


Editor's note: Next week, Josh will cover how to add paddles to your Pong game, and in the first week of the new year, he'll cover how to add a ball and score.

Josh Glover has been hacking code for as long as anyone can remember. He is employed as a Unix systems administrator by

Return to the Linux DevCenter.

Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!

Linux Resources
  • Linux Online
  • The Linux FAQ
  • Linux Kernel Archives
  • Kernel Traffic

  • Sponsored by: