oreilly.comSafari Books Online.Conferences.


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

Initializing SDL and Doing Something Useful at Long Last!

The first order of business for any SDL application is initializing SDL itself. To do this, simply add to your sdl-pong.c source code, under the "Initialise SDL" comment:

  if (SDL_Init( SDL_INIT_VIDEO ) != 0) {

    fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() );
    return 1;

  } // if (could not init video)

SDL_Init() is doing all the work--the other lines are just checking the return value and handling an error condition by returning from the main function, which causes the program to exit. Since this is Unix, a return value of 0 means no error; when you see a non-zero value returned, start worrying. Upon exit, sdl_pong returns 1, a non-zero value, to let the executing shell know that our program is exiting in response to an error.

Once you have initialized SDL, you need to make sure that you tear it down cleanly before exiting the program for any reason. For this purpose, define a new function:

// Function definitions
int cleanUp( int err );

The implementation of the function is simple: all you need to do is call the SDL_Quit() function with no parameters, but the cleanUp() function takes and returns an integer so that you can make sure the exit code is reported to whoever executed the program. Add the cleanUp() function to the end of your file, after main():

/* 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()

Note that we kindly write a header comment for this function; this is so that other people who might want to modify our program know what the function does and how to call it. It is also useful when you forget how a function works because you wrote it a long time ago (and in my case, "a long time ago" means more than an hour ago). The specific style of this header comment can be parsed by a program called NaturalDocs to generate lovely HTML versions of your API documentation.

Now that you have the cleanUp() function, you will want to modify the last line of the main() function by changing:

  # Victory!
  return 0;

to read:

  # Victory!
  return cleanUp( 0 );

This ensures that SDL releases all of the memory and hardware resources that it grabbed on the program's behalf before exiting.

The next step is to display the main window in which all of the action will take place. Let's make it 640 pixels wide and 480 pixels tall for now, but we'll define the screen width and height as macros so that we can change them later. Add this to your sdl-pong.c source code:

// Macro definitions
#define SCREEN_WIDTH  640 // width of the screen, in pixels
#define SCREEN_HEIGHT 480 // height of the screen, in pixels

The main window will be represented by a variable of the SDL_Surface type. Add this to the "Declare variables" section of the code:

  SDL_Surface *screen; // main game window

To display the window, call the SDL_SetVideoMode() function (add the following to the section of the code initializing SDL):

  if ((screen =
      == NULL) {

    fprintf( stderr, "Could not set SDL video mode: %s\n", SDL_GetError() );
    return cleanUp( 1 );
  } // if (could not set mode)

The first two parameters to SDL_SetVideoMode() are the width and height of the window, respectively. The third parameter is bits per pixel--the number of bits SDL will use to represent the color of every pixel on the screen. Obviously, the higher the number, the wider the variety of colors that can be displayed. SDL supports eight, 15, 16, 24, and 32 bits per pixel. We can get away with a paltry eight bits (one byte) per pixel because this Pong clone is going to be monochromatic. Don't worry, you may play Ted Turner and colorize the game later if you choose (details on this and other improvements to the game will come in the next two articles in this series). The final parameter is for SDL flags: we only need SDL_SWSURFACE, but for a list of all of the flags that can be set, check out the entry for SDL_SetVideoMode() in the SDL library documentation. Note that these flags are bitmasks, so to set multiple flags, you will need to OR them together, like so: SDL_SWSURFACE | SDL_FULLSCREEN.

Note again to check the return value of the function for errors. In this case, since SDL_SetVideoMode() returns a pointer (if you do not know what a pointer is, don't worry about it just yet, but be warned that the concept of pointers will haunt your waking hours if you ever decide to use C for anything complicated), a value of zero (NULL is simply a #define 0 in some C standard library header file) signals an error, and anything else is OK. To recap, a return value of 0 in Unix always means everything is fine, except when it doesn't.

Pages: 1, 2, 3, 4

Next Pagearrow

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: