Introduction to Allegro using Dev-CPP
Introduction to Allegro using Dev-C++
NOTE: This article was originally written a long time ago. I no longer use Dev-C++ as I have found Code::Blocks to be all-around better (in my opinion.) This information is provided for those who do wish to use Dev-C++. If you have not installed Dev-C++, I would recommend looking at Code::Blocks, or even Visual C++ Express (which is also free.) Of course, if you have the money, the professional version of Visual Studio is about as good as it gets. (It's not often I recommend a Microsoft product.)
One of the most difficult aspects of cross-platform development is handling the OS-specific methods, such as window creation and reading input. GLUT simplifies many of these tasks by providing methods for creating and initializing the window and reading user input. Using OpenGL with GLUT would allow a developer to create code that should run on most platforms, but in order to make a decent game, you will need to be able to handle music, networking, etc. Allegro is a full featured API that will handle all aspects of game programming: graphics, audio, input, window creation, etc. I still prefer using OpenGL for rendering the graphics, and a later tutorial will cover AllegroGL, which allows using OpenGL for the graphics, and Allegro for everything else. Before we can get to that, though, we need to get Allegro itself up and running.
(The Allegro website also has nice instructions on installing Allegro for Dev-C++)
Requirements
-
Allegro source code (DOS/Windows version)
Install MinGW and the DirectX Headers
Once you have downloaded the updated MinGW files and the MinGW DirectX header files, you need to decompress them to the Dev-C++ install path. For the MinGW files, copy the MinGW32 folder to the Dev-C++ install path. For the DirectX headers, just copy the contents to the Dev-C++ install path. There are a bunch of header files that need to go in the include directory, as well as library files, which need to go in the lib directory.
Setup the Paths
If you haven't done so, install Dev-C++. You will need to set your environment path to include the location of the Dev-C++ binary files so you can run 'gcc' from the command-line (without entering the full path…)
-
Windows 95 or Windows 98:
Using a text editor, open the c:\autoexec.bat file. Add to the SET PATH entry (or create a new entry if necessary)SET PATH=<devcpp_install_path>\bin;%path% SET MINGDIR=<devcpp_install_path>You will need to restart your computer for the changes to take effect.
-
Windows ME:
Go to 'Start→Run' and type 'msconfig.' Select the 'Environment' tab and make the above changes. Restart the computer for the changes to take effect.
-
Windows NT, 2000, XP:
Open theSystemapplet from theControl Paneland select theAdvancedtab. Click theEnvironment Variablesbutton to bring up the editor dialog. UnderSystem Variables, select (or create)Pathand click theEditbutton. Add<devcpp_install_path>\binat the end of the entry and hitOK. Create a new variable calledMINGDIRand set it equal to<devcpp_install_path>. Restart the computer for the changes to take effect.
Compile the Binaries
Open a DOS prompt, and go to the directory where you extracted the Allegro source files and type gcc -v. This will take care of configuring Dev-C++.
Configure Allegro for Windows/MinGW32
This step is extremely easy. At the DOS prompt, type fix mingw32, and it will configure Allegro for Windows/MinGW32. Type make to compile it.
Install Allegro
If you haven't set the MINGDIR environment variable, you can do it manually by typing SET MINGDIR=<devcpp_install_path> at the prompt. To install Allegro, type make install.
Your First Program
Now that we are done with the installation, we can have some fun and write our first Allegro program. This example will create a window, and write some text in the center of the screen. Since we are going to be using OpenGL for the graphics, we won't spend too much time with Allegro's graphics handling routines (although, you might decide to use them in your projects.)
Create a New Project
Create the project for the Allegro example. Go to File→New→Project and select Empty Project on the Basic tab. Specify a project name, and click 'OK.'

Add a Blank Document
The template will have generated a project with no files, so we'll need to add one now. Right-click on the project name at the left side of the screen and choose New File to add a blank document to the project. You can go ahead and save this as main.cpp.

Start Adding Code
Include the Allegro header:
#include <Allegro.h>We need to write the main entry point for the application. In order to keep cross-platform compatibility, Allegro uses int main(void) followed by a macro (which we'll cover in a bit) to make this work on all operating systems.
int main() {
We use the allegro_init macro to initialize the Allegro library:
// Initialize Allegro: allegro_init();
Install the Allegro keyboard handler so we can exit the program when the user presses a key:
// Install the keyboard: install_keyboard();
Create a 640×480 pixel full-screen window by using Allegro's set_gfx_mode( int card, int w, int h, int v_w, int v_h ). The card parameter should usually be GFX_AUTODETECT, and indicates if the window should be windowed or full-screen. For instance, if the requested size is not allowable for full-screen resolution, it will be windowed. If the requested size is an allowable resolution, it will be full screen. You can override this by using GFX_AUTODETECT_FULLSCREEN or GFX_AUTODETECT_WINDOWED. The latter might be useful when you absolutely want a windowed application, but be careful forcing an application to be full-screen. If the system cannot use the requested resolution, set_gfx_mode will return an error. To make sure it always sets the mode correctly, combine the card parameter with GFX_SAFE.
The w and h parameters specify the requested width and height of the display, respectively, and v_w and v_h are the minimum virtual screen dimensions. Once the screen is set up, you can get the dimensions using the macros SCREEN_W, SCREEN_H, VIRTUAL_W, and VIRTUAL_H.
// Create a 640x480 fullscreen window: if ( set_gfx_mode( GFX_AUTODETECT_FULLSCREEN | GFX_SAFE, 640, 480, 0, 0 ) ) { // Error creating window: allegro_message( "Error setting the graphics mode!" ); return 1; }
We will output our text in the center of the screen. The textout_centre method will center the text horizontally about the specified point. The value screen refers to a global bitmap the size of the screen, and font is a simple 8×8 font, which is the mode 13h BIOS default. We use SCREEN_W and SCREEN_H to get the screen size so we can calculate the center of the screen. Finally, makecol will convert three RGB byte values to a color value.
// Write a message in the center of the screen: textout_centre( screen, font, "Welcome to Allegro", SCREEN_W / 2.0, SCREEN_H / 2.0, makecol( 255, 255, 255 ) );
We need to wait for the user to press a key before we can exit, so we tell Allegro to just sit around waiting for keyboard input. After that, we can leave the application successfully.
// Check for keyboard input: readkey(); // Return successful: return 0; }
The last thing we need to do is add the END_OF_MAIN() macro. This ensures cross-platform compatibility by mangling the main procedure to something the operating system likes (for example, Windows likes WinMain.)
END_OF_MAIN();
Remember to link to the Allegro library by going to Project→Project Options and adding -lalleg to the linker parameters under the Parameters tab. Now you can compile and run your first Allegro program. Be prepared to be awed.
Downloads
| MyFirstAllegro.zip | The above application, ready for Dev-C++. |
| DevCpp_Allegro_template.zip | A project template for Dev-C++ to generate a simple Allegro application, like the one above. |
