January 27, 2021
Hot Topics:

Simple DirectMedia Layer: Gaming Platform for C++, Java, and More

  • By Victor Volkman
  • Send Email »
  • More Articles »

Gentlemen, Start Your 3D Gaming Engines!

In gaming, more so than any other programming regime, it is important to specify the platform correctly from the start. Will you support Windows, Linux, and OS X? Indeed, many people would cite the revolutionary Quake game written for OpenGL in 1996 as the granddaddy of game platforms. However, to reach Quake-level gameplay standards you will also need world-class audio support, network connectivity, user-input device support, and real-time management capabilities, just to name a few… The solution to both problems, cross-platform support and the extras that make a game exciting, is Simple DirectMedia Layer (SDL). This is a cross-platform multimedia library designed to provide low-level access to audio, keyboard, mouse, joystick, OpenGL, and 2D video framebuffer. SDML supports almost every platform I can think of, including Linux, Windows, all MacOs variants, WinCE, Dreamcast, and others. Because it is distributed on GNU LGPL v2, you can use SDL freely in commercial programs as long as you link with the supplied DLL.

SDML shows up in MPEG players, hardware emulators, and many popular games, including the award winning Linux port of Civilization: Call To Power. SDML is written in C, but works with C++ natively, and has bindings to several other languages, including Ada, Eiffel, Java, Lua, ML, Perl, PHP, Pike, Python, and Ruby. The sky is the limit with SDL, which is the engine for my favorite Open Source flight simulator GL-117. In fact, there are now 568 games built on top of the SDL engine (of which 450 can build Win32 executables) registered on the SDML homepage.

Click here for a larger image.

Figure 1: The view from GL-117

ALIENS in Your Midst!

The best way to get inside a game engine is to look at some sample code. This time, you're going inside a 1980s Space Invaders clone game by Sam Lantinga, called simply aliens. Remarkably, the complete game source code is only 560 lines of code! I wish I could lay out the entire thing in this article, but it exceeds my boundaries just a little. Instead, I will focus with laser-like intensity on the specifics that matter the most and ignore bits which are rote or repetitious. [I'll be using line numbers from aliens.c if you want to follow along in Visual Studio.]

Figure 2: Screenshot of Aliens game

The first thing to notice is that "aliens" leverages code from two other SDL projects to help get the job done: SDL_mixer and SDL_image. SDL_mixer is a platform-independent sound mixing library plug-in for SDL apps. It allows your apps to play multiple samples along with music without having to code a mixing algorithm. For example, you would use this to allow gunfire noises to seamlessly mix into background music. It also simplifies loading and playing samples and music from all sorts of file formats. SDL_image is a platform-independent graphics loading plug-in for SDL apps. It allows your apps to read BMP, PNM (PPM/PGM/PBM), XPM, LBM, PCX, GIF, JPEG, PNG, TGA, and TIFF format files. So, be sure to download both packages if you want to follow along!

The main() Business

The main() function, shown below, is where the action starts, of course. In Line #524, you ask SDL_Init() to initialize the audio and video subsystems. Other options include turning on joystick, timer, and CD-ROM access as well as an option to run the event manager in a separate thread. In Line #531, you start using the SDL_Mixer manager with a call to Mix_OpenAudio(). In this example, you're using low-quality audio suitable for older/slower CPUs: 11Khz, 8-bit unsigned samples, one channel (mono), and a 512 byte buffer. Next, you open up the video with SDL_SetVideoMode() for 640x480 display using whatever bits per pixel is appropriate for the device. You ask for a system-memory buffer and request a "full screen" display, meaning that you want your window to dominate the windowing system. Other possible options include double-buffering (smoother animation), OpenGL context, resizable window, and asynchronous repaints.

521 main(int argc, char *argv[])
522 {
523    /* Initialize the SDL library */
524    if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
525       fprintf(stderr, "Couldn't initialize SDL: %s\n",
526       exit(2);
527    }
528    atexit(SDL_Quit);
530    /* Open the audio device */
531    if ( Mix_OpenAudio(11025, AUDIO_U8, 1, 512) < 0 ) {
532       fprintf(stderr,
533       "Warning: Couldn't set 11025 Hz 8-bit audio\n-
           Reason: %s\n",
534       SDL_GetError());
535    }
537    /* Open the display device */
538    screen = SDL_SetVideoMode(640, 480, 0,
539    if ( screen == NULL ) {
540       fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
541       SDL_GetError());
542       exit(2);
543    }
545    /* Initialize the random number generator */
546    srand(time(NULL));
548    /* Load the music and artwork */
549    if ( LoadData() ) {
550       /* Run the game */
551       RunGame();
553       /* Free the music and artwork */
554       FreeData();
555    }
557    /* Quit */
558    Mix_CloseAudio();
559    exit(0);
560 }

Page 1 of 3

This article was originally published on April 3, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date