2D Game Dev – Part 2.1: Starting the Code

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 <allegro5allegro.h>
#include <allegro5allegro_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.

Posted in 2D Game Dev, Allegro, C++, Code, Game Dev, Part 2, Tutorial
9 Comments » for 2D Game Dev – Part 2.1: Starting the Code
  1. Hi there, it seems that upon error the al_show_native_message_box does not display the specified error message if you do not specify a text for the third parameter. Well, at least on my system it does not.

    So what I did is something like this:
    al_show_native_message_box(NULL, “ERROR”, “ERROR”,
    “Failed to initialize allegro!”, NULL, NULL);

  2. It would be wise to add in:
    int main(int argc, char **argv){
    // add the code here….
    }

    The guy who commented… i didn’t get an error
    unless you installed another version of allegro

    • Mike Geig says:

      It may be wise, but I am intentionally leaving out everything that is superfluous or doesn’t apply to the demonstration at hand.

  3. John says:

    Just wanted to say thanks for these. Finally there’s a straightforward tutorial on Allegro and game programming in general. I’ve been getting sick of having to try to figure stuff out myself. You’re a great help : )

  4. Jasper Lu says:

    IMO you should really go more in depth as to what all the functions do. I’m staring at the page right now just wondering how the functions work.

  5. Jasper Lu says:

    nm i watched a video lol

  6. Pelint says:

    Am I the only one getting a ton of errors? :(
    ./main.cpp:6:1: error: expected unqualified-id before ‘if’
    ./main.cpp:13:1: error: ‘display’ does not name a type
    ./main.cpp:15:1: error: expected unqualified-id before ‘if’
    ./main.cpp:22:18: error: expected constructor, destructor, or type conversion before ‘(’ token
    ./main.cpp:23:18: error: expected constructor, destructor, or type conversion before ‘;’ token
    ./main.cpp:24:8: error: expected constructor, destructor, or type conversion before ‘(’ token
    ./main.cpp:25:19: error: expected constructor, destructor, or type conversion before ‘(’ token
    ./main.cpp:26:1: error: expected unqualified-id before ‘return’

    • gla3dr says:

      To me, that just looks you have a syntax error somewhere. Carefully look over your code and make sure it matches the code from the previous video exectly.

  7. Cody says:

    I think that more people should take a few C++ classes before trying this lol. It’s all laid out there, easy as can be. If you don’t know what a pointer or a function is, then there’s no point in even trying to figure out what’s going on in the code.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Error: Twitter did not respond. Please wait a few minutes and refresh this page.