This post is a portion of Part 4 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 4.0 – A Look at Timing
- Part 4.1 – Timing Our Game Loop
- Part 4.2 – Putting the Timing Together
Source code can be found here.
Notice that the source code is slightly different from the video? Here is why.
Thank you – this was very informative. Please keep the tutorials coming asap! 🙂
I am glad you enjoy. The next video is underway.
God bless you dude, hey im wondering why i can’t watch your videos now, yesterday i could, but i don’t know what’s happenning right now, do i need any addon?
-Thank you, blessings-
These videos have been really helpful 🙂
Cheers!
Working my way through the course. Really, can’t say thanks enough.
Question on this video: it seems cutoff at the very end. Given your consistent pattern, it’s likely just a couple of words, but wanted to make sure I wasn’t missing anything.
You are correct. I need to fix this.
Hey,
First of all thanks for this it has been really helpful and hopefully I will learn how to make some games will I’m on winter break.
Any who, it is saying that the video was taken down my the user 🙁
Thanks for letting me know. It is fixed now
hi! first thing: my apologies for my english.
Your course is amazing!! all those collision detection methods, background styles, etc…, really awesome, thanks for your work!!.
In the other hand, i’m spanish, and it’s a bit difficult to me understand you in the videos, (hehehe, i have to work my english), so, in this video, i can’t understand why the square is drawn without hit any key when you activates the timer. The drawing code is in the same line after all.
I believe I understand your question. Basically, since the timer is now running (it runs automatically) we don’t need to hit a button to get things to draw. The events start firing off right away.
great vid, thnx
is there any way to make the square move at a constant speed: its now moving at 10 pixels/fps both x direction and y direction. but if you press like UP-key and LEFT-KEY its speed will increase to squareroot 200 pixels/fps witch is about 14 pixels/fps. is there anyway to also make it move diogonaly at 10 pixels/fps?
Sure is. I cover angular velocity at some point. I think it is in the “gravity demo” in the sprite section. Keep watching.
Hi Mike,
I’m working on a game that is based off of your code and when I run the game, it uses over 100% of the CPU. I have to run ./a.out a random number of times in order for my game to run. I’m new to Allegro, but I would think this has something to do with the timing. Here is the timer related code that i have and then I destroy the timer at the end. I’m not sure if this is enough of my code for you to determine what is wrong or not.
Thanks!
Nicki
// Syncing various events
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
// Sets the background color
al_clear_to_color(al_map_rgb(0,0,0));
// Draws the start screen image
// Arguments: image,dx,dy,flags
al_draw_bitmap(imageStartScreen,0,0,0);
// Rotates the two buffers
al_flip_display();
// Plays and loops the music until the window closes
al_play_sample(music, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);
// Starts the game timer
al_start_timer(timer);
// Starts the game window and stops when it is closed
while(!done)
{
int step = 4;
// Create a new variable and then wait for an event to occur
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
// Counter to alternate “press start” every second
pressSpace = fpsCounter(countStart, FPS);
// Timer trigered event for the game logic
if(ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = true;
}
how does al_is_event_queue_empty(event_queue) works in this example? it should prevent programm from redrawing the screen if nothing changed, right? but for me it is always redrawing it,i’ve made the counter to see it. so i replaced this:
if(redraw && al_is_event_queue_empty(event_queue))
with this:
if(redraw && (keys[UP] || keys[DOWN] || keys[RIGHT] || keys[LEFT]))
that’s how i stopped it from redrawing screen all the time. but there should be a better way, right?
i know you explained why the rendering code should not be placed within the eventHandling code bt i am afraid i didn’t understand it very well. i know it’s too much to ask bt could you please give a boarder explanation?
tnx in advance sir
1. you said if do not monitor the FPS then 2 things will happen, one of them is with my input the object will move much more quickly than we want it to and also it will move inconstantly.
but when i copied your code, in my program the square actually moved more quickly. this is opposed to what mike said, now i am confused.
if anyone can explain this i will be really grateful