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
This is the final entry into part 2. In this video we look at many of the graphical primitives available to us in Allegro 5. Since there are quite a few primitives, and only one video to cover them in, much of the stuff is covered quickly. Feel free to message me or leave a comment with any questions you may have.
Update:
Thank you Krisztian for pointing out an error in my videos:
There is a slight error in the explanation about screen size. It is incorrect that the screen width of 640 is 0 – 639. The declaration of the screen width as
int width = 640;
actually creates a screen that is 641 pixels wide. 0 – 640. The Allegro reference manual does not note how Allegro handles this internally, so you can be off by 1 pixel. The traditional array construct that is used when arrays are offset is useful, if you want to specify the absolute width.
int width = 640 – 1;
You can see this with the following code if width = 640.
al_draw_line(640, 0, 640, 480, al_map_rgb(255, 255, 255), 1); //edited by MG
In the above comment, the line of code (modified by me) will draw a line down the right side of the screen on the 640th pixel, which by original explanation should not be visible.
Source Codes:

Thanks so much for these tutorials. They are excellent!
Great tutorials.. Quick question though: Why in the al_draw_line() you insert integer arguments since it says that it accepts float?
Simplicity. I am going for as easy as possible.
One thing i want to point out about your learnings:
Focus a lot more on the theory, rather than the commands.
Explain why you used the numbers for about 2 times more than writting the code.
This is not about me, i got everything working, but in a class you are going to teach,
none will get what you will say, they will just type in the code and that is all.
I’d actually prefer he focus on the commands. The most important part of dealing with any library/API is knowing what functions do what. I can learn the theory later. As a beginner, I just want to know how to do basic things and be able to write a program at all; the extra stuff is, well, extra.
Why is fullscreen disabled?
Fullscreen is off be default. I leave it off because it is easy to test run games when I am not worried about going fullscreen. I will talk about turning it on in a future video.
There is a slight error in the explanation about screen size. It is incorrect that the screen width of 640 is 0 – 639. The declaration of the screen width as
int width = 640;
actually creates a screen that is 641 pixels wide. 0 – 640. The Allegro reference manual does not note how Allegro handles this internally, so you can be off by 1 pixel. The traditional array construct that is used when arrays are offset is useful, if you want to specify the absolute width.
int width = 640 – 1;
You can see this with the following code if width = 640.
al_draw_line(0, 100, 639, 100, al_map_rgb(255, 255, 255), 1);
al_draw_line(0, 103, 640, 103, al_map_rgb(255, 0, 0), 5);
The red line is 1 pixel more to the right and is still visible on screen, while the left most pixels are at 0.
Ah yes, you are correct. Thank you. I am going to modify my post with your response. Though I am going to edit your code example slightly to really highlight that extra pixel.
Hello Mike,
first of all: GREAT work! I just started learning C++/Allegro and your tutorials are the best I found in the www.
And if I understood everything till the end of Part 2, the correction isn’t correct.
On the one hand, it is also true, that the last visible x-value is 640.
But on the other hand, al_create_display(640, 480) really creates a screen that is 640 pixels wide. You already proofed it in part 2.3 when al_get_display_width returned 640 and not 641.
So there is only one conclusion, that both is true: the first visible x- and y-value is not 0 (like in arrays), it is 1!
I tried to draw a line on the x-value 0 and I didn’t see it.
al_draw_line(0, 0, 0, 480, al_map_rgb(255, 0, 255), 1);
I think, it can be seen in this little example:
al_clear_to_color(al_map_rgb(0,0,0));
// 3 lines left and rigth
al_draw_line(1, 100, 1, 200, al_map_rgb(255, 100, 100), 1);
al_draw_line(1, 200, 1, 300, al_map_rgb(100, 255, 100), 1);
al_draw_line(1, 300, 1, 400, al_map_rgb(100, 100, 255), 1);
al_draw_line(640, 100, 640, 200, al_map_rgb(255, 100, 100), 1);
al_draw_line(640, 200, 640, 300, al_map_rgb(100, 255, 100), 1);
al_draw_line(640, 300, 640, 400, al_map_rgb(100, 100, 255), 1);
al_flip_display();
al_rest(4.0);
al_clear_to_color(al_map_rgb(0,0,0));
// 1 lines left and 2 lines rigth
al_draw_line(-1, 100, -1, 200, al_map_rgb(255, 100, 100), 1);
al_draw_line(0, 200, 0, 300, al_map_rgb(100, 255, 100), 1);
al_draw_line(1, 300, 1, 400, al_map_rgb(100, 100, 255), 1);
al_draw_line(639, 100, 639, 200, al_map_rgb(255, 100, 100), 1);
al_draw_line(640, 200, 640, 300, al_map_rgb(100, 255, 100), 1);
al_draw_line(641, 300, 641, 400, al_map_rgb(100, 100, 255), 1);
al_flip_display();
al_rest(4.0);
// 2 lines left and 3 lines rigth
al_draw_line(0, 200, 0, 300, al_map_rgb(100, 255, 100), 2);
al_draw_line(641, 300, 641, 400, al_map_rgb(100, 100, 255), 3);
al_flip_display();
al_rest(4.0);
In the first 4 seconds, you’ll see 3 different coloured lines left and right.
After al_flip_display() there is only the bottom line on the left, and the two upper lines on the right side.
After the last redrawing with thickness 2 and 3, you can see the right half of the left middle line and one-third of the bottem right line again.
With best regards from Germany
Calle
Cool. Thank you for this.
What does the command return -1; do differently compared to return 0;?
Both will bounce you out of the current block of code, but you can use the numbers to denote significant data. For instance, exiting with 0 or 1 (sometimes) can mean everything worked fine. Exiting with something else can signify an error occurred. You could say, if an image doesn’t load, return -2 and if a font doesn’t load return -3. Then, when your program exits with a certain code, you can narrow down the issues.
Hello, I am receiving an error every time I try to run this program in VS2010 Professional x86.
Whenever I run the program, the console screen flashes and then it appears to be creating dozens of displays until it eventually gives me this error:
Unhandled exception at 0x1012512b in AllegroTest_FONT.exe: 0xC0000005: Access violation reading location 0×00000178.
The program ‘[3496] AllegroTest_FONT.exe: Native’ has exited with code 0 (0×0).
Any clarification on what that might mean would be awesome!
Email me your code
Thanks for the fast response!
Luckily, I was able to figure it out last night after playing around with VS. The problem wasn’t actually in the coding, VS just decided it was going to change my output directory, thus looking for something that wasn’t there…
But now, I’m trying to draw a triangle to the display, but I’m getting the LNK2019 error for both al_draw_triangle and al_init_primitives_addon();
I’ll e-mail you the .cpp file, perhaps I’m doing something wrong?
Thank you again!
Figured it out! – while setting the link output, I typed in version 5.0.1 — I’m currently using 5.0.5, silly mistakes, but they are the absolute worse =/
Fantastic tutorials, incredibly useful and informative!