Skip to content


Subversion checkout URL

You can clone with
Download ZIP
tree: 4c3c6cfe3d
Fetching contributors…

Cannot retrieve contributors at this time

240 lines (150 sloc) 6.833 kb


Simple DirectMedia Layer (a.k.a. libsdl) is a cross-platform C library that provides access to several input and output devices. Most popularly it is used for its access to the 2D video framebuffer and inputs for games.

In addition to the core library there are several other libraries that provide useful features such as Text, Mixers, Images, and GFX.

SDL Perl binds several of these libraries together in the SDL::* namespace. Moreover, SDL Perl provides several high-level libraries in the SDLx::* namespace that encapsulate valuable game-writing abstractions.

The SDLx:: layer

The main purpose of the SDLx::* layer is to smooth out the drudgery of using the SDL::* layer directly. For example, drawing a rectangle involves the following work.


Don't worry about understanding the code at this moment. Just compare the two code listings for displaying the same blue rectangle below.

Using the SDL::* layer to draw a blue rectangle looks something like this:

while drawing a blue rectangle in the SDLx::* layer is as simple as:

A secondary purpose of the SDLx::* modules are to manage additional features for users, such as Layers, Game Loop handling, and more.


This book is written for new users of SDL Perl who have some experience with Perl, but not much experience with SDL. It is not necessary for the audience to be aware of SDL internals, as this book covers most areas as it goes.

Format of this book

This book will be formatted into chapters that progressively increase in complexity. However each chapter can also be treated individually as a separate tutorial to jump to and learn from.

Each chapter will have a specific goal (e.g., Making Pong), which we will work towards. The source code for each chapter will be broken up and explained in some detail. Sources and data files are all provided on HTTP://SDL.Perl.Org.

Finally chapters will end with an exercise for the reader to try out.

Purpose of this book

This book is intended to introduce game development to Perl programmers and at the same time introduce Modern Perl concepts through game development. The book presents a progression from simple to intermediate examples and provides suggestions for more advanced endeavors.

Installing SDL Perl

We assume that a recent perl language and supporting packages have been installed on your system. Depending on your platform you may need some dependencies. Then we can do a final CPAN install.


Alien::SDL will install binaries for 32bit and 64bit so there is no need to compile anything.



Fink has packages for SDL Perl available, however Pango is not currently supported.

Or Compiling Dependencies

Alien::SDL will compile SDL dependencies from scratch with no problems as long as some prerequisites are installed. libfreetype6, libX11, libvorbis, libogg, and libpng headers will suffice for most examples in this book.


Most current GNU/Linux distributions include all the parts needed for this tutorial in the default install and in their package management system. It is also always possible to install on GNU/Linux using the available open source code from the proper repositories. The Alien::SDL perl module automates much of downloading, compiling, and installing the needed libraries.


You can probably use your distribution's packages. On Ubuntu and Debian try:

    sudo apt-get install libsdl-net1.2-dev libsdl-mixer1.2-dev \
    libsdl1.2-dev libsdl-image1.2-dev libsdl-ttf2.0-dev \
    libsdl-gfx1.2-dev libsdl-pango-dev

Or Compiling Dependencies

To compile from scratch, a compiler, system header packages, and some libraries are required.

    sudo apt-get install build-essential xorg-dev libx11-dev libxv-dev \
    libpango1.0-dev libfreetype6-dev libvorbis-dev libpng12-dev \

CPAN install

Before installing SDL you should make sure that some important modules are up-to-date.

    sudo cpan CPAN
    sudo cpan YAML Module::Build

After these two steps cpan will be able to find all depedencies for SDL.

    sudo cpan SDL

For most platforms a CPAN install will suffice. Supported and tested platforms are listed at HTTP://Pass.CPANTesters.Org/distro/S/SDL.html.


Hopefully this book answers most of your questions. If you find you need additional assistance, please contact us by one of the following methods:


SDL Perl's homepage is at HTTP://SDL.Perl.Org.


The channel #sdl on IRC.Perl.Org is very active and a great resource for help and getting involved.

Mailing lists

If you need help with SDL Perl, send an to sdl-devel@Perl.Org.


The code examples in this book are provided at:



Thanks to contributors and reviewers from the #sdl channel, including:

and many more
    (Apologies if I have missed you; let me know and I will add you.)


Hey! The above document had some coding errors, which are explained below:

Around line 1:

Unknown directive: =head0

Around line 29:

=end sidebar without matching =begin. (Stack: [empty])

Around line 102:

Deleting unknown formatting code U<>

Around line 180:

Deleting unknown formatting code U<>

Around line 190:

Deleting unknown formatting code U<>

Around line 205:

Deleting unknown formatting code U<>

Jump to Line
Something went wrong with that request. Please try again.