Cross-Platform Game Development for C++ Devs

Do you dream of writing the next hit game title but aren't sure how to get started? Are you interested in game development just for the fun of it? Take a close look at a versatile cross-platform gaming engine that's freely available for the beginning game developer.

A (Very) Brief History of 3D Gaming Engines

In gaming, more so than any other programming discipline, it is important to specify the platform correctly from the start. Will you support Windows, Linux, and OS X? Isn't using OpenGL enough to get you there? OpenGL was designed in the early 1990's for $25,000 Unix CAD workstations and later adapted to Windows and low-end platforms as the gaming industry drove the cost of graphics accelerators down from $2,000 a pop to the $150 mass-market price point you see today.

Indeed, many people would cite the revolutionary game Quake, written for OpenGL in 1996, as the direct cause of this. However, achieving Quake-level gameplay standards required more: world-class audio support, network connectivity, user-input device support, and real-time management capabilities—just to name a few. The solution for both requirements, cross-platform support and the extras that make a game exciting, is a decent game development platform.

Simple DirectMedia Layer for C++, Java, and More

Well, that's all very interesting history, but it doesn't really address the question of where fragging coders should start: Not every game is going to be a Quake clone. One option that has been touted for its many virtues is Simple DirectMedia Layer (SDML). This cross-platform multimedia library provides low-level access to audio, keyboard, mouse, joystick, OpenGL, and 2D video framebuffer. SDML supports most every platform I can think of, including Linux, Windows, all MacOS variants, WinCE, Dreamcast, and others. It 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 SDML, which happens to be the engine for my favorite open source flight simulator, GL-117 (see Figure 1). In fact, 513 games currently are built on top of the SDML engine and registered on the SDML homepage.

Figure 1. The View from GL-117

Tunnel Vision Demo Program

The best way to get inside a game engine is to look at some sample code. Take a brief look at a 2-D tunnel-type display in SDML (see Figure 2) to see what you can do in just a few lines of code. This example might be something you use for a screen-saver, music visualization, and so forth. I've trimmed the actual drawing code for brevity. Follow my comments for a description of how SDML works:

#include "Tunnel.h"

// SDL Stuff
SDL_Surface     *screen;
SDL_Surface     *bBuffer;
SDL_Surface     *Image;
SDL_Rect        rScreen;
SDL_Rect        rBuffer;

// --------------------------------------------------------------

int main (int argc, char **argv)

  int flag = SDL_SWSURFACE;    // Requests a software surface.
                               // Software surfaces are in
                               // system memory, and are not
                               // generally as fast as hardware
                               // surfaces
#ifdef WIN32
  int fullscreen = MessageBox(NULL, "Show Full Screen (y/n):",
                              "Screen Setting", MB_YESNO);

  if (fullscreen==IDYES)     {
      flag |= SDL_FULLSCREEN;    // Take over whole screen, if
                                 // user desires

  Tunnel_Timer();    // Read initial system clock

  SDL_Init( SDL_INIT_VIDEO );    // Initialize just the video
                                 // subsystem
 // Set screen to 320x240 with 32-bit color
 screen = SDL_SetVideoMode( 320, 240, 32, flag);

// Request hardware buffers for the screen surface, if available
  bBuffer = SDL_CreateRGBSurface( SDL_HWSURFACE, screen->w,

// This is the seed image that you will convolute when you get going
  Image = SDL_LoadBMP( "tunnel_map.bmp" );

  Image = SDL_ConvertSurface(Image, screen->format, SDL_HWSURFACE);

  rBuffer.x = 0;
  rBuffer.y = 0;
  rBuffer.w = bBuffer->w;
  rBuffer.h = bBuffer->h;

  // Ignore most events, including mouse, and disable the cursor
  SDL_ShowCursor( SDL_DISABLE );

  Tunnel.Set( 320, 240 );    // Tunnel will fill the whole buffer
  Tunnel.Precalc( 16 );      // Inner circle diameter

  while (SDL_PollEvent(NULL)==0)    {
      float fTime = Tunnel_GetTime();

      // Surfaces must be locked before modification, especially
      // if the buffer resides in the graphics hardware memory

      Tunnel.Draw(bBuffer, Image, 180*sin(fTime), fTime*100);

      SDL_UnlockSurface(bBuffer);    // After updating, you may
                                     // unlock

      // Push the buffer out to the screen draw area and force
      // a repaint
      SDL_BlitSurface( bBuffer, NULL, screen, &rBuffer );
      SDL_UpdateRect( screen, 0, 0, 0, 0 );


Figure 2. Spinning and Twisting 2D Tunnel Demo

Cross-Platform Game Development for C++ Devs

Some Other Game Engines to Explore

Take a whirlwind tour of some other open source gaming engines.

ALLEGRO (Allegro Low LEvel Game ROutines)

Allegro is an open source portable library mainly aimed at videogame and multimedia programming. Allegro was created by Shawn Hargreaves (lately of Climax) and has grown to be a capable cross-platform gaming system for Linux, Windows, MacOS, MS-DOS, and a half-dozen other platforms.

In addition to a super 2D graphics library, it also offers easy access to the mouse, keyboard, joystick, and high-resolution timer interrupts. Allegro doesn't encapsulate or replace OpenGL, but you can learn how to integrate OpenGL in Allegro apps by visiting their extensive developer's site.

There are about 700 different game projects published with Allegro, the top two categories of which are Arcade and Puzzles. I particularly liked the remake of the classic Zaxxon arcade game (see Figure 3).

Figure 3. Cool Remake of Zaxxon

Irrlicht: Lightning Fast Real-Time 3D Engine

The Irrlicht Engine is a cross-platform, high-performance real-time engine written in C++. You can choose Direct3D, OpenGL, or software-based rendering. High-end features include dynamic shadows, particle systems, character animation, indoor and outdoor technology, and collision detection (see Figure 4). Irrlicht supports Windows and Linux and offers language bindings for Java, Perl, Ruby, and others. Gaming pro Nikolaus Gebhardt does it all with a little help from his friends.

Figure 4. A Breathtaking Scene in Irrlicht

ClanLib: Designed for Multiplayer Gaming

ClanLib delivers a platform-independent interface to write games that have a common interface to low-level libraries such as DirectX and OpenGL. You can target ClanLib apps for Windows, Linux, and OS X with a single set of sources. ClanLib includes an extensive sound library, 2D collision detection, animation, GUI framework, and network libraries. Figure 5 shows a scene from the game XenoHammer.

Figure 5. XenoHammer Screenshot

Book of the Month: Dewhurst Does it Again

With all the programming-for-idiots and teach-yourself-in -"X"-days book series these days, it's refreshing to see something for the long-suffering intermediate C++ programmer. Stephen C. Dewhurst's C++ Common Knowledge comes to the rescue with 63 easily digestible items that satisfy your hunger for doing things the right way in C++.

The book leads off with several items on pointers—what they are and aren't—which can be vexing for people who didn't grow up on a diet of C code. It quickly leads into more complex topics, many template-related, including partial specialization, generic algorithms, substitution failure, argument deduction, and template template parameters.

I'd bet my last two bits that every C++ programmer would learn something from this slim volume.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College's CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries; just send an e-mail to sysop@HAL9K.com.

This article was originally published on August 5th, 2005

About the Author

Victor Volkman

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.

Most Popular Programming Stories

More for Developers

RSS Feeds

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