A Crash Course in SDL
Linux gaming is exploding, partly because of the simple fact that geeks like games, and partly because of recent developments in Linux multimedia. Over the past few years a number of excellent Linux-oriented multimedia toolkits have emerged, such as the GGI graphics interface and the ALSA sound system. The SDL library has also made a bit of a splash recently. SDL is a general-purpose, multimedia-programming library that provides fast and portable access to graphics, sound, input devices, threads and OpenGL rendering. The core SDL library is portable to several flavors of UNIX as well as BeOS, MacOS and Win32. This makes it an excellent choice for developing cross-platform games without compromising performance.
Unlike many multimedia toolkits, SDL does not actually talk to the system's hardware. Instead, it serves as a layer between an application and the underlying system. For instance, SDL's graphics system might use the frame buffer console or X11 under Linux, but DirectDraw under Windows. In either case, SDL's API is unchanged, and the application need not worry about what's going on underneath, and in some cases a carefully written SDL application can be ported to a new platform with a quick recompile.
In this article we'll take a tour of SDL's video API from the ground up. We'll also demonstrate how to collect input from the keyboard. Most of this article has been excerpted from a chapter in the author's upcoming book, on Linux game development (No Starch Press and Loki Entertainment Software, scheduled for early 2001).
SDL is free software (under the LGPL), and it's available for download at their web site (http://www.libsdl.org/). In addition to the actual SDL library, the SDL home page is full of example source code, demos, games and extensions. SDL is easy to install from source, but the SDL home page also provides binaries for several of the more common platforms.
If you've ever worked with Microsoft's DirectX toolkit, you'll notice that SDL is a tiny library in comparison. The source code to the core library weighs in at just under six megabytes, and that includes a lot of extra code that would never be linked into a Linux application. Don't be fooled, though—those six megabytes are well used, and the core SDL library provides almost everything you need to develop high-quality Linux games and media players. In addition, the web site is home to a number of add-on libraries that provide extra features such as image loading and advanced audio mixing. By keeping these features separate from the core library, SDL remains small and easy to learn.
The SDL library consists of several sub-API's, providing cross-platform support for video, audio, input handling, multithreading, OpenGL rendering contexts and other things that game programmers appreciate. Unfortunately, we don't have enough room to cover all of this, so we'll stick to video programming and input handling, the two things you really need in order to get your feet wet with SDL.
The SDL video API's sole purpose is to find a suitable video device and set it up for your application to use. After it has initialized the display (created a window or switched the video card into a particular mode) SDL gets out of your way, providing only a minimal set of functions for pushing blocks of pixels around. SDL is not a drawing toolkit; what you do with the video device after it is initialized is not SDL's business.
SDL uses structures called surfaces (of type SDL_Surface) to represent graphical data. A surface is simply a block of memory for storing a rectangular region of pixels (individual colored dots). Each surface has a width, a height and a specific pixel format (more on this later). SDL loads image files directly into surface structures, and the screen is also a surface (albeit a special one).
The most important property of surfaces is that they can be copied onto each other very quickly; that is, one surface's pixels can be transferred to an identically-sized rectangular area of another surface. This operation is called a blit (block image transfer). Blits are a fundamental part of game programming because they allow complete images to be composed out of pre-drawn graphics (often created by artists with image processing software). Since the screen is a surface like any other, entire images can be sent to the screen with a single blitting operation. SDL provides a generic function for performing fast blits between surfaces, and it can even convert between surfaces of different pixel formats on the fly.
- Smoothwall Express
- Machine Learning Everywhere
- Bash Shell Script: Building a Better March Madness Bracket
- Own Your DNS Data
- Simple Server Hardening
- Understanding OpenStack's Success
- From vs. to + for Microsoft and Linux
- The Weather Outside Is Frightful (Or Is It?)
- Understanding Firewalld in Multi-Zone Configurations
- Ensono M.O.