Advertisement
KeithS

Double Event Loop A5

Aug 13th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_font.h>
  3. #include <allegro5/allegro_ttf.h>
  4. #include <cstdio>
  5.  
  6. // `pkg-config --libs allegro-5.1 allegro_font-5.1 allegro_ttf-5.1`
  7.  
  8. using namespace std;
  9.  
  10. const int SCREEN_X = 800;
  11. const int SCREEN_Y = 600;
  12. const float fps = 60.0f;
  13.  
  14. //---------------------------------------------------------------
  15. //------------------FUNCTIONS------------------------------------
  16. //---------------------------------------------------------------
  17.  
  18. bool allegro5_setup(        ALLEGRO_DISPLAY **disp,
  19.                             ALLEGRO_EVENT_QUEUE **event_q,
  20.                             ALLEGRO_TIMER **f_timer,
  21.                             ALLEGRO_FONT **font)
  22. {
  23.     if(al_init() == false)
  24.     {
  25.         printf("Allegro did not initialize.\n");
  26.         return false;
  27.     }
  28.     if(al_install_mouse() == false)
  29.     {
  30.         printf("Mouse did not install.\n");
  31.         return false;
  32.     }
  33.     al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
  34.     *disp = al_create_display(SCREEN_X, SCREEN_Y);
  35.     if(*disp == NULL)
  36.     {
  37.         printf("Display not created.\n");
  38.         return false;
  39.     }
  40.     al_set_window_title(*disp, "Double event loops test");
  41.     *event_q = al_create_event_queue();
  42.     if(*event_q == NULL)
  43.     {
  44.         printf("Event Queue not created.\n");
  45.         al_destroy_display(*disp);
  46.         return false;
  47.     }
  48.     *f_timer = al_create_timer(1.0f / fps);
  49.     if(*f_timer == NULL)
  50.     {
  51.         printf("Timer not created.\n");
  52.         al_destroy_display(*disp);
  53.         al_destroy_event_queue(*event_q);
  54.         return false;
  55.     }
  56.  
  57.     al_init_font_addon();
  58.     if(al_init_ttf_addon() == false)
  59.     {
  60.         printf("TTF addon not initialized.\n");
  61.         al_destroy_display(*disp);
  62.         al_destroy_event_queue(*event_q);
  63.         al_destroy_timer(*f_timer);
  64.         return false;
  65.     }
  66.     *font = al_load_ttf_font("Verdana.ttf", -20, NULL);
  67.     if(*font == NULL)
  68.     {
  69.         printf("TTF Font did not load. Check that it exists.\n");
  70.         al_destroy_display(*disp);
  71.         al_destroy_event_queue(*event_q);
  72.         al_destroy_timer(*f_timer);
  73.         return false;
  74.     }
  75.  
  76.     al_register_event_source(*event_q, al_get_display_event_source(*disp));
  77.     al_register_event_source(*event_q, al_get_mouse_event_source());
  78.     al_register_event_source(*event_q, al_get_timer_event_source(*f_timer));
  79.     return true;
  80. }
  81.  
  82. //---------------------------------------------------------------
  83.  
  84. void allegro5_terminate(        ALLEGRO_DISPLAY **disp,
  85.                                 ALLEGRO_EVENT_QUEUE **event_q,
  86.                                 ALLEGRO_TIMER **f_timer,
  87.                                 ALLEGRO_FONT **font)
  88. {
  89.     printf("\nAllegro 5.1 Termination accessed\n");
  90.     al_destroy_display(*disp);
  91.     al_destroy_event_queue(*event_q);
  92.     al_destroy_timer(*f_timer);
  93.     al_destroy_font(*font);
  94. }
  95.  
  96. //---------------------------------------------------------------
  97.  
  98. //---------------------------------------------------------------
  99.  
  100. bool print_message(ALLEGRO_FONT *font)
  101. {
  102.     bool second_loop = true;
  103.  
  104.     ALLEGRO_EVENT_QUEUE *event_queue_2 = NULL;
  105.     event_queue_2 = al_create_event_queue();
  106.     if(event_queue_2 == NULL)
  107.     {
  108.         printf("Event Queue not created.\n");
  109.         return false;
  110.     }
  111.  
  112.     al_register_event_source(event_queue_2, al_get_mouse_event_source());
  113.     al_clear_to_color(al_map_rgb(100,0,100));
  114.     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.");
  115.     al_flip_display();
  116.  
  117.     while(second_loop == true)
  118.     {
  119.         ALLEGRO_EVENT event_2;
  120.         al_wait_for_event(event_queue_2, &event_2);
  121.         switch(event_2.type)
  122.         {
  123.             case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
  124.                 second_loop = false;
  125.                 break;
  126.             default:
  127.                 break;
  128.         }
  129.     }
  130.     al_destroy_event_queue(event_queue_2);
  131.     return true;
  132. }
  133.  
  134. //---------------------------------------------------------------
  135.  
  136. bool draw_screen(ALLEGRO_FONT *font)
  137. {
  138.     static int r = 0;
  139.     al_clear_to_color(al_map_rgb(r, 0, 0));
  140.     al_flip_display();
  141.     r++;
  142.     if(r > 255) // When we get maximum red value
  143.     {
  144.         if(print_message(font) == false)
  145.         {
  146.             printf("\nNo, it did not work\n");
  147.             return false;
  148.         }
  149.         r = 0;
  150.     }
  151.     return true;
  152. }
  153.  
  154.  
  155. //---------------------------------------------------------------
  156.  
  157. int main(int argc, char *argv[])
  158. {
  159.     ALLEGRO_DISPLAY *display = NULL;
  160.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  161.     ALLEGRO_TIMER *timer = NULL;
  162.     ALLEGRO_FONT *font_1 = NULL;
  163.  
  164.     bool first_loop = true;
  165.     bool draw = false;
  166.  
  167.     if(allegro5_setup(&display, &event_queue, &timer, &font_1) == false)
  168.     {
  169.         printf("\nProgram crashed out.\n\n");
  170.         return -1;
  171.     }
  172.  
  173.  
  174.     al_start_timer(timer);
  175.  
  176.     while(first_loop == true)
  177.     {
  178.         ALLEGRO_EVENT event;
  179.         al_wait_for_event(event_queue, &event);
  180.  
  181.         switch(event.type)
  182.         {
  183.             case ALLEGRO_EVENT_DISPLAY_CLOSE:
  184.                 first_loop = false;
  185.                 break;
  186.             case ALLEGRO_EVENT_TIMER:
  187.                 draw = true;
  188.                 break;
  189.             default:
  190.                 break;
  191.         }
  192.         if(draw == true && al_event_queue_is_empty(event_queue))
  193.         {
  194.             if(draw_screen(font_1) == false)
  195.                 first_loop = false;
  196.  
  197.             draw = false;
  198.         }
  199.     }
  200.  
  201.     al_stop_timer(timer);
  202.     allegro5_terminate(&display, &event_queue, &timer, &font_1);
  203.     printf("\nApparently a Normal Termination of the Program was executed.\n\n");
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement