Warning: A non-numeric value encountered in /home/fixbyp5/public_html/wp-content/themes/Divi/functions.php on line 5752

This post is a portion of Part 2 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.


In Part 1.2, we typed and ran some code to ensure that Allegro was installed properly. At the time, the explanation of the code was skipped. In this section we will be going back over that code step by step and seeing what each part does (refer here for a full listing of the code).

#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>

The first two lines of this program simply import the header files which contain our Allegro functions. Normally, we only need to load allegro5allegro.h, but in this sample app we are also utilizing native message boxes, which is why we have to include the allegro_native_dialog.h file.

ALLEGRO_DISPLAY *display = NULL;

if(!al_init())
{
    al_show_native_message_box(NULL, NULL, NULL,
        "failed to initialize allegro!", NULL, NULL);
    return -1;
}

Next we create a display variable for our game screen. In Allegro, we create a variable of type ALLEGRO_DISPLAY which allows us to control and work with screen elements. After our variable is declared, we run and test the al_init() function. This function starts up Allegro and prepares some useful global variables for us. If initializing Allegro fails, a native message box (native means it will work on any operating system: the power of Allegro) appears and lets the user know before the application exits.

display = al_create_display(640, 480);

if(!display)
{
    al_show_native_message_box(NULL, NULL, NULL,
        "failed to initialize display!", NULL, NULL);
    return -1;
}

Here we create our display and set the resolution to 640 wide by 480 tall. We test the display to make sure it is working correctly.

al_clear_to_color(al_map_rgb(255,0,255));
al_flip_display();
al_rest(5.0);
al_destroy_display(display);
return 0;

Finally, we set our “back buffer” (more on that later) to the color of hideous magenta. We then flip our display to bring our magenta background to the screen. After that we rest 5 seconds and then destroy the display and close the program.

That wraps up our look at the code from last chapter. Now we can move on and look at doing something slightly more interesting.