Advertisement
tourniquet

Allegro Input Keyboard Event Lesson 4

Jan 30th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. // gcc -Wall 4.c -o 4 $(pkg-config --libs allegro-5.0 allegro_image-5.0 allegro_primitives-5.0)
  2.  
  3. #define ALLEGRO_STATICLINK
  4.  
  5. #include <allegro5/allegro.h>
  6. #include <allegro5/allegro_primitives.h>
  7.  
  8. int main() {
  9.  
  10.     int win_x = 400;
  11.     int win_y = 400;
  12.     bool user_exit = false;
  13.  
  14.     int pos_x = win_x / 2;
  15.     int pos_y = win_y / 2;
  16.  
  17.     ALLEGRO_DISPLAY *display;
  18.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  19.  
  20.     if(!al_init()) {
  21.         return -1;
  22.     }
  23.  
  24.     display = al_create_display(win_x, win_y);
  25.  
  26.     if(!display) {
  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.  
  36.     while(!user_exit) {
  37.         ALLEGRO_EVENT ev;
  38.         al_wait_for_event(event_queue, &ev);
  39.  
  40.         if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
  41.             switch(ev.keyboard.keycode) {
  42.                 case ALLEGRO_KEY_UP:
  43.                     pos_y -= 20;
  44.                     break;
  45.                 case ALLEGRO_KEY_DOWN:
  46.                     pos_y += 20;
  47.                     break;
  48.                 case ALLEGRO_KEY_RIGHT:
  49.                     pos_x += 20;
  50.                     break;
  51.                 case ALLEGRO_KEY_LEFT:
  52.                     pos_x -= 20;
  53.             }
  54.  
  55.         } else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
  56.                 if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
  57.                 user_exit = true;
  58.         }  
  59.     }
  60.  
  61.         al_draw_filled_rectangle(pos_x, pos_y, pos_x + 50, pos_y + 50, al_map_rgb(0, 0, 255));
  62.         al_flip_display();
  63.         al_clear_to_color(al_map_rgb(0, 0, 0));
  64.  
  65.     al_destroy_event_queue(event_queue);
  66.     al_uninstall_keyboard();
  67.     al_destroy_display(display);
  68.  
  69.     return 0;
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement