2D Game Dev – Part 2.4: Graphical Primitives

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.

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.

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?

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.

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.

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);

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.

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!

nocrej
on November 25, 2011 at 2:24 am

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.

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();

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?

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.

Categories

Meta

About Me

Mike Geig is a Trainer for Unity Technologies where he develops and delivers recorded, live, and onsite learning content. He enjoys loitering and accordions. A gamer at heart, Mike works to make game development fun and accessible for all skillsets. Rumor has it he also wrote a book called "Unity Game Development in 24 Hours". Mike was once set on fire and has over a million “likes” on Facebook. Hi, Mom!