I just got into allegro this week and by the looks of it, allegro 5 is brand new and so their are very few tutorials for it (at least as far as what I can find). After going through the few tutorials on the allegro.cc wiki, I decided to say to heck with it and try to make pong, and I've actually gone quite a bit further than I thought I would.
Now here is the problem, I've managed to get 3 bitmaps: 2 paddles and a bouncer.
I got the two paddles mapped to the keyboard so I can move them up and down, and the bouncer is happily bouncing around all over the place. Now I'm trying to make the bouncer bounce off the paddles. I've included my code, the logic for the bouncing starts at line 132.
I've got the bouncer bouncing at the same x-coordinate as the paddles, which means it bounces even when it's not hitting the paddle, no good!
BTW, I'm new to the forum, so let me know if this isn't the place to post this, since it's kind of a coding question along with a concept one.
Make line 141:
if((bouncer_x <= left_paddle_x + paddle_width / 2.0 + bouncer_size/2.0 && bouncer_y >= left_paddle_y - bouncer_size/2.0 && bouncer_y <= left_paddle_y + paddle_height - bouncer_width/2.0) || (bouncer_x >= right_paddle_x - (paddle_width / 2.0)) - bouncer_size/2.0 && bouncer_y >= right_paddle_y - bouncer_size/2.0 && bouncer_y <= right_paddle_y + paddle_height - bouncer_width/2.0))
You also want to check if it's y value is in the boundaries of the paddle.
Great! The bouncer is now bouncing off the paddles!
Now as far as changes go from allegro4 to 5, should I be able to figure out the rest of what I need from reading examples from allegro 4 for things like, a scoreboard, restarting the board when the bouncer hits the edge of the screen, a title screen etc?
Thanks for the help btw!
My first game!
Yay!
Rather than create bitmaps for the paddles and fill them with color, you could just draw them with primitives instead. Bust out some rounded rectangles, add a little shine-shine!
{"name":"603455","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/a\/1a49b8bc2a314928768582ec86377c39.png","w":672,"h":530,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/a\/1a49b8bc2a314928768582ec86377c39"}
I made some examples:
I used
#include <allegro5/allegro_primitives.h> #include <allegro5/allegro_color.h>
and don't forget to call al_init_primitives_addon().
Also, I used
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);
before setting up the display to make the lines draw smoothly.
Ok I can't for the life of me figure out how you did that. I've never used primitives before, and all I have to work off of is the allegro5 manual. Do you still draw a bitmap and then draw the primitive on top of it?
It makes sense to me to put the draw_paddle function in another file to avoid clutter but does that mean that I should put the al_init_primitives_addon() in both .cc files?
Basically, how do I go about implementing those primitives into my code? Do I just add the draw_paddle function at the end of the main loop where I draw the bitmaps?
I'll do a step by step from your original code attachment.
1.
First, I added
#include <allegro5/allegro_primitives.h> #include <allegro5/allegro_color.h>
just below your #include <allegro5/allegro.h>. All allegro_color does is gives me the function al_color_html(). You could just as easily take this out and use al_map_rgba_f() or other similar function for your colors.
2.
Those two functions I made, draw_paddle(float x, float y) and draw_ball(float x, float y) (same as they are in the last post), I placed them just before main. This is also below your constants and enums. Usually my code structure works out like this:
# includes
# contants and other definitions
# functions
# main
3.
before you create the display, (eg. before display = al_create_display(SCREEN_W, SCREEN_H);, I added three lines
and
al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);
al_init_primitives_addon(); can go anywhere after al_init(), and it's best to keep it with the rest of your init()s if you have more.
4.
Then, way down where you have
al_draw_bitmap(right_paddle, right_paddle_x, right_paddle_y, 0); al_draw_bitmap(left_paddle, left_paddle_x, left_paddle_y, 0); al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
I commented those out and put in:
//al_draw_bitmap(right_paddle, right_paddle_x, right_paddle_y, 0); //al_draw_bitmap(left_paddle, left_paddle_x, left_paddle_y, 0); //al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); draw_paddle(right_paddle_x, right_paddle_y); draw_paddle(left_paddle_x, left_paddle_y); draw_ball(bouncer_x, bouncer_y);
5.
Profit!
Do you still draw a bitmap and then draw the primitive on top of it?
Nope. Primitives draw themselves. Of course, you're drawing the primitives to the display, which technically is a bitmap. But you don't need to have any extra surfaces to render primitives.
It makes sense to me to put the draw_paddle function in another file to avoid clutter but does that mean that I should put the al_init_primitives_addon() in both .cc files?
al_init_primitives_addon() is like al_init(). You only need to call it once when you setup your program and you're good to go.
Do I just add the draw_paddle function at the end of the main loop where I draw the bitmaps?
Yeaup.
Thanks for the help!!!
Now I'm trying to figure out how to add font so that I can add in a scoreboard. I took a look at the code in the allegro wiki on how to do fonts, but I can't seem to compile it properly. Here is the error
jason@Wally:~/Documents/game dev/allegro wiki lessons/lesson 6$ gcc lesson6fonts.cc -o lesson $(pkg-config --libs allegro-5.0 allegro_font-5.0 allegro_ttf-5.0)
jason@Wally:~/Documents/game dev/allegro wiki lessons/lesson 6$ ./lesson
lesson: /home/jason/Documents/games/allegro-5.0/addons/font/text.c:73: al_draw_ustr: Assertion `font' failed.
Aborted
jason@Wally:~/Documents/game dev/allegro wiki lessons/lesson 6$
Looking at this terminal panic, it's looking for the fonts config in a folder called jason/Documents/games, which doesn't exist. My allegro folder is in Documents/game dev/allegro-5.0/etc...
Do you mean that page?
Anyway, it seems that the compilation is not the problem, but loading the font is. Have you tried putting the font file into the directory where you call your program from? This would most probably be the directory where you have your executable.
Just tried that, but it didn't work
Which front end/compiler are you using? CodeBlocks likes to create a bin folder for your executable, but keep the root folder for your project by default.
Also, what is the code you're using to try and initialize the font?
I'm using gcc
The code that I'm using is from the allegro wiki
Just trying to get that code to work so that I can implement it into my game