Originally Published: Tuesday, 26 June 2001 Author: John Hall and Loki Software
Published to: develop_articles/Development Articles Page: 6/6 - [Printable]

Linux Gaming APIs: Chapter 3 of PROGRAMMING LINUX GAMES

Today's Linux.com article, derived from Chapter 3 of the book Programming Linux Games by John Hall, takes a look at the various Linux gaming APIs you can use to construct your game. Don't reinvent the wheel, and don't compromise your open source roots, and check out this excerpt from No Starch Press.

  << Page 6 of 6  

File Handling

Games often need to load images and audio samples from files. This can be a bit of a trick with today's complex file formats and compression techniques. Fortunately, you can usually avoid doing this decoding yourself -- there are Linux-compatible libraries for just about every type of image or sound file you could possibly want to load. Many of these libraries are free software.

libpng and libjpeg

These two libraries allow you to load Portable Network Graphic (.png) and JPEG (.jpg) images, respectively. PNG is an excellent general-purpose image format that compresses images without loss in detail. It is based on a completely open specification, and it is widely supported by image manipulation programs. JPEG is an older, "lossy" image format that does a good job with landscapes and other natural scenes but produces noticeably lousy results with precise images such as line art. JPEG is also an open standard.

If you need to add support for PNG or JPEG images to a game, these libraries are the way to go. It would not be a good idea to try to implement either format yourself unless you have a lot of time on your hands. We'll use these libraries in this book, albeit indirectly: the SDL image library (Chapter 4) links against them to provide seamless PNG and JPEG loading support.

libpng is the official PNG reference library, and it is available at http://www.libpng.org. libjpeg is maintained by the Independent JPEG Group at http://www.ijg.org. These libraries are included in most Linux distributions.

libaudiofile and libsndfile

libaudiofile and libsndfile are libraries for loading audio data from files. Each can read and write a wide assortment of file formats. There is a lot of functional overlap between these two libraries, but they have different interfaces. Libsndfile is probably the more convenient of the two, and we will use it for loading wave files in Chapter 5. libaudiofile has a slightly more arcane (but perhaps more powerful) interface, but it can be a bit annoying to use. libsndfile was designed and written by Erik de Castro Lopo, and it is available under the GNU LGPL license. libaudiofile was originally implemented by Silicon Graphics for its multimedia workstations, but it has since been largely implemented as free software, and it has been officially adopted by the GNOME project.

You can find more information about libsndfile in Chapter 5 or at the library's home page, http://www.zip.com.au/~Eerikd/libsndfile/.

libaudiofile is available at http://www.68k.org/~Emichael/audiofile/, but it is included in most Linux distributions. You'll probably have to download libsndfile yourself. It's worth the trouble.

Ogg Vorbis

Ogg Vorbis is a new audio compression scheme designed to compete with MP3 and the upcoming (stymied) SDMI format. Vorbis is patent-free, and support for it can easily be dropped into an application with the libvorbis library. Although Ogg Vorbis is still under development, the bitstream format is finalized (meaning that future versions of Vorbis will not break compatibility), and, at this writing, it already compresses audio data slighly better than MP3 (with further improvements expected soon). Let's hear a round of applause for the people behind the Ogg project!

The SDL MPEG Library, SMPEG

The SDL MPEG library is a free MPEG-1 video and audio library with a heavy SDL slant. If you want to add MPEG-1 video or MP3 audio playback to your SDL-based game or application, SMPEG is an excellent choice. It may or may not be a viable solution for non-SDL programs, though (since SMPEG outputs directly to SDL surfaces).

MPEG-1 is popular compressed video format based on the discrete cosine transform and motion prediction. It is lossy (that is, it discards video data that it judges to be of less importance), but it generally produces good results, and it is commonly used for game cinematics. MPEG-2 is a newer video codec that produces higher quality results at the expense of a lower compression ratio, but it is encumbered by patents and is therefore not supported by SMPEG. The SMPEG library is available in the Development section of http://www.lokigames.com. Loki Software commercially maintains it for use in its games, but SMPEG is free software.

We will use Ogg Vorbis to implement game music in Chapter 5. The Vorbis library is available for free download online at http://www.vorbis.com.

On To The Code!

Enough groundwork. It's time to throw around some code. In the next chapter we'll talk about the SDL library, a one-stop shop for portable graphics and audio.

We'll also get started on Penguin Warrior, a complete Linux game that we'll develop over several chapters.





  << Page 6 of 6