Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Mouse area select
- API: Allegro 5.1
- KeithS
- June 2012
- */
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_ttf.h>
- #include <cstdio>
- #include <string>
- using namespace std;
- const int SCREEN_X = 800;
- const int SCREEN_Y = 600;
- const float fps = 20.0f;
- struct mouse_area
- {
- float x1;
- float y1;
- float x2;
- float y2;
- string label;
- };
- //------------------FUNCTIONS------------------------------------
- //---------------------------------------------------------------
- bool allegro5_setup( ALLEGRO_DISPLAY **disp,
- ALLEGRO_EVENT_QUEUE **event_q,
- ALLEGRO_TIMER **f_timer,
- ALLEGRO_FONT **font)
- {
- if(al_init() == false)
- {
- printf("Allegro did not initialize.\n");
- return false;
- }
- if(al_install_mouse() == false)
- {
- printf("Mouse did not install.\n");
- return false;
- }
- al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
- *disp = al_create_display(SCREEN_X, SCREEN_Y);
- if(*disp == NULL)
- {
- printf("Display not created.\n");
- return false;
- }
- *event_q = al_create_event_queue();
- if(*event_q == NULL)
- {
- printf("Event Queue not created.\n");
- al_destroy_display(*disp);
- return false;
- }
- *f_timer = al_create_timer(1.0f / fps);
- if(*f_timer == NULL)
- {
- printf("Timer not created.\n");
- al_destroy_display(*disp);
- al_destroy_event_queue(*event_q);
- return false;
- }
- al_init_font_addon();
- if(al_init_ttf_addon() == false)
- {
- printf("TTF addon not initialized.\n");
- al_destroy_display(*disp);
- al_destroy_event_queue(*event_q);
- al_destroy_timer(*f_timer);
- return false;
- }
- *font = al_load_ttf_font("../../fonts/Verdana.ttf", -20, NULL);
- if(*font == NULL)
- {
- printf("TTF Font did not load. Check that it exists in /font.\n");
- al_destroy_display(*disp);
- al_destroy_event_queue(*event_q);
- al_destroy_timer(*f_timer);
- return false;
- }
- if(al_init_primitives_addon() == false)
- {
- printf("Primitives did not initialize.\n");
- al_destroy_display(*disp);
- al_destroy_event_queue(*event_q);
- al_destroy_timer(*f_timer);
- return false;
- }
- al_register_event_source(*event_q, al_get_display_event_source(*disp));
- al_register_event_source(*event_q, al_get_mouse_event_source());
- al_register_event_source(*event_q, al_get_timer_event_source(*f_timer));
- return true;
- }
- //---------------------------------------------------------------
- void allegro5_terminate( ALLEGRO_DISPLAY **disp,
- ALLEGRO_EVENT_QUEUE **event_q,
- ALLEGRO_TIMER **f_timer)
- {
- al_destroy_display(*disp);
- al_destroy_event_queue(*event_q);
- al_destroy_timer(*f_timer);
- }
- //---------------------------------------------------------------
- void set_click_areas(mouse_area *clk_ar, int SIZE)
- {
- float h_offset = 10.0f;
- float v_offset = 10.0f;
- float h_width = 250.0f;
- float v_height = 23.0f;
- char buffer[100];
- int i;
- for(i = 0; i < SIZE; i++)
- {
- clk_ar[i].x1 = h_offset;
- clk_ar[i].y1 = (i * 25.0f) + v_offset;
- clk_ar[i].x2 = h_offset + h_width;
- clk_ar[i].y2 = (i * 25.0f) + v_offset + v_height;
- sprintf(buffer, "Label %i", i);
- clk_ar[i].label.assign(buffer);
- }
- }
- //---------------------------------------------------------------
- int detect_click(mouse_area *clk_ar, int SIZE, int mx, int my)
- {
- int i;
- for(i = 0; i < SIZE; i++)
- {
- if(mx > clk_ar[i].x1 && my > clk_ar[i].y1 && mx < clk_ar[i].x2 && my < clk_ar[i].y2)
- return i;
- }
- return -1;
- }
- //---------------------------------------------------------------
- void draw_click_areas(mouse_area *clk_ar, int SIZE, int sel_ar, ALLEGRO_FONT *font)
- {
- ALLEGRO_COLOR select_color = al_map_rgb(250,50,50);
- ALLEGRO_COLOR deselect_color = al_map_rgb(150,150,150);
- ALLEGRO_COLOR color = deselect_color;
- al_clear_to_color(al_map_rgb(0,0,30));
- int i;
- for(i = 0; i < SIZE; i++)
- {
- if(i == sel_ar)
- color = select_color;
- else
- color = deselect_color;
- al_draw_text(font, color, clk_ar[i].x1 + 3.0f, clk_ar[i].y1, ALLEGRO_ALIGN_LEFT, clk_ar[i].label.c_str());
- al_draw_rectangle(clk_ar[i].x1, clk_ar[i].y1, clk_ar[i].x2, clk_ar[i].y2, color, 0);
- }
- }
- //---------------------------------------------------------------
- //---------------------------------------------------------------
- //--------------------------MAIN---------------------------------
- //---------------------------------------------------------------
- int main(int argc, char *argv[])
- {
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_TIMER *timer = NULL;
- ALLEGRO_FONT *font_1 = NULL;
- bool draw = false;
- bool loop = true;
- bool mouse_b = false;
- int m_x = 0;
- int m_y = 0;
- int M_AREAS = 10;
- int selected_area = -1;
- mouse_area click_area[M_AREAS];
- set_click_areas(click_area, M_AREAS);
- if(allegro5_setup(&display, &event_queue, &timer, &font_1) == false)
- return -1;
- al_start_timer(timer);
- while(loop == true)
- {
- ALLEGRO_EVENT event;
- al_wait_for_event(event_queue, &event);
- switch(event.type)
- {
- case ALLEGRO_EVENT_DISPLAY_CLOSE:
- loop = false;
- break;
- case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
- m_x = event.mouse.x;
- m_y = event.mouse.y;
- mouse_b = true;
- selected_area = detect_click(click_area, M_AREAS, m_x, m_y);
- break;
- case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
- mouse_b = false;
- break;
- case ALLEGRO_EVENT_TIMER:
- draw = true;
- break;
- default:
- break;
- }
- if(draw == true && al_event_queue_is_empty(event_queue))
- {
- draw_click_areas(click_area, M_AREAS, selected_area, font_1);
- al_flip_display();
- draw = false;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement