ALLEGRO_EVENT_KEY_DOWN repeating
wdtracy

Hey guys, I'm new to Allegro and have been following some tutorials. I'm not new to game programming so I understand the concept of ALLEGRO_EVENT_KEY_DOWN and that it registers the initial keypress and not the hold. When I run my program, it registers the key being held down as continuous ALLEGRO_EVENT_KEY_DOWN events. I'm not using custom KB software that would turn trigger key presses when I hold the key down, so I am not sure what is going on. The guy in the video specifically says his doesn't repeat when the key is held down. Any ideas? Here is the code:

#SelectExpand
1#include <string> 2#include <allegro5\allegro.h> 3#include <allegro5\allegro_native_dialog.h> 4#include <allegro5\allegro_primitives.h> 5 6int main(int argc, char **argv) 7{ 8 int width = 640; 9 int height = 480; 10 11 bool done = false; 12 int pos_x = width / 2; 13 int pos_y = height / 2; 14 ALLEGRO_EVENT ev; 15 16 ALLEGRO_DISPLAY *display = NULL; 17 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 18 19 if(!al_init()) { 20 al_show_native_message_box(NULL, "Allegro Error", "Error", "Failed to initialize Allegro!", NULL, NULL); 21 return -1; 22 } 23 24 display = al_create_display(width, height); 25 if(!display) { 26 al_show_native_message_box(NULL, "Allegro Error", "Error", "Failed to create display!", NULL, NULL); 27 return -1; 28 } 29 30 al_init_primitives_addon(); 31 al_install_keyboard(); 32 33 event_queue = al_create_event_queue(); 34 al_register_event_source(event_queue, al_get_keyboard_event_source()); 35 al_register_event_source(event_queue, al_get_display_event_source(display)); 36 37 int x = 1, y = 1; 38 39 while(!done) 40 { 41 42 al_wait_for_event(event_queue, &ev); 43 44 if(ev.type = ALLEGRO_EVENT_KEY_DOWN) 45 { 46 x > width ? x = 1 : ++x; 47 y > height ? y = 1 : ++y; 48 49 switch(ev.keyboard.keycode) 50 { 51 case ALLEGRO_KEY_UP: 52 pos_y -= 10; 53 break; 54 case ALLEGRO_KEY_DOWN: 55 pos_y += 10; 56 break; 57 case ALLEGRO_KEY_RIGHT: 58 pos_x += 10; 59 break; 60 case ALLEGRO_KEY_LEFT: 61 pos_x -= 10; 62 break; 63 case ALLEGRO_KEY_ESCAPE: 64 done = true; 65 break; 66 } 67 } 68 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 69 { 70 done = true; 71 } 72 73 al_clear_to_color(al_map_rgb(0, 0, 0)); 74 al_draw_circle(pos_x, pos_y, 30, al_map_rgb(100, 180, 50), 5); 75 al_draw_filled_circle(x, y, 10, al_map_rgb(100, 180, 0)); 76 al_flip_display(); 77 } 78 79 al_destroy_display(display); 80 81 return 0; 82}

Arthur Kalliokoski

You need to enable warnings. You have an assignment on line 44 when you're trying to test for equality.

if(ev.type = ALLEGRO_EVENT_KEY_DOWN)

should be

if(ev.type == ALLEGRO_EVENT_KEY_DOWN)

wdtracy

Wow, I can't believe I missed that one. Sheesh! Thanks for the catch!

Arthur Kalliokoski

I didn't see it myself either, the compiler told me when I habitually use the -Wall switch. (Warn all errors)

t.c:44:3: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if(ev.type = ALLEGRO_EVENT_KEY_DOWN)

Thomas Fjellstrom

Confusingly -Wall doesn't actually enable all warnings. You need -Wextra as well I think.

Elias

Indeed. (In the past -Wextra was called just -W.) I always use this:

gcc -Wall -Wextra -Werror

Thomas Fjellstrom

I tend to only use -Werror in special cases. I hate getting stuck in unimportant warnings.

Arthur Kalliokoski

Actually for this program gcc was complaining about al_show_native_message_box() too, it doesn't like NULL for the flags.

Thread #614459. Printed from Allegro.cc