Advertisement
tourniquet

Allegro Mouse Events Lesson 5

Jan 30th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 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. #include <allegro5/allegro.h>
  5. #include <allegro5/allegro_primitives.h>
  6.  
  7. int main() {
  8.  
  9.     ALLEGRO_DISPLAY *display;
  10.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  11.  
  12.     if(!al_init()) {
  13.         return -1;
  14.     }
  15.  
  16.     int win_x = 400;
  17.     int win_y = 400;
  18.  
  19.     display = al_create_display(win_x, win_y);
  20.  
  21.     if(!display) {
  22.         return -1;
  23.     }
  24.  
  25.     al_init_primitives_addon();
  26.     al_install_mouse();
  27.  
  28.     event_queue = al_create_event_queue();
  29.     al_register_event_source(event_queue, al_get_mouse_event_source());
  30.     al_register_event_source(event_queue, al_get_display_event_source(display));
  31.  
  32.     bool user_exit = false;
  33.  
  34.     while(!user_exit) {
  35.         ALLEGRO_EVENT ev;
  36.         al_wait_for_event(event_queue, &ev);
  37.  
  38.         // to close the program with 'x' button of window, need to initialize al_register_event_source
  39.         if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  40.             user_exit = true;
  41.         }
  42.  
  43.         int pos_x;
  44.         int pos_y;
  45.         bool draw = true;
  46.  
  47.         if(ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
  48.             pos_x = ev.mouse.x;
  49.             pos_y = ev.mouse.y;
  50.         } else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
  51.             if(ev.mouse.button) {
  52.                 draw = !draw;
  53.             }
  54.         }
  55.  
  56.         if(draw) {
  57.             al_hide_mouse_cursor(display);
  58.             al_draw_filled_circle(pos_x, pos_y, 10, al_map_rgb(0, 255,0));
  59.         } else {
  60.             al_show_mouse_cursor(display);
  61.         }
  62.  
  63.         al_flip_display();
  64.         al_clear_to_color(al_map_rgb(24, 24, 24));
  65.     }
  66.  
  67.     al_destroy_event_queue(event_queue);
  68.     al_uninstall_keyboard();
  69.     al_destroy_display(display);
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement