Programming with SDL
Total Page:16
File Type:pdf, Size:1020Kb
PROGRAMMING Creating a game with SDL Creating a game Programming with SDL In this, the first in a series of articles, type fonts (TTF). Most of these libraries have been submitted by end users and Steven Goodwin looks at SDL.What it do not form part of the main SDL pack- age. This is not a problem in itself, but if is. How it works. And more impor- your primary purpose is to use SDL for cross-platform development work, be tantly, how to use it to write a brand sure to check the libraries’ availability new game, Explorer Dug. for your particular platform, since not all are fully supported. BY STEVEN GOODWIN The Windows heritage of SDL is very evident within the API, as much of the Figure 1:The opening image terminology (and many of the functions) have very close equivalents in DirectX. DL stands for Simple DirectMedia for Linux, using the SDL project, by com- However, it does not emulate Windows Layer and is a cross-platform API panies that otherwise, are unwilling (or in any way. Instead, every call to the SDL Sfor programming multi-media unable) to release their source code to graphics API, for example, will make applications, such as games. It provides the world. direct use of a driver from the host oper- a stable base on which developers may Some of the most notable releases have ating system, rather from an emulated work, without being concerned with come from the aforementioned Loki system. how the hardware will deal with it – or Games and include Civilization: Call to On Windows, this means DirectX. even what hardware it is running on. Power, Descent 3 and Unreal Tourna- Under Linux, this functionality will Sam Lantinga started the SDL project ment. be provided by one of the graphic driver in 1998 as a way of porting a Windows Good SDL implementations are cur- libraries, such as X11 or DGA. MTRR application to the Macintosh. Soon after rently available for Linux (i386, PPC and (Memory Type Range Register, [2]) beginning the SDL project he went PS2), BSD, Windows, Macintosh OS 9 / control, for accelerated full-screen appli- to work for (the sadly lamented) Loki X, Solaris, IRIX and BeOS. Code for other cations, is also supported. See Box 1: Software, and then to Blizzard Entertain- platforms, such as the Amiga, Atari and Drivers. ment. SymbianOS, also exist, but are not cur- On a similar note, the audio API can Throughout this time he has continued rently supported directly by the core enlist the services of OSS, ESD or aRts to to work on SDL and, in the true spirit of developers. provide music and sound effects. By an open community, has been supported accessing, as opposed to emulating, the (with various platform ports and bug We Built This City system drivers, a high level of perfor- fixes) by a loose knit group of developers As an API, SDL consists of five different mance can be achieved across all across the Internet. subsystems: video, audio, CDROM, joy- platforms. SDL is available under the Lesser GNU stick input and timers. Each one is Public License (LGPL), and as such does targeted at a different area of develop- Box 1: Drivers not require the source code of any appli- ment, and can be initialised and used SDL isn’t limited to X11 as an output driver. It cation using it to be released. independently of the others. In addition, can also use dga,fbcon and even aalib! To Although this might raise political con- SDL promotes the use of libraries that make use of this facility,you must make sure siderations for some people, it has meant can provide new functionality to the that the appropriate driver is compiled into the release of several commercial games basic API. SDL (which may require another ./configure, The two most common libraries are make, make install of the package).You can When builders go down SDL_mixer (which provides better audio then use the environment variable the pub they talk about handling), and SDL_Image which pro- SDL_VIDEODRIVER to indicate which driver football. Presumably you want to use. For example: vides support for a wider selection of therefore, when foot- HOR graphic file formats, including GIF, JPG, export SDL_VIDEODRIVER=aalib T ballers go down the ./explore pub they talk about PNG and TGA. The main SDL website [1] A list of the drivers provided with SDL can be builders! When Steven Goodwin goes lists 76 different libraries currently in found by typing: down the pub he doesn’t talk about production, providing a wide range of THE AU football. Or builders. He talks about ./configure --help | grep U feature sets from basic graphic primi- computers. Constantly… enable-video tives, to networking and support for true 66 August 2003 www.linux-magazine.com Creating a game with SDL PROGRAMMING Do The Digs Dug tar xfz SDL-1.2.5.tar.gz will search. All other SDL header files SDL has many uses, and has bestowed are incorporated inside this one, to limit life upon emulators, music editors, DivX and installed with the equally familiar: maintenance. We will also need to link players and video processing tools. How- the main SDL library (libSDL) into our ever, it is within the field of games that ./configure application. Initially we shall only be SDL has risen above its peers. There are make using functionality from this library. hundreds of SDL games available on the make install We’ll add other libraries as time goes on, Internet and most of these are available and our requirements grow. Until then, with full source code. We shall be adding By default, this will place the required both of the above demands can be satis- to this collection with a small platform library and include files into /usr/local/ fied on the command line with: game called Explorer Dug. It will be pro- lib and /usr/local/include/SDL respec- grammed in ‘C’ (as it is probably the tively, although this can be changed by gcc -I/usr/include/SDL -lSDL U most widely understood), although SDL starting the process with: test/testsprite.c itself can be written in many others, such as PHP, Ruby, Perl, Java and LISP. ./configure -prefix=/put/sdl/U If you change (or forget!) the location of We shall primarily focus on the features somewhere/else your include and library paths, you can of SDL and how to use them to produce use the sdl-config program to provide a game. We shall also highlight the areas There are a wealth of other configuration this information (see Box 2: SDLCON- of game development that require more options available here which allow you FIG). work from us, the programmer. OK? to enable (or disable) various drivers Good! Let’s start… (such as ALSA), or include framebuffer All Around the World support. Apart from providing additional Our first foray into SDL programming Thunderball drivers, you should have little cause to will be the games “Welcome” screen. The first task is to download and install re-./configure since SDL is very stable This introduces a number of fundamen- SDL. The current stable version is 1.2.5, and easy to setup, and invariably works tal concepts, such as surfaces, blitting and is available from [3] as a gzipped out of the box. and screen updates, that will reoccur tarball. In addition to the source code The SDL package also contains a com- throughout any SDL application you distribution there are two different prehensive set of man pages (section 3) write. binary versions available: runtime and detailing the parameters and available As you read the code, you will notice development. Naturally, the latter pro- flags for each function. that every SDL function and structure vides better debugging facilities which is begins with the SDL_ prefix. In addition, more useful for us, as developers. How- Build that wall we shall wrap these functions up inside ever, I recommend working from source The SDL package comes with a small our own (all prefixed with ‘ex’) to encap- as this allows us better customisation, test suite that checks the integrity of sulate more functionality and provide a and full use of a debugger to step into each subsystem. These are held in the common place where game code and the SDL code itself. This is not only help- test directory, and provided the install SDL code meet. This will help in debug- ful in tracking down bugs, but can also succeeded, should ‘just work’. In cases ging. be very illuminating – especially some of where things do not work as expected, The first stage is to initialise SDL. the comments! look for the error messages each test out- There are two functions to do this, The tar.gz is unpacked in the usual puts to the command line as this can tell SDL_Init and SDL_InitSubSystem. way: you if there’s a problem with your hard- SDL_Init can initialise one (or more) ware, or its setup. Fortunately, if your subsystems and should always be the Box 2: SDLCONFIG machine is capable of running a desktop, first SDL function in your program. sdl-config is a small program that comes it should be up to spec for anything Although you can initialise additional bundled with SDL to report the base,or pre- you’ll be doing with SDL! subsystems later you must always start fix,location of your SDL installation. It can All SDL programs can start with either with an SDL_Init, because this contains also be used to give you the correct flags for the line: other initialisation components that both the compiler and linker.