Originally Published: Monday, 25 June 2001 Author: Josh Boudreau
Published to: develop_articles_tutorials/Development Tutorials Page: 1/4 - [Printable]

Programming Linux Games: An Introduction to SDL Development

Welcome to Game Week on Linux.com! All this week we'll be featuring and playing games. The game week feature article by Linux.com writer Josh "bitwize" Boudreau is a succinct introduction to programming with the SDL (Simple DirectMedia Layer) library, essential to understanding Linux game development. So all you game programmers or would be game programmers: read up!

Introduction   Page 1 of 4  >>

SDL (Simple DirectMedia Layer) is a cross-platform development library providing an API useful to game developers. It gives access to video, audio, timing functions, event handling and threads. SDL supports many platforms; Linux, Windows, MacOS, Solaris, IRIX and FreeBSD are all supported: this means that when your application uses the SDL library functions, they will work when you compile your application on a different supported platform. SDL is written in C and it supports C and C++ out of the box; bindings for Ada, Eifel, ML, Perl, PHP, Perl, Python and Ruby have also been created.

This article provides an introduction to a few of the features of the SDL library and will use the C programming language for the source code examples. I will assume the reader has basic knowledge of the C programming language and knows how to work with the GNU programming tools.

Installing The SDL Library

If your Linux distribution didn't come with SDL you can download the library from www.libsdl.org. SDL's web site has binary packages and also a source code distribution if a package for your distribution is not available. If you downloaded a binary package you can install it with your distribution's package manager tool (for example, RPM on RedHat). I'll give instructions on how to compile and install SDL from source code, in case they didn't provide a package for you. Be sure to download the development version. The runtime version of SDL is only used for running programs that were compiled with SDL. The development version contains the libraries used to compile and link your program but also contains the runtime libraries.

tar zxvf SDL-1.2.1.tar.gz
cd SDL-1.2.1
./configure --prefix=/usr
make install

These commands will build and install SDL in /usr. The library will go in /usr/lib and the C header files will be put in /usr/include/SDL. You can change the install location by passing a different directory to the --prefix argument while running the configure script.

Compiling SDL Applications

SDL programs need to be linked with the SDL library and also pthread since the SDL library has thread support. Each application also needs to include the SDL.h header file. Here's a sample application that we will compile using SDL.

#include <stdio.h>
#include "SDL.h"

int main(int argc, char *argv[])
printf("hello world\n");
return 0;

While this application doesn't really use any SDL function, we still include the SDL.h header file and we will link it with the SDL library as an example. If you installed the SDL library you should have a script called sdl-config. This script is used when compiling applications to tell the build environment where the SDL library and C header are located. By running sdl-config with --libs and --cflags as arguments, you'll see that the script outputs gcc options telling us where the library and headers are and which library we should link to. To compile the example program I listed above you can use the following command.

gcc `sdl-config --cflags --libs` program.c

This will compile and link your applications with the appropriate libraries and give you an executable named a.out in the same directory. Take note that the sdl-config script is not enclosed between quotes, those are backsticks (The same key as the tilde). You should be able to run the application by typing ./a.out.

Introduction   Page 1 of 4  >>