Advertisement
KeithS

Mouse area detect

Jun 11th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | None | 0 0
  1. /*
  2. Mouse area select
  3. API: Allegro 5.1
  4. KeithS
  5. June 2012
  6. */
  7. #include <allegro5/allegro.h>
  8. #include <allegro5/allegro_primitives.h>
  9. #include <allegro5/allegro_font.h>
  10. #include <allegro5/allegro_ttf.h>
  11. #include <cstdio>
  12. #include <string>
  13.  
  14.  
  15. using namespace std;
  16.  
  17. const int SCREEN_X = 800;
  18. const int SCREEN_Y = 600;
  19. const float fps = 20.0f;
  20.  
  21. struct mouse_area
  22. {
  23.     float x1;
  24.     float y1;
  25.     float x2;
  26.     float y2;
  27.     string label;
  28. };
  29.  
  30. //------------------FUNCTIONS------------------------------------
  31. //---------------------------------------------------------------
  32.  
  33. bool allegro5_setup(        ALLEGRO_DISPLAY **disp,
  34.                             ALLEGRO_EVENT_QUEUE **event_q,
  35.                             ALLEGRO_TIMER **f_timer,
  36.                             ALLEGRO_FONT **font)
  37. {
  38.     if(al_init() == false)
  39.     {
  40.         printf("Allegro did not initialize.\n");
  41.         return false;
  42.     }
  43.     if(al_install_mouse() == false)
  44.     {
  45.         printf("Mouse did not install.\n");
  46.         return false;
  47.     }
  48.     al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
  49.     *disp = al_create_display(SCREEN_X, SCREEN_Y);
  50.     if(*disp == NULL)
  51.     {
  52.         printf("Display not created.\n");
  53.         return false;
  54.     }
  55.     *event_q = al_create_event_queue();
  56.     if(*event_q == NULL)
  57.     {
  58.         printf("Event Queue not created.\n");
  59.         al_destroy_display(*disp);
  60.         return false;
  61.     }
  62.     *f_timer = al_create_timer(1.0f / fps);
  63.     if(*f_timer == NULL)
  64.     {
  65.         printf("Timer not created.\n");
  66.         al_destroy_display(*disp);
  67.         al_destroy_event_queue(*event_q);
  68.         return false;
  69.     }
  70.  
  71.     al_init_font_addon();
  72.     if(al_init_ttf_addon() == false)
  73.     {
  74.         printf("TTF addon not initialized.\n");
  75.         al_destroy_display(*disp);
  76.         al_destroy_event_queue(*event_q);
  77.         al_destroy_timer(*f_timer);
  78.         return false;
  79.     }
  80.     *font = al_load_ttf_font("../../fonts/Verdana.ttf", -20, NULL);
  81.     if(*font == NULL)
  82.     {
  83.         printf("TTF Font did not load. Check that it exists in /font.\n");
  84.         al_destroy_display(*disp);
  85.         al_destroy_event_queue(*event_q);
  86.         al_destroy_timer(*f_timer);
  87.         return false;
  88.     }
  89.     if(al_init_primitives_addon() == false)
  90.     {
  91.         printf("Primitives did not initialize.\n");
  92.         al_destroy_display(*disp);
  93.         al_destroy_event_queue(*event_q);
  94.         al_destroy_timer(*f_timer);
  95.         return false;
  96.     }
  97.     al_register_event_source(*event_q, al_get_display_event_source(*disp));
  98.     al_register_event_source(*event_q, al_get_mouse_event_source());
  99.     al_register_event_source(*event_q, al_get_timer_event_source(*f_timer));
  100.     return true;
  101. }
  102.  
  103. //---------------------------------------------------------------
  104.  
  105. void allegro5_terminate(        ALLEGRO_DISPLAY **disp,
  106.                                 ALLEGRO_EVENT_QUEUE **event_q,
  107.                                 ALLEGRO_TIMER **f_timer)
  108. {
  109.     al_destroy_display(*disp);
  110.     al_destroy_event_queue(*event_q);
  111.     al_destroy_timer(*f_timer);
  112. }
  113.  
  114. //---------------------------------------------------------------
  115. void set_click_areas(mouse_area *clk_ar, int SIZE)
  116. {
  117.     float h_offset = 10.0f;
  118.     float v_offset = 10.0f;
  119.     float h_width = 250.0f;
  120.     float v_height = 23.0f;
  121.     char buffer[100];
  122.     int i;
  123.  
  124.     for(i = 0; i < SIZE; i++)
  125.     {
  126.         clk_ar[i].x1 = h_offset;
  127.         clk_ar[i].y1 = (i * 25.0f) + v_offset;
  128.         clk_ar[i].x2 = h_offset + h_width;
  129.         clk_ar[i].y2 = (i * 25.0f) + v_offset + v_height;
  130.         sprintf(buffer, "Label %i", i);
  131.         clk_ar[i].label.assign(buffer);
  132.     }
  133.  
  134. }
  135. //---------------------------------------------------------------
  136. int detect_click(mouse_area *clk_ar, int SIZE, int mx, int my)
  137. {
  138.     int i;
  139.     for(i = 0; i < SIZE; i++)
  140.     {
  141.         if(mx > clk_ar[i].x1 && my > clk_ar[i].y1 && mx < clk_ar[i].x2 && my < clk_ar[i].y2)
  142.             return i;
  143.     }
  144.     return -1;
  145. }
  146. //---------------------------------------------------------------
  147. void draw_click_areas(mouse_area *clk_ar, int SIZE, int sel_ar, ALLEGRO_FONT *font)
  148. {
  149.     ALLEGRO_COLOR select_color = al_map_rgb(250,50,50);
  150.     ALLEGRO_COLOR deselect_color = al_map_rgb(150,150,150);
  151.     ALLEGRO_COLOR color = deselect_color;
  152.  
  153.     al_clear_to_color(al_map_rgb(0,0,30));
  154.  
  155.     int i;
  156.     for(i = 0; i < SIZE; i++)
  157.     {
  158.         if(i == sel_ar)
  159.             color = select_color;
  160.         else
  161.             color = deselect_color;
  162.  
  163.         al_draw_text(font, color, clk_ar[i].x1 + 3.0f, clk_ar[i].y1, ALLEGRO_ALIGN_LEFT, clk_ar[i].label.c_str());
  164.         al_draw_rectangle(clk_ar[i].x1, clk_ar[i].y1, clk_ar[i].x2, clk_ar[i].y2, color, 0);
  165.     }
  166. }
  167. //---------------------------------------------------------------
  168.  
  169. //---------------------------------------------------------------
  170. //--------------------------MAIN---------------------------------
  171. //---------------------------------------------------------------
  172.  
  173. int main(int argc, char *argv[])
  174. {
  175.     ALLEGRO_DISPLAY *display = NULL;
  176.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  177.     ALLEGRO_TIMER *timer = NULL;
  178.     ALLEGRO_FONT *font_1 = NULL;
  179.  
  180.     bool draw = false;
  181.     bool loop = true;
  182.     bool mouse_b = false;
  183.     int m_x = 0;
  184.     int m_y = 0;
  185.     int M_AREAS = 10;
  186.     int selected_area = -1;
  187.  
  188.     mouse_area click_area[M_AREAS];
  189.     set_click_areas(click_area, M_AREAS);
  190.  
  191.     if(allegro5_setup(&display, &event_queue, &timer, &font_1) == false)
  192.         return -1;
  193.  
  194.     al_start_timer(timer);
  195.  
  196.     while(loop == true)
  197.     {
  198.         ALLEGRO_EVENT event;
  199.         al_wait_for_event(event_queue, &event);
  200.  
  201.         switch(event.type)
  202.         {
  203.             case ALLEGRO_EVENT_DISPLAY_CLOSE:
  204.                 loop = false;
  205.                 break;
  206.             case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
  207.                 m_x = event.mouse.x;
  208.                 m_y = event.mouse.y;
  209.                 mouse_b = true;
  210.                 selected_area = detect_click(click_area, M_AREAS, m_x, m_y);
  211.                 break;
  212.             case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
  213.                 mouse_b = false;
  214.                 break;
  215.             case ALLEGRO_EVENT_TIMER:
  216.                 draw = true;
  217.                 break;
  218.             default:
  219.                 break;
  220.         }
  221.         if(draw == true && al_event_queue_is_empty(event_queue))
  222.         {
  223.             draw_click_areas(click_area, M_AREAS, selected_area, font_1);
  224.             al_flip_display();
  225.             draw = false;
  226.         }
  227.     }
  228.  
  229.     return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement