Now: Tutorial for Web and Software Design > OS > Linux > OS Content
> Retro Gaming Hacks, Part 1: Clone Pong, Using Only SDL (and Your Brain) [Bookmark it]
Retro Gaming Hacks, Part 1: Clone Pong, Using Only SDL (and Your Brain)

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

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 libSDL.so 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:

./sdl-pong

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.

Prev  [1] [2] [3] [4] Next

[Bookmark][Print] [Close][To Top]
  • Prev Article-OS:

  • Next Article-OS:
  • Related Materias
    How to Deploy Software Usi
    Mastering Windows New Fire
    Creating Visual Studio Pro
    Better Registry Searching
    Using Data Compression in 
    How to Remove Startup Prog
    Registry Hacks for Servers
    Build a Virtual Routed Net
    How To Recover from Regist
    The Ultimate Free Windows 
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial