March 20, 2019
Hot Topics:

Cross-Platform Game Development for C++ Developers, Part II: The Allegro Platform

  • August 17, 2005
  • By Victor Volkman
  • Send Email »
  • More Articles »

Good news, would-be game developers: I am extending my introduction to cross-platform game development into a weekly series. This week, I highlight the Allegro (Allegro Low LEvel Game ROutines) open source library, going into technical depth and providing a brief demo, so you can determine if it's the right platform for you.

An Engine for Many Environments

Allegro began on the venerable Atari ST platform in the late 1980s and quickly migrated to the popular DJGPP environment (a 32-bit MS-DOS extender popular in the early 90s). Since then, Allegro has been ported to most popular Windows C++ development environments including VS, MinGW, Cygwin, and Borland C++. Other platforms that support it include Linux, BeOS, QNX, Mac OSX, and nearly any other Unix platform with X11 libraries.

Allegro can render into a variety of bitmap and hardware-accelerated environments, such as DirectX, XWindows, SVGAlib, FreeBE/AF, CGDirectDisplay, QuickDraw, and others. Allegro does not attempt to provide its own 3D environment or emulation thereof, but OpenGL can be easily integrated using the AllegroGL library, which provides a GLUT-like interface (including extension management).

An Extremely Brief Feature Overview

Before delving directly into APIs, a quick look under the hood is in order:

  • Drawing functions down to the pixel level, including flat shaded, gouraud shaded, texture mapped, and z-buffered polygons and circles, floodfill, bezier splines, patterned fills, sprites, blitting, bitmap scaling and rotation, translucency/lighting, and text output with proportional fonts
  • FLI/FLC animation player (a format with better compression characteristics than MPEG for computer-generated animations)
  • Plays background MIDI music and up to 64 simultaneous sound effects, and can record sample waveforms and MIDI input (Sound platform support includes WaveOut, DirectSound, OSS, ESD, CoreAudio, and QuickTime, just to name a few)
  • Easy access to the mouse, keyboard, joystick, and high-resolution timer interrupts, including a vertical retrace interrupt simulator in the DOS version
  • Routines for reading and writing LZSS compressed files
  • Math functions, including fixed point arithmetic, lookup table trig, and 3D vector/matrix/quaternion manipulation
  • GUI dialog manager and file selector
  • Built-in support for 16-bit and UTF-8 format Unicode characters

Let's Get This Engine Started!

In Allegro use, as in many other game scenarios, the overall structure consists of pre-game initialization, the game loop, and post-game cleanup. Initialization means both the Allegro startup code and the basics of loading or generating your game level at the starting location. Allegro imposes very little overhead in terms of writing your initialization code (see Figure 1).

If you need a lot of screen real estate, it is polite to first inquire with get_gfx_mode_list() to find the largest mode available:

#include <allegro.h>       // must come AFTER system headers
   set_color_depth(32);    // Defaults to 8-bit color otherwise
   if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
      abort_on_error("Couldn't set a 32 bit color resolution");

The last two parameters of set_gfx_mode() specify the size of the virtual buffer within which your graphics screen lies. This makes easy work of writing a side-scroller game where the terrain is continuously in motion. For example, you might make the virtual buffer, say, 20% wider to give you room to smoothly scroll in new sprites and terrain.

A Complete Allegro Example

This tutorial uses at Kee-Yip Chan's SnookerClone demo, which is based on another demo of the same name by James Lohr. Figure 1 shows the demo's basic screen shot.

Figure 1. Kee-Yip Chan's "SnookerClone" Demo

This project shows off a variety of Allegro techniques, including animation, keyboard and mouse input, collision, and gameplay physics (for example, gravity). It utilizes three primary elements: a rotating wheel with eight arms, a big red circle controlled by the arrow keys, and blue circles that drop from the ceiling. The wheel pushes the red ball on contact, and the red ball interacts realistically when colliding with the blue ones. The whole thing is kind of a cross between a pinball machine and one of those executive toys with falling beads suspended in a clear solution.

Page 1 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



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