(A5) Vaild Form of Game Loop?
Ukrzuel

Good morning,

I've used a lot of Allegro 4, and never really tried Allegro 5. Right now the biggest learning curve just seems to be getting use to these Events.

I've done a basic Game Loop, I think it's a valid one, but I'm not sure. I'm using the same Game Loops I normally do, checking logic and drawing within the same timer.

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3 4int main() 5{ 6 // Start Up Allegro 7 al_init(); 8 9 // Load Keyboard 10 al_install_keyboard(); 11 ALLEGRO_KEYBOARD_STATE Game_Keyboard_State; 12 13 // Create Display 14 ALLEGRO_DISPLAY *Game_Display = NULL; 15 16 Game_Display = al_create_display(640, 480); 17 18 // Create Timer 19 ALLEGRO_TIMER *Game_Timer = NULL; 20 Game_Timer = al_create_timer(1.0 / 60.00f); 21 22 // Create Events 23 ALLEGRO_EVENT_QUEUE *Game_Event_Queue = NULL; 24 Game_Event_Queue = al_create_event_queue(); 25 26 // Setup Events 27 al_register_event_source(Game_Event_Queue, al_get_display_event_source(Game_Display)); 28 al_register_event_source(Game_Event_Queue, al_get_timer_event_source(Game_Timer)); 29 al_register_event_source(Game_Event_Queue, al_get_keyboard_event_source()); 30 31 // Start Timer 32 al_start_timer(Game_Timer); 33 34 35 bool Game_Running = true; 36 bool Game_Drawing = true; 37 38 // TEST 39 bool Cycle = false; 40 bool Allow_Flash = false; 41 42 // ================= 43 // GAME LOOP - START 44 // ================= 45 46 while (Game_Running) 47 { 48 // Draw 49 if (Game_Drawing) 50 { 51 if (Allow_Flash) 52 { 53 if (Cycle) 54 { 55 al_clear_to_color(al_map_rgb(255, 255, 255)); 56 Cycle = false; 57 } 58 else 59 { 60 al_clear_to_color(al_map_rgb(0, 0, 0)); 61 Cycle = true; 62 } 63 64 al_flip_display(); 65 } 66 67 if (!Allow_Flash) 68 { 69 al_clear_to_color(al_map_rgb(255, 0, 0)); 70 al_flip_display(); 71 } 72 73 Game_Drawing = false; 74 } 75 76 while (1) 77 { 78 // Logic 79 ALLEGRO_EVENT ev; 80 al_wait_for_event(Game_Event_Queue, &ev); 81 82 if (ev.type == ALLEGRO_EVENT_TIMER) 83 { 84 al_get_keyboard_state(&Game_Keyboard_State); // Get Keyboard State 85 if (al_key_down(&Game_Keyboard_State, ALLEGRO_KEY_SPACE)) 86 { 87 Allow_Flash = true; 88 } 89 else 90 { 91 Allow_Flash = false; 92 } 93 94 95 Game_Drawing = true; 96 } 97 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 98 { 99 Game_Drawing = false; 100 Game_Running = false; 101 break; 102 } 103 104 if (al_is_event_queue_empty(Game_Event_Queue)) 105 { 106 break; 107 } 108 } 109 } 110 111 // ================= 112 // GAME LOOP - END 113 // ================= 114 115 // Destroy 116 al_destroy_timer(Game_Timer); 117 al_destroy_display(Game_Display); 118 al_destroy_event_queue(Game_Event_Queue); 119 120}

If this doesn't work fully as a valid Game Loop, please let me know what I can do better. I really want to get started with development once again with Allegro! :) I really enjoyed Allegro 4, other than the software rendering.

Elias

Yes, this should work fine. The only difference to the tutorial game loop is that you do the very first redrawing before the first call to al_wait_for_event.

Ukrzuel

Thank you very much!

I will look into the other way as well, just for my reference.

Thread #607655. Printed from Allegro.cc