2 Initializing Allegro



Table of Contents

  1. Introduction
  2. Initializing Allegro
  3. Keyboard Input
  4. Drawing Graphics
  5. Playing Sound
  6. Drawing Text
  7. Regulating FPS
  8. Enumerations and Random Numbers
  9. Bounding Box Collision Detection
  10. Planning the Game
  11. Super Basic Game Structure
  12. Wrapping up


Download entire guide as ODT


Part 2 – Initializing Allegro


Before you can have fancy graphics and a sweet game, you first need to display a window on the screen!

Allegro initialization:

When you create a new project, make a blank console application. You can get to the linker by Project > Compiler options in Code::Blocks or Project > Project Options in DevC++.

In the linker, you’ll write:


And that’s it!


int main()
	/* Initialization */
	set_color_depth( 16 );

	bool fullscreen = false;

	if ( fullscreen == true )   // For fullscreen 
		set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
	else                        // For windowed 
		set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

	BITMAP *buffer = create_bitmap( 640, 480 );
	/* End Initialization */

	/* Game loop and such would go here */
	while ( !key[KEY_ESC] )
		/* Draw functions */
		blit( buffer, screen, 0, 0, 0, 0, 640, 480 );
		clear_bitmap( buffer );

	/* Free memory afterwards! */
	destroy_bitmap( buffer );

	return 0;

Right now, this will merely pop up a window and immediately close it, as there is nothing to keep the program running.

Make sure that there is


is at the end of your program or you’ll get a “[Linker error] undefined reference to ‘WinMain@16’” error.

Double Buffering

Normally with a game, you will use double buffering. This will keep the screen from flickering. Basically, it will draw everything to a surface (surface for SDL, bitmap for allegro), and once everything’s all drawn to this surface, it is drawn to the screen all at once.

Cleaner Code

The initialization functions should not be put in main. Normally, I would create a “Game” or “System” class, and put these into the constructor or a Setup function. That way, you will just have something like this:

int main()
	System myAllegroGame;

	return 0;

One of your goals in writing games is to keep main as small as possible, and try to find a balance between Object Oriented code and using more C-style techniques where appropriate. While our computers don’t really need optimized code, you can go overboard with OO techniques.

Make sure your exe works right!

Make sure you keep alleg42.dll in your game’s folder (same directory as the .exe)

© Rachel J. Morris, 2009

Print Friendly