Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_font.h>
- #include <allegro5/allegro_ttf.h>
- #include <cstdio>
- // `pkg-config --libs allegro-5.1 allegro_font-5.1 allegro_ttf-5.1`
- using namespace std;
- const int SCREEN_X = 800;
- const int SCREEN_Y = 600;
- const float fps = 60.0f;
- //---------------------------------------------------------------
- //------------------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;
- }
- al_set_window_title(*disp, "Double event loops test");
- *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("Verdana.ttf", -20, NULL);
- if(*font == NULL)
- {
- printf("TTF Font did not load. Check that it exists.\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,
- ALLEGRO_FONT **font)
- {
- printf("\nAllegro 5.1 Termination accessed\n");
- al_destroy_display(*disp);
- al_destroy_event_queue(*event_q);
- al_destroy_timer(*f_timer);
- al_destroy_font(*font);
- }
- //---------------------------------------------------------------
- //---------------------------------------------------------------
- bool print_message(ALLEGRO_FONT *font)
- {
- bool second_loop = true;
- ALLEGRO_EVENT_QUEUE *event_queue_2 = NULL;
- event_queue_2 = al_create_event_queue();
- if(event_queue_2 == NULL)
- {
- printf("Event Queue not created.\n");
- return false;
- }
- al_register_event_source(event_queue_2, al_get_mouse_event_source());
- al_clear_to_color(al_map_rgb(100,0,100));
- al_draw_text(font, al_map_rgb(255,255,255), 400.0f, 300.0f, ALLEGRO_ALIGN_CENTER, "HERE'S THE TEST. CLCIK MOUSE TO CONTINUE.");
- al_flip_display();
- while(second_loop == true)
- {
- ALLEGRO_EVENT event_2;
- al_wait_for_event(event_queue_2, &event_2);
- switch(event_2.type)
- {
- case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
- second_loop = false;
- break;
- default:
- break;
- }
- }
- al_destroy_event_queue(event_queue_2);
- return true;
- }
- //---------------------------------------------------------------
- bool draw_screen(ALLEGRO_FONT *font)
- {
- static int r = 0;
- al_clear_to_color(al_map_rgb(r, 0, 0));
- al_flip_display();
- r++;
- if(r > 255) // When we get maximum red value
- {
- if(print_message(font) == false)
- {
- printf("\nNo, it did not work\n");
- return false;
- }
- r = 0;
- }
- return true;
- }
- //---------------------------------------------------------------
- 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 first_loop = true;
- bool draw = false;
- if(allegro5_setup(&display, &event_queue, &timer, &font_1) == false)
- {
- printf("\nProgram crashed out.\n\n");
- return -1;
- }
- al_start_timer(timer);
- while(first_loop == true)
- {
- ALLEGRO_EVENT event;
- al_wait_for_event(event_queue, &event);
- switch(event.type)
- {
- case ALLEGRO_EVENT_DISPLAY_CLOSE:
- first_loop = false;
- break;
- case ALLEGRO_EVENT_TIMER:
- draw = true;
- break;
- default:
- break;
- }
- if(draw == true && al_event_queue_is_empty(event_queue))
- {
- if(draw_screen(font_1) == false)
- first_loop = false;
- draw = false;
- }
- }
- al_stop_timer(timer);
- allegro5_terminate(&display, &event_queue, &timer, &font_1);
- printf("\nApparently a Normal Termination of the Program was executed.\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement