Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- MOCK UP:
- Scrollable tags with selectable element
- (keyboard driven version)
- API: Allegro 5.1
- KeithS, June, 2012
- Link options...
- `pkg-config --libs allegro-5.1 allegro_font-5.1 allegro_ttf-5.1 allegro_primitives-5.1`
- */
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_primitives.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_ttf.h>
- #include <cstdio>
- //#include <math.h>
- #include <vector>
- //#include <ctime>
- #include <string>
- //#define _USE_MATH_DEFINES
- //#define RAD_TO_DEG(x) (x)*(180.0/M_PI)
- //#define DEG_TO_RAD(x) (x)*(M_PI/180.0)
- //#define KT_TO_MS(x) (x) * 0.51444
- //#define MS_TO_KT(x) (x) * 1.94386
- //------------------------------------------------------------------
- using namespace std;
- const double fps = 20.0f;
- const int SCREEN_X = 800;
- const int SCREEN_Y = 600;
- const int LINES = 20;
- enum keys{KEY_Q, KEY_A, KEY_UP, KEY_DOWN};
- //------------------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 5 did not initialize\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("Dislplay was not created\n");
- return false;
- }
- *event_q = al_create_event_queue();
- if(*event_q == NULL)
- {
- al_destroy_display(*disp);
- printf("Event Queue was not created\n");
- return false;
- }
- *f_timer = al_create_timer(1.0f / fps);
- if(*f_timer == NULL)
- {
- al_destroy_display(*disp);
- al_destroy_event_queue(*event_q);
- printf("Timer F was not created\n");
- return false;
- }
- if(al_init_primitives_addon() == false)
- {
- al_destroy_display(*disp);
- al_destroy_timer(*f_timer);
- al_destroy_event_queue(*event_q);
- printf("Primitives Addon did not initialize\n");
- return false;
- }
- if(al_install_keyboard() == false)
- {
- al_destroy_display(*disp);
- al_destroy_timer(*f_timer);
- al_destroy_event_queue(*event_q);
- printf("Keyboard not installed\n");
- return false;
- }
- al_init_font_addon();
- if(al_init_ttf_addon() == false)
- {
- al_destroy_display(*disp);
- al_destroy_timer(*f_timer);
- al_destroy_event_queue(*event_q);
- printf("TTF addon did not initialize\n");
- return false;
- }
- *font = al_load_ttf_font("./fonts/Verdana.ttf", -22, NULL);
- if(*font == NULL)
- {
- al_destroy_display(*disp);
- al_destroy_timer(*f_timer);
- al_destroy_event_queue(*event_q);
- printf("TTF font did not load. Check that it exists in /Release/font/ dir?\n");
- return false;
- }
- al_register_event_source(*event_q, al_get_display_event_source(*disp));
- al_register_event_source(*event_q, al_get_keyboard_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_event_queue(*event_q);
- al_uninstall_keyboard();
- al_destroy_timer(*f_timer);
- al_destroy_display(*disp);
- }
- //------------------------------------------------------------------
- void key_down_handler(bool *keys, ALLEGRO_EVENT ev)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_Q:
- keys[KEY_Q] = true;
- break;
- case ALLEGRO_KEY_A:
- keys[KEY_A] = true;
- break;
- case ALLEGRO_KEY_UP:
- keys[KEY_UP] = true;
- break;
- case ALLEGRO_KEY_DOWN:
- keys[KEY_DOWN] = true;
- break;
- default:
- break;
- }
- }
- //------------------------------------------------------------------
- void key_up_handler(bool *keys, ALLEGRO_EVENT ev)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_Q:
- keys[KEY_Q] = false;
- break;
- case ALLEGRO_KEY_A:
- keys[KEY_A] = false;
- break;
- case ALLEGRO_KEY_UP:
- keys[KEY_UP] = false;
- break;
- case ALLEGRO_KEY_DOWN:
- keys[KEY_DOWN] = false;
- break;
- default:
- break;
- }
- }
- //------------------------------------------------------------------
- void key_event_handler(bool *keys, int *sel_tag, int vec_size, int *start, int max)
- {
- if(keys[KEY_Q] == true)
- {
- if(*start > 0)
- *start -= 1;
- return;
- }
- if(keys[KEY_A] == true)
- {
- if(*start < max)
- *start += 1;
- return;
- }
- if(keys[KEY_UP] == true)
- {
- if(*sel_tag > 0)
- *sel_tag -= 1;
- return;
- }
- if(keys[KEY_DOWN] == true)
- {
- if(*sel_tag < (vec_size - 1))
- *sel_tag += 1;
- return;
- }
- }
- //------------------------------------------------------------------
- void assign_vector_tags(vector<string> &tags, int vec_size, char *buffer)
- {
- int i;
- for(i = 0; i < vec_size; i++) // for(i = 0; i < (int)tags.size(); i++) ... would be just as good here.
- {
- sprintf(buffer, "This is tag number %i", i);
- tags.push_back(buffer);
- }
- }
- //------------------------------------------------------------------
- void display_tags(vector<string> tags, int vec_size, ALLEGRO_FONT *font, int sel_tag, int *start)
- {
- ALLEGRO_COLOR selected = al_map_rgb(250, 50, 50);
- ALLEGRO_COLOR deselected = al_map_rgb(150, 150, 150);
- ALLEGRO_COLOR color = deselected;
- float v_offset = SCREEN_Y * 0.075f;
- al_clear_to_color(al_map_rgb(0,0,30));
- string tag_display[LINES];
- int LINES_LIMIT;
- if(LINES > vec_size)
- {
- // If there are less elements in the vector than lines to print it...
- LINES_LIMIT = vec_size;
- *start = 0; // Just in case that variable came as some other value. It would cause some mayhem if it did.
- }
- else
- LINES_LIMIT = LINES;
- int i;
- for(i = 0; i < LINES_LIMIT; i++)
- {
- tag_display[i].assign(tags.at(i + (size_t)*start)); // I am uncomfortable with typecasting the pointer.
- }
- for(i = 0; i < LINES_LIMIT; i++)
- {
- if(sel_tag == (i + *start))
- color = selected;
- else
- color = deselected;
- al_draw_text(font, color, float(SCREEN_X) * 0.05f, (i * 25.0f) + v_offset, ALLEGRO_ALIGN_LEFT, tag_display[i].c_str());
- }
- }
- //------------------MAIN--------------------------------------------
- //------------------------------------------------------------------
- int main(int argc, char *argv[])
- {
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_TIMER *fps_timer = NULL;
- ALLEGRO_FONT *font_1 = NULL;
- bool keys[4] = {false, false, false, false};
- bool draw = false;
- bool loop = true;
- char text_buffer[256];
- vector<string> vec_tags;
- int VEC_SIZE = 70;//35; //12;
- vec_tags.reserve(VEC_SIZE);
- int selected_tag = 7; // This needs some error trapping somewhere...
- int start_at_line = 4;
- int max_start_at = VEC_SIZE - LINES; // To avoid running off the end of the vector.
- if(allegro5_setup(&display, &event_queue, &fps_timer, &font_1) == false)
- return -1;
- al_set_window_title(display, *argv);
- al_start_timer(fps_timer);
- assign_vector_tags(vec_tags, VEC_SIZE, text_buffer);
- while(true == loop)
- {
- ALLEGRO_EVENT event;
- al_wait_for_event(event_queue, &event);
- switch(event.type)
- {
- case ALLEGRO_EVENT_TIMER:
- draw = true;
- key_event_handler(keys, &selected_tag, VEC_SIZE, &start_at_line, max_start_at);
- break;
- case ALLEGRO_EVENT_DISPLAY_CLOSE:
- loop = false;
- break;
- case ALLEGRO_EVENT_KEY_DOWN:
- key_down_handler(keys, event);
- break;
- case ALLEGRO_EVENT_KEY_UP:
- key_up_handler(keys, event);
- break;
- default:
- break;
- }
- if(al_event_queue_is_empty(event_queue) && true == draw)
- {
- display_tags(vec_tags, VEC_SIZE, font_1, selected_tag, &start_at_line);
- al_flip_display();
- draw = false;
- }
- }
- al_stop_timer(fps_timer);
- allegro5_terminate(&display, &event_queue, &fps_timer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement