Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 5 basic game loop

Credits go to Xpicolo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Allegro 5 basic game loop
LennyLen
Member #5,313
December 2004
avatar

I'd like to spend the next few weeks learning the basics of Allegro 5 (I think it's finally time to say goodbye to Allegro 4). I know A5 is event based unlike A4, so the basic game loop is going to be different.

I won't have much spare time between paid work and moving so I don't want to spend too much time reinventing wheels, and as I learn best from dissecting code rather than from tutorials, I'm hoping someone could give me an example of a basic event driven gameloop. Just something simple like looping a large background image over a smaller window would be good to get me started I think. I can add extra functionality (input etc, from there).

Thanks in advance.

edit:

This is the code I tried to do the above, but I seem to have missed something somewhere, as I'm trying to increment an x value which represents the position on the larger bitmap to draw from, but x never seems to increment. I tried it without using the timer and just increased x each loop and that worked, but I want it to run at a set rate.

#SelectExpand
1#include "stdafx.h" 2#include <allegro5\allegro.h> 3#include <allegro5\allegro_image.h> 4#include <allegro5/allegro_native_dialog.h> 5 6int main() 7{ 8 ALLEGRO_DISPLAY * display; 9 ALLEGRO_EVENT_QUEUE *queue; 10 ALLEGRO_BITMAP * nebula = NULL; 11 ALLEGRO_TIMER *timer; 12 13 al_init(); 14 display = al_create_display(1366, 768); 15 queue = al_create_event_queue(); 16 timer = al_create_timer(1.0 / 100.0); 17 18 al_install_keyboard(); 19 al_register_event_source(queue, al_get_keyboard_event_source()); 20 al_register_event_source(queue, al_get_display_event_source(display)); 21 al_register_event_source(queue, al_get_timer_event_source(timer)); 22 23 al_init_image_addon(); 24 nebula = al_load_bitmap("Assets\\Gfx\\Background\\Nebula Red.png"); 25 if (nebula == NULL) { 26 al_show_native_message_box(display, "Error", "", "Error loading bitmap", "", ALLEGRO_MESSAGEBOX_ERROR); 27 al_destroy_display(display); 28 al_uninstall_keyboard(); 29 exit(EXIT_FAILURE); 30 } 31 32 bool done = false; 33 34 int display_width = al_get_display_width(display); 35 int height = al_get_display_height(display); 36 int nebula_width = 4096; 37 float x = 0.0; 38 39 while (!done) { 40 if (display_width + x <= nebula_width) { 41 al_draw_bitmap_region(nebula, x, 0, display_width, height, 0, 0, 0); 42 } 43 else { 44 al_draw_bitmap_region(nebula, x, 0, nebula_width - x, height, 0, 0, 0); 45 al_draw_bitmap_region(nebula, 0, 0, display_width - (nebula_width - x), height, nebula_width - x, 0, 0); 46 } 47 al_flip_display(); 48 49 50 if (x > nebula_width) x = 0.0; 51 52 ALLEGRO_EVENT event; 53 54 if (!al_is_event_queue_empty(queue)) { 55 al_wait_for_event(queue, &event); 56 if (event.type == ALLEGRO_EVENT_KEY_UP || event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 57 done = true; 58 if (event.type == ALLEGRO_EVENT_TIMER) { 59 x += 0.05; 60 } 61 } 62 } 63 64 al_destroy_display(display); 65 al_uninstall_keyboard(); 66 al_destroy_bitmap(nebula); 67 68 return 0; 69}

Xpicolo
Member #16,781
December 2017

Where is al_start_timer ?

LennyLen
Member #5,313
December 2004
avatar

Xpicolo said:

Where is al_start_timer ?

I knew I'd missed something really obvious.

NiteHackr
Member #2,229
April 2002
avatar

I honestly can't recall the issues I had with Allegro 5 when i first switched. It was just a matter of getting used to the events, which was trivial.

The only thing that popped into my head was where I added in a function similar to A4's to check if a key was pressed, and wrote my own wait for keypress function. Otherwise... it's pretty simple and honestly quite nice. The benefits outweigh any nitpicks I ever had (like the sound system, and some minor joystick issues I forget about now).

This was handy as I was used to having a similar function in A4...

// Returns whether or not a specified key is being pressed
bool key_pressed(int key)
{
   ALLEGRO_KEYBOARD_STATE s;
   al_get_keyboard_state(&s);
   return al_key_down(&s, key);
}

This was a function I wrote for Deluxe Pacman 2 to wait for a keypress or xxxx amount of milliseconds before continuing (this is the trimmed down version)...

#SelectExpand
1// Waits a specified time (in ms, 1000=1s) for a key to be pressed. 2// Will also respond if a mouse or joystick button is clicked. 3// If the time passes before a key/mouse/joystick is pressed, the function returns 4// If the time specified is zero, the function will wait indefinitely. 5bool wait_for_keypress(double wait_time) 6{ 7 double ts = al_get_time(); // time stamp 8 double elapsed_time; 9 10 al_flush_event_queue(event_queue); 11 12 bool done = false; 13 14 // Wait until the specified time passes or a key is pressed. 15 do { 16 al_wait_for_event(event_queue, &event); 17 18 if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 19 shut_down(); // free resources 20 exit(0); 21 } 22 23 else if(wait_time && event.type == ALLEGRO_EVENT_TIMER) { 24 elapsed_time = al_get_time() - ts; 25 if(elapsed_time >= wait_time) return false; 26 } 27 28 // Wait for key to be pressed and released to continue 29 else if(event.type == ALLEGRO_EVENT_KEY_UP) { 30 if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) return true; 31 else done = true; 32 } 33 34 // And if a key wasn't pressed, maybe a mouse button was... 35 else if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 36 done = true; 37 } 38 } 39 while(!done); 40 41 return false; 42}

LennyLen
Member #5,313
December 2004
avatar

This is what I ended up with:

video

NiteHackr
Member #2,229
April 2002
avatar

Looks nice!

LennyLen
Member #5,313
December 2004
avatar

I'm thinking about writing a side-scrolling shooter and having this as the background.

NiteHackr
Member #2,229
April 2002
avatar

Yeah, that would make a great looking backdrop. It brings to mind R-Type, a game I spent too many quarters on back in the days of arcades. ;)

Edit: Yeah, a lot like it... call your game... "Deluxe R-Type" LMAO :P (edit: or L-Type!)

video

Trezker
Member #1,739
December 2001
avatar

I had this music on when I saw the parallax background and it felt very fitting.

video

Go to: