Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Vsync-Dependent Game Loop

This thread is locked; no one can reply to it. rss feed Print
Vsync-Dependent Game Loop
DragonDePlatino
Member #16,608
December 2016

I recently started C programming so I decided to check out allegro 5. I've statically linked everything and confirmed allegro is correctly installed by running some examples.

I want to create a simple game loop tied to vsync but I cannot do so. I've tried every combination of these I can think of but it always crashes the moment an event is found. I've tried debugging but it fails with [Inferior 1 (process 2552) exited with code 03] every time I try to add an event loop.

To be clear I do not want delta time, I do not want to use timers and I do not want to pause my program's execution to wait for events. I want an SDL-style event loop where I continually render at 60 FPS regardless if there are any events in the queue. I've done this in the past with SDL 2.0, SFML and CSFML but I cannot do it with allegro.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <stdio.h> 3 4static ALLEGRO_DISPLAY* display; 5static ALLEGRO_EVENT_QUEUE* window_queue; 6 7static void init(void); 8static void quit(void); 9static void loop(void); 10 11int main(void) 12{ 13 init(); 14 loop(); 15 quit(); 16} 17 18void init(void) { 19 if (!al_init()) { 20 puts("Failed to initialize allegro!"); 21 exit(1); 22 } 23 24 al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE); 25 display = al_create_display(640, 480); 26 if (!display) { 27 puts("Failed to create display!"); 28 exit(1); 29 } 30 31 window_queue = al_create_event_queue(); 32 if (!window_queue) { 33 puts("Failed to create event queue!"); 34 al_destroy_display(display); 35 exit(1); 36 } 37 al_register_event_source(window_queue, al_get_display_event_source(display)); 38} 39 40void quit(void) { 41 al_destroy_display(display); 42 al_destroy_event_queue(window_queue); 43} 44 45void loop(void) { 46 bool window_open = true; 47 ALLEGRO_EVENT* event = NULL; 48 int value = 0; 49 50 while (window_open) { 51 while (al_get_next_event(window_queue, event)) { 52 if (event->type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 53 window_open = false; 54 } 55 } 56 57 value = (value + 1) % 256; 58 al_clear_to_color(al_map_rgb(value, value, value)); 59 al_flip_display(); 60 } 61}

And yes, I am aware that my game's speed will be tied to framerate and that the game will not even boot if the user does not have vsync.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

DragonDePlatino
Member #16,608
December 2016

Hey, I got it working! Keyboard input too! Thanks for your help. ;D

#SelectExpand
1#include <allegro5/allegro.h> 2#include <stdio.h> 3 4static ALLEGRO_DISPLAY* display; 5static ALLEGRO_EVENT_QUEUE* event_queue; 6 7static void init(void); 8static void quit(void); 9static void loop(void); 10 11int main(void) 12{ 13 init(); 14 loop(); 15 quit(); 16} 17 18void init(void) { 19 if (!al_init()) { 20 puts("Failed to initialize allegro!"); 21 exit(1); 22 } 23 24 if(!al_install_keyboard()) { 25 puts("Failed to initialize keyboard!"); 26 exit(1); 27 } 28 29 al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE); 30 display = al_create_display(640, 480); 31 if (!display) { 32 puts("Failed to create display!"); 33 exit(1); 34 } 35 36 event_queue = al_create_event_queue(); 37 if (!event_queue) { 38 puts("Failed to create event queue!"); 39 al_destroy_display(display); 40 exit(1); 41 } 42 al_register_event_source(event_queue, al_get_display_event_source(display)); 43 al_register_event_source(event_queue, al_get_keyboard_event_source()); 44} 45 46void quit(void) { 47 al_destroy_display(display); 48 al_destroy_event_queue(event_queue); 49} 50 51void loop(void) { 52 bool window_open = true; 53 ALLEGRO_EVENT event; 54 int value = 0; 55 56 while (window_open) { 57 while (al_get_next_event(event_queue, &event)) { 58 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 59 window_open = false; 60 } else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { 61 switch (event.keyboard.keycode) { 62 case ALLEGRO_KEY_ESCAPE: 63 window_open = false; 64 } 65 } 66 } 67 68 value = (value + 1) % 256; 69 al_clear_to_color(al_map_rgb(value, value, value)); 70 al_flip_display(); 71 } 72}

Go to: