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.
- Part 2.0: 2D Graphic Basics
- Part 2.1: Starting the Code
- Part 2.2: Initializing Allegro Graphics
- Part 2.3: Using Fonts and Text
- Part 2.4: Graphical Primitives
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.
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);
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
It may be wise, but I am intentionally leaving out everything that is superfluous or doesn’t apply to the demonstration at hand.
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 : )
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.
nm i watched a video lol
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’
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.
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.
So I’m having a problem whenever I use Allegro. I’m copying your code perfectly, but whenever I debug the program, it creates the magenta windows but no matter how many times I try to click on it in the task bar, it doesn’t bring it to the front of the windows I have open.
I’m having the same issue. HELP!
Am I the only one getting the wrong colors?
Your code (255,0,255) gives me cyan, and so does (0,0,255).
(0,0,0) and (255,0,0) give me blue
(0,255,255) and (255,255,255) give me white
(0,255,0) and (255,255,0) actually give me magenta
Giving middle values seems to work the same. (128,128,0) results in a dark violet, for example.
When I add a fourth parameter, it refuses to compile (as expected).
So it seems that
a) the colors are ordered the wrong way: (B?,R,G)
b) blue is locked to full brightness, no matter what
I’m actually using Allegro 5.0.7 and compiling with MinGW. But that shouldn’t be the reason?
It gets weirder (or starts to make sense, depending on how you look at it): When using al_map_rgba(), the last three parameters are actually R,G,B. The first parameter seems to be ignored, and Alpha can’t be set. The *_f() versions exhibit the same behavior.
I suspect a bug in the way ALLEGRO_COLOR handles it’s memory.
Now I tried it with Visual Studio, and it works. So this seems to be a MinGW problem (or a problem with the libraries compiled for MinGW) after all. Looks like I have to use VS2010 for time being – too bad, I hate that piece of bloatware.
Sorry I didn’t respond sooner. Glad you were able to work it out. Is it possible that the method is different (different argument list) in the MinGW version? That doesn’t really seem likely, but it is what your experience seems to indicate.
Shouldn’t there be a slash after allegro5 in the includes like #include ?. Or maybe I unpackaged and installed things differently.
looks like the page has mistaken my text for html tags in my previous comment. I meant to write #include “allegro5/allegro.h”
Wow, you’re the first person to notice that (I think). Whoops. I need to fix that.
Going through all this right now for the first time and just wanted to let you know that your work is greatly appreciated.
Does al_flip_display() flip all displays presently allocated? Or does it instead flip the last display worked on? I am trying to go through your videos and take extensive notes, and it seems arbitrary as to what this method does. I understand that it flips the display to the back buffer, but just in case I ever have more than one display open, how does it decide WHICH display to flip?
I’ve been trying this with Dev-C++ 5.4.2 and in order for it to compile without error I had to use ALLEGRO_MESSAGEBOX_ERROR instead of NULL for the last paramater in the al_show_native_message_box() parts.
It now compiles and runs for me, although it always gives me a blue screen in the program window regardless of the R, G, and B values passed to al_map_rgb() and it also doesn’t exit gracefully, instead Windows 7 happily gives me an “AllegroTest.exe has stopped working…” box when it’s finished running.
Oh well, on to the next part… :]
I think the syntax of Allegro 4 was much simpler and more intuitive.
I did too before using Allegro 5 extensively. Now I wouldn’t go back.
Good tutorials, thanks!
In Xcode on iMac, I had to put a zero as the last argument for al_show_native_message_box. That last argument expects an int flags and will not take NULL.
Other than that, it all worked fine for me.
Peace!
I am getting an error with header file allegro_native_dialog. I checked and found that liballegro_native_dialog.so and related files are missing.how to fix it? I am working in codeblocks. Please help. I am completely strucked..
First i would say may God reward you for the good work.
The code above displays only a black screen no matter the value of RGB. i tried it both on visual studio and Code Block. wWhat could be the problem.
thanks.
I wrote this program on Ubuntu, where I’ve installed Allegro5.
While I try to compile, the compiler gives me this error: Undefined references for “al_show_native_message_box”.
How can I solve it??
This is a nice tutorial! Unfortunately it’s in c++ (anybody else here the C kind?) so I’m blowing through a brief C++ tutorial, setting up Visual Studio (it’s HUGE and is taking about an hour), and also reading comments. It’s very similiar to C and is pretty easy to learn by my standards, just a bit confusing why it uses “COUT <<"