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.
So I am a student at my school and I am at a complete loss… I don’t have to time to create collisions with a certain type of character by May 15 next week. And no one will help me understand… Do you think that maybe you could send me a source code? If you have time?
just kidding I don’t need it sorry for bothering you
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.
Well after all this discussing around I was a bit confused and tried something out myself.
Using Allegro 5.0.7 I came to the result that the screen width and height you enter will be like you entered it. So 640 will be 640, not 641.
Now the tricky part. When placing pixels with al_draw_pixel you place it on pixel coordinates.
But! when drawing lines, you are NOT on pixel coordinates but on grid coordinates. So the 0 indicated you are on the left side of pixel 1 and with 640 you are on the right side of pixel 640 (or left of 641). So al_draw_line(0, x, 640, x, xxx, 1) will draw you a line which is indeed 640 pixel long.
Oh yeah, and thanks for the great tutorials!
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 0x00000178.
The program ‘[3496] AllegroTest_FONT.exe: Native’ has exited with code 0 (0x0).
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!
very nice
Hello Mike,
Top-notch tutorials!
Are you a professional game developer or just a hobbyist? How long have you been programming?
Just want to say thank you for your tutorials. They can make all the difference for ppl like me who just have fun coding and don’t want to spend money for classes and top end engines.
awesome tutorial sir, your explanation makes things seem so easy 😀
Amazing tutorials, i am learning so much. I remember you mentioned planning out graphics on graph paper before hand. I tried doing this but couldn’t scale the graph paper to the 640 by 480 pixels that I have to work with on the computer. Do you have any suggestions as to what I can do to more accurately draw graphics?
HEY! I might be a bit late, but, is there a way we acan store this primitives in a variable, and destroy them with ease at the call of an event?
Example, click HERE to destroy the blue rectangle.
I Hope you can answer!
Ok need help, I am running allegro 5.0.10 with code::blocks 12.11.
The command al_destroy_display(display) is causing my window to crash and terminating with status -1073741819.
Here’s the code:
#include
#include
int main(){
int width = 640;
int height = 480;
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
return -1;
display = al_create_display(width, height);
if(!display)
return -1;
al_init_primitives_addon();
al_draw_line(0, 0, 50, 50, al_map_rgb(255,255,255), 5);
al_clear_to_color(al_map_rgb(255,0,255));
al_flip_display();
al_rest(1.0);
al_destroy_display(display);
return 0;
}
any way for graphing primitives you could explain an effective way of laying it out on graphing paper? like if you had a display of 480×600, would you map out 0-479 x points on your graphing paper? which you would need quite a lot of graphing paper to do approximately.
How can i draw a polygon and fill it?