- Online Community Forums » Programming Questions » Is there a way to wait before drawing a bitmap in Allegro5?

This thread is locked; no one can reply to it. rss feed Print
Is there a way to wait before drawing a bitmap in Allegro5?
Member #20,796
August 2021

I'm have two characters, one is playable and the second is not. A keyboard event changes the variable that draws the attack bitmaps of the playable character. Is there a way to wait before the variable of the second character is changed? They need to get triggered by the same keyboard event, but I don't want the bitmaps to be be drawn at the same time.

William Labbett
Member #4,486
March 2004

I'm not any kind of game programming expert but I think something like this might work

Let's say it's ALLEGRO_KEY_ENTER that is the keyboard event that changes the variable that draws the attack bitmaps.

First, have one variable for each player.

Usually a switch is used for events so you might have something like

1 2#define PLAYER_2_ATTACK_DELAY_TIME 500 3 4 5bool enter_key_pressed = false; 6ALLEGRO_TIMER *timer = al_create_timer(60.0f); 7al_start_timer(timer); 8al_register_event_source(queue, al_get_timer_event_source(timer)); 9 .... ......... ... 10 11 12pseudo : have a main loop starting here : 13 14/* pseudo : */ 15while(/*there's still events in the queue*/) 16{ 17 switch(event.type) 18 { 19 case ALLEGRO_KEYBOARD_EVENT: 20 switch(event.keyboard.keycode) 21 { 22 case ALLEGRO_KEY_ENTER: 23 //pseudo : change player one's variable 24 enter_key_pressed = true; 25 time_of_enter_keypress = al_get_timer_count(timer); 26 } 27..... 28} 29 30/* process input : */ 31 32if(enter_key_pressed == true && al_get_timer_count(timer) > time_of_enter_keypress + PLAYER_2_ATTACK_DELAY_TIME) 33{ 34 //pseudo : change player two's variable. 35 36} 37 38pseudo : end of main loop (i.e. go back to the beginning).


If you don't get the general idea, I can try to explain it better. It's not very well explained. I just thought you might get the right idea from it.

Chris Katko
Member #1,881
January 2002

this is the only way to do it. start a timer when you trigger an event, and process it afterward.

You can technically could track a timer using frames instead of real-world time. But if you don't understand basic timers yet, it's a waste of time [ha!] to explain and understand it.

Another problem is any time you trigger a function when someone presses a keyboard key, someone can simply press the key infinite times regardless of framerate. So if they have an autokey generator they can fire a gun infinitely, for example. [That's also how "turbo" controllers worked on old videogame systems] The solution is, instead of triggering a timer directly from a key, you separate key sampling from the actions of those keys into two different functions.

The proper way to sample keyboard is follows:

1//pseudo code. NOT REAL CODE. 2 3int storedKeys[256]; 4 5oneFrameOfGame() 6 { 7 sampleKeys() //normal allegro key checks [see below] 8 reactFromKeys() // react to those keys 9 drawGame() // draw the game 10 //repeat 11 } 12 13void sampleKeys() 14 { 15 storedKeys[0 to 255] = 0; //reset values 16 17 //mark any keys that are down 18 if(key[KEY_ESC]) storedKeys[KEY_ESC] = 1; 19 if(key[KEY_UP]) storedKeys[KEY_UP] = 1; 20 //etc ... 21 //or if using allegro 5 timers, this can be integrated into the Allegro 5 event function [see previous posters code]. 22 } 23 24void reactFromKeys() 25 { 26 if(storedKeys[KEY_UP] == true) 27 { 28 moveCharacterUp(); //note here that moveCharacterUp can ONLY be called once per frame. It has been "decoupled" from the amount of times you pressed UP. It can only "react" once. 29 } 30 } 31 32}

This isn't just for preventing cheating, because this proper way also means anyone with different computer speeds will have the same speed and reactions in game. Without it, even if the game slows down in sections, the effects from the same keyboard inputs will change.

In the case of the previous posters code, you would mark a key press whenever it hits (in the allegro event code), and then react to it by starting the timer once during the proper game loop. You only want reactions to trigger once for a frame of game.

“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007

Go to: