Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Key-presses speed-up program

Credits go to torhu for helping out!
This thread is locked; no one can reply to it. rss feed Print
Key-presses speed-up program
thatrabbit
Member #18,225
August 2020

Hello. I'm on a Macbook Air running OS 10.14.6 (Mojave).

#SelectExpand
1$ pkg-config allegro-5 --modversion 25.2.8

Below, when line-17 is ignored, the program runs as I expect.

When line-17 is executed, the program 'speeds up' in response to pressing keyboard keys, and holding keys down.

#SelectExpand
1#include <allegro5/allegro5.h> 2#include <allegro5/display.h> 3#include <allegro5/allegro_primitives.h> 4 5int main() 6{ 7 int offset = 0; 8 int direction = 1; 9 10 al_init(); 11 al_install_keyboard(); 12 13 ALLEGRO_TIMER* timer = al_create_timer(1.0 / 24); 14 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 15 ALLEGRO_DISPLAY* disp = al_create_display(320, 200); 16 17 // al_register_event_source(queue, al_get_keyboard_event_source()); 18 al_register_event_source(queue, al_get_display_event_source(disp)); 19 al_register_event_source(queue, al_get_timer_event_source(timer)); 20 21 bool redraw = true; 22 ALLEGRO_EVENT event; 23 24 al_start_timer(timer); 25 26 while(1) 27 { 28 al_wait_for_event(queue, &event); 29 30 if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 31 break; 32 } 33 34 if(event.type == ALLEGRO_EVENT_TIMER) { 35 redraw = true; 36 } 37 38 offset += 1 * direction; 39 40 if(offset > 5) { 41 direction = -1; 42 } else if(offset < -5) { 43 direction = 1; 44 } 45 46 if(redraw && al_is_event_queue_empty(queue)) 47 { 48 al_clear_to_color(al_map_rgb(0, 0, 0)); 49 50 al_draw_filled_rectangle(110, 90 + offset, 210, 110 + offset, al_map_rgb(48, 102, 219)); 51 52 al_flip_display(); 53 54 redraw = false; 55 } 56 } 57 58 59 al_destroy_display(disp); 60 al_destroy_timer(timer); 61 al_destroy_event_queue(queue); 62 63 return 0; 64}

The 'speed up' can be observed in the movement of the rectangle in relation to keyboard interaction.

I have set OS-X's "Key Repeat" to "Off", and the Allegro behavior remains. Also, the keyboard repeats as per usual; not sure what that setting is meant to do.

Any ideas?

torhu
Member #2,727
September 2002
avatar

Because any event causes al_wait_for_event to return, meaning offset is increased every time you press or release a key.

If you print something every time you get a KEY_UP, KEY_DOWN, or KEY_CHAR event, you will see what happens.

thatrabbit
Member #18,225
August 2020

torhu said:

Because any event causes al_wait_for_event to return...

I see what you mean.

I put my platform-shifting code into the ALLEGRO_EVENT_TIMER branch, and it now behaves as I expect.

Thank-you torhu.

Chris Katko
Member #1,881
January 2002
avatar

Basically, in any game, what I do is this:

int keys[256]; // global, or somewhere else

// in allegro_key_down of events
keys[event.keyboard.keycode] = true;

// in allegro_key_up of events
keys[event.keyboard.keycode] = false;

Then, every "frame" of the game, I have a logic(); and draw(); function for every object. All objects.logic() are called, then all objects.draw() are called.

In logic(), an object will check if a key is down that it cares about:

if(keys[ALLEGRO_KEY_DOWN]) // down arrow
{
// move guy downward
position_y += 20;
}

then finally the draw() code gets called.

We've "decoupled" a bunch of unrelated things. Logic and drawing are separate. And object logic is separate from the act of handling whether or not a key has been pressed (controller logic). Objects don't respond to keyboard events, they only check whether a specific flag has been set.

That way, objects can never react to the same event twice by accident (otherwise, if you press a key faster, your object can go faster in a frame).

Instead of directly checking ALLEGRO_KEY_DOWN, you can also have generic actionUp, actionDown, actionLeft, actionRight functions in an object. Then those functions call whatever "key" they are pointed to. So now you can have two or more objects, and they check their "up" key respectively without having to write more code.

-----sig:
“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

Go to: