What is the best way to code a game loop in Allegro 5 that always runs at the same speed, and that properly separates drawing logic from update logic? Should I use threads or not? Should I make use of the new Allegro event system?
+2
A:
Taken from the allegro wiki:
al_install_timer(1.0 / FPS);
...
while (1) {
al_wait_for_event(queue, &event);
/* handle input events */
if (event.type == ALLEGRO_EVENT_TIMER) {
handle_game_tick();
need_redraw = true;
}
if (need_draw && al_event_queue_is_empty(queue)) {
render_last_frame();
need_redraw = false;
}
}
If you want frame skipping, skip the render_last_frame() command whenever you detect that you are lagging behind in frames (e.g. by using the al_current_time() function).
Ah, so you should make use of events. Could threading potentially improve image stability? Are event queues thread-safe?
amarillion
2009-08-01 13:14:49
Threading won't help at all. Usually it can help if you make use of the exact time when you are rendering (again, al_current_time() is your friend). This will allow things like interpolate the exact position where to display something in relation to the game logic. You should also keep in mind that al_flip_display() will wait for vsync (if it is enabled, but having vsync on is a must for perfectly smooth animation anyway).And A5 is 100% thread save - event queues are especially so as they are one of the means to communicate across threads.
2009-08-01 16:24:11
Link to wiki page mentioned: http://wiki.allegro.cc/index.php?title=Porting_from_A4_to_A5
Leftium
2010-08-20 03:57:59
+1
A:
Here is a more complete version of Allefant's answer (follow the link for detailed line-by-line explanation):
#include <stdio.h>
#include <allegro5/allegro.h>
const float FPS = 60;
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
bool redraw = true;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
timer = al_create_timer(1.0 / FPS);
if(!timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer);
return -1;
}
event_queue = al_create_event_queue();
if(!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
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_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(timer);
while(1)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
if(redraw && al_event_queue_is_empty(event_queue)) {
redraw = false;
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
}
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}
Leftium
2010-08-20 06:41:42