Linux DevCenter    
 Published on Linux DevCenter (
 See this if you're having trouble printing code examples


by Josh Glover, contributor to Retro Gaming Hacks

One of the great things about the games of yore is that they tended to be pretty simple, mostly because the hardware that pushed them only could do approximately 1.5 floating-point operations per day (FLOPD--an unfortunate acronym if ever I've seen one). Of course, one of the benefits of such spartan software is that in an actual game, you know, something "fun," has to exist at the kernel. When you don't have full-motion, cinema-quality cutscenes to distract the player, you had better serve up some serious gameplay. And the people working for Atari and Broderbund knew how to cook up games, including Atari's smash-hit video game Pong--one of the greatest games ever invented, and one of the simplest to implement.

So gather up your C programming skills, your dusty old eighth grade geometry book, and a copy of the Simple DirectMedia Layer (SDL)--we're going to show you how to clone Pong all by yourself. And stay tuned--this is the first of a three-part series. The next two articles (Adding Paddles to Your Pong and Add a Ball and Score to Your Pong) will build on what we do here, so look for those in the coming weeks.


As mentioned in the previous paragraph, SDL is the Simple DirectMedia Layer. If you are wondering how this has any relevance on the cloning of Pong, allow me to explain. In the heady, dot-com days of 1998, a company named Loki Entertainment Software was founded, with the mission of porting best-selling games to Linux. Their first port was of Civilization: Call to Power, which hit stores in April of 1999. Loki ported 19 other great games before they closed up shop at the end of January 2003.

As great as Loki's ports were, the crown jewel of its collection was not a game, but a game library: the Simple DirectMedia Layer. Loki, an open source company, released SDL under the GNU Lesser/Library Public License (LGPL), allowing anyone to download and use SDL to create his or her own games. SDL is written in C (and you can use it natively in C++ code), but bindings exist for Ada, Eiffel, Java, Lua, ML, Perl, PHP, Pike, Python, and Ruby. SDL does a great job of abstracting away the nasty details of game writing and letting you concentrate on the fun stuff, like writing your very own Pong clone! To install it, follow the directions for your Linux distribution:

Debian GNU/Linux

  1. Become root.
  2. Run the command:
  3. apt-get install libsdl1.2 libsdl-ttf1.2 libsdl1.2-dev \ 


  1. Become root.
  2. Run the command:
  3. emerge libsdl sdl-ttf

Red Hat Linux, Fedora, and Other RPM-Based Distributions

  1. Visit the SDL project's download page and save the RPMs for the latest Runtime and Development Libraries (Version 1.2.8-1, as of this writing) to your /tmp directory.
  2. Open a terminal window and become root, then run this command:
  3. rpm -Ivh /tmp/SDL-*.rpm

Other Linux/Unix Distributions

  1. Visit the SDL project's download page and save the latest source tarball (SDL-1.2.8.tar.gz, as of this writing) to your /tmp directory.
  2. Open a terminal window and become root, then run these commands:
  3. cd /tmp
    tar xvzf SDL-1.2.8.tar.gz && rm SDL-1.2.8.tar.gz
    cd SDL-1.2.8
    ./configure && make && make install

Other Operating Systems

Users of Windows, BeOS, and Mac OS (both Classic and X) can also visit the SDL project's download page, grab the appropriate distribution files for the latest Runtime and Development Libraries, and install them. You may find the "How to set up your SDL Build Environment" article on the Game Programming Wiki to be quite useful.

Note: Windows users can choose between Visual C++ and MingW32 versions of SDL. MingW32 is a free minimal Windows development environment that you can get using the Cygwin installer. If you choose the Cygwin route, it will closely resemble the Linux/Unix/Mac OS X-friendly instructions in the rest of this hack.

With SDL now installed, let's get on with the hacking of code.

A Rough Outline

As with any engineering project, a video game should start with a plan. We'll write our plan in C. Open up your favorite editor and create a new file. I recommend calling it sdl-pong.c.

Note: This hack assumes you are coding in a Unix, Linux, or Unix-like environment (Mac OS X, Cygwin, and so on). If you are not, you should know how to work your compiler, as when I say "gcc this, gcc that," you will have to translate to "File > Compile," and so on. For a pure hacking experience, install the GNU Compiler Collection (gcc) on your machine, and write your coding in a (programmer's) text editor, such as XEmacs or vi.

Retro Gaming Hacks

Related Reading

Retro Gaming Hacks
Tips & Tools for Playing the Classics
By Chris Kohler


// Standard library headers
#include <stdio.h>

// SDL headers
#include <SDL.h>

// Macro definitions

// Structure definitions

// Function definitions

// Main program
int main( int argc, char **argv ) {

  // Declare variables

  // Initialise game data
  // Parse command-line arguments

  // Initialise SDL
  // Initialise sprite locations

  // Main loop
  while (1) {

    // (Re)draw the screen

    // Handle events (keyboard input, mainly)

    // Move sprites about
    // Give the CPU a break
  } // while (main loop)

  // Victory!
  return 0;

} // main()











Initializing SDL and Doing Something Useful at Long Last!


  if (SDL_Init( SDL_INIT_VIDEO ) != 0) {

    return 1;

  } // if (could not init video)



// Function definitions
int cleanUp( int err );


/* Function: cleanUp()
 * Cleanly tears down SDL in preparation for an exit.
 * Parameters:
 *   err - Unix-style error code
 * Returns:
 *   err parameter

int cleanUp( int err ) {


  return err;

} // cleanUp()



  # Victory!
  return 0;

to read:

  # Victory!
  return cleanUp( 0 );



// Macro definitions


  SDL_Surface *screen; // main game window


  if ((screen =
      == NULL) {

    return cleanUp( 1 );
  } // if (could not set mode)





  // Set window caption and turn off the mouse cursor
  SDL_ShowCursor( SDL_DISABLE );


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


  // 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




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




and then run the game:




Return to the Linux DevCenter.

Copyright © 2009 O'Reilly Media, Inc.