oreilly.comSafari Books Online.Conferences.


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

Populate the file with the following "code" (which is basically all comments, but that's why we get to call it a "plan"), and save it as sdl-pong.c:

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

If you do not program in C, you may want to look at a quick overview of the language--the University of Leicester's Computer Centre has a decent-looking C tutorial. I will try to explain the tricky bits as I go.

What we have written is an English-language description of how we plan to implement our game. But, as you may have noticed, we have also written a C program. One that does nothing useful, of course, but a C program nonetheless. To prove this (and demonstrate how to compile an SDL program at the same time), let's fire up gcc:

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

If you aren't familiar with gcc, a bit of explanation may be in order. The -g flag builds the program with symbols intact, which means that you will be able to use a debugger (such as the fabulous GNU Debugger, gdb) to fix any introduced code problems. The -Wall flag turns on all warnings; a good thing to do, as compiler warnings mean that the compiler cannot quite understand what you mean. The -I/usr/include/SDL bit tells gcc to add /usr/include/SDL to the list of directories that it searches for included files (that is, header files).

Note: It is possible that your Linux distribution or flavor of Unix decided to install the SDL header files somewhere other than /usr/include/SDL; you will know this in a hurry if gcc spews an error message like "SDL.h: No such file or directory" when you try to compile. If you know how, try to entice your package system to show you where it installed all of the files for the SDL package (Gentoo users, run qpkg -I -l libsdl; RPM types, try rpm -ql sdl). Failing this, you can always try find / -name SDL.h 2>/dev/null on a Unix system, or use the file finder tool in the Windows Start menu. However you find the SDL.h file, stick the directory in which it resides after a -I on the gcc command line and all will be well.

The -o sdl-pong lets gcc know that we want the compiled program to be named sdl-pong; leave the -o off the command line and your program is likely to be called a.out, which is not such a catchy or memorable name for a game. Unsurprisingly, sdl-pong.c is the name of the file containing the source code that we want to compile. Finally, the -lSDL flag tells gcc to link the shared library in with your program. Without the -lSDL flag, gcc would spew errors like: "undefined reference to `SDL_Init'."

Don't worry if you did not understand the last paragraph, you can just treat the gcc lines as mysterious incantations that result in you being able to play the game that you wrote.

And speaking of playing the game--to run your freshly compiled program, type:


The program will start up--and just hang. Right, that is what infinite loops tend to do: remember the while (1) { ... } block in the middle of the code, labeled "Main loop?" Don't worry, you can get your terminal back by hitting Ctrl-C, and you will put some code in that main loop soon.

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: