BHXSpecter

Scrolling Text with Allegro

Jun 27th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_image.h>
  3. #include <allegro5/allegro_font.h>
  4. #include <allegro5/allegro_ttf.h>
  5. #include <allegro5/allegro_primitives.h>
  6.  
  7. #include <iostream>
  8. #include <string>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. const int SCREEN_W = 640;
  14. const int SCREEN_H = 480;
  15. const float FPS = 60.0;
  16. const int yVel = 50;
  17.  
  18. int main()
  19. {
  20.     int fps = 0, fps_accum = 0;
  21.     double fps_time = 0;
  22.     int lvlX = SCREEN_W / 2 - 24, lvlY = 0, lvlNum = 1;
  23.     string level = "Level ";
  24.     ofstream outfile;
  25.     outfile.open("level.out");
  26.     if(!al_init())
  27.     {
  28.         cout << "Failed to init allegro\n";
  29.     }
  30.     if(!al_install_keyboard())
  31.     {
  32.         cout << "Failed to install keyboad\n";
  33.     }
  34.  
  35.     if(!al_init_primitives_addon())
  36.     {
  37.         cout << "Failed to init primitives addon\n";
  38.     }
  39.  
  40.     if(!al_init_image_addon())
  41.     {
  42.         cout << "Failed to init image addon\n";
  43.     }
  44.  
  45.     al_init_font_addon();
  46.     al_init_ttf_addon();
  47.  
  48.     ALLEGRO_DISPLAY *display;
  49.     ALLEGRO_EVENT_QUEUE *event_queue;
  50.     ALLEGRO_TIMER *timer;
  51.     ALLEGRO_FONT *fnt, *lvltxt;
  52.  
  53.         fnt = al_load_ttf_font("pirulen.ttf", 12, 0);
  54.         lvltxt = fnt;
  55.  
  56.     bool doexit = false;
  57.     bool redraw = false;
  58.  
  59.     timer = al_create_timer(1.0/FPS);
  60.     display = al_create_display(SCREEN_W, SCREEN_H);
  61.     event_queue = al_create_event_queue();
  62.     //al_set_display_flag(display, ALLEGRO_FULLSCREEN_WINDOW, 1);
  63.     al_set_window_title(display, "Scrolling Text");
  64.     al_set_window_position(display, 20, 20);
  65.  
  66.     al_register_event_source(event_queue, al_get_display_event_source(display));
  67.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  68.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  69.  
  70.     al_clear_to_color(al_map_rgb(0, 0, 0));
  71.     al_flip_display();
  72.     al_start_timer(timer);
  73.  
  74.     while(!doexit)
  75.     {
  76.         ALLEGRO_EVENT ev;
  77.         al_wait_for_event(event_queue, &ev);
  78.  
  79.         if(ev.type == ALLEGRO_EVENT_TIMER)
  80.         {
  81.             // logic
  82.             if(lvlY < 0 || lvlY > SCREEN_H)
  83.             {
  84.                 lvlY = 0;
  85.                 outfile << level << " " << lvlNum << endl;
  86.                 lvlNum++;
  87.             }
  88.             else lvlY += yVel;
  89.  
  90.  
  91.             redraw = true;
  92.         }
  93.         else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  94.         {
  95.             break;
  96.         }
  97.         else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
  98.         {
  99.             switch(ev.keyboard.keycode)
  100.             {
  101.                 break;
  102.             }
  103.         }
  104.         else if(ev.type == ALLEGRO_EVENT_KEY_UP)
  105.         {
  106.             switch(ev.keyboard.keycode)
  107.             {
  108.                 case ALLEGRO_KEY_ESCAPE:
  109.                     doexit = true;
  110.                     break;
  111.             }
  112.         }
  113.  
  114.         if(redraw && al_is_event_queue_empty(event_queue))
  115.         {
  116.             redraw = false;
  117.             al_clear_to_color(al_map_rgb(0, 0, 0));
  118.             double t = al_get_time();
  119.             // draw map code here
  120.             al_draw_filled_rounded_rectangle(4, 4, 100, 22, 8, 8, al_map_rgb(255, 0, 255));
  121.             al_draw_textf(fnt, al_map_rgb(255, 255, 255),  8,  8, 0, "FPS: %d", fps);
  122.             al_draw_textf(lvltxt, al_map_rgb(255, 255, 255), lvlX, lvlY, 0, "Level %d", lvlNum);
  123.             al_flip_display();
  124.  
  125.             fps_accum++;
  126.             if(t - fps_time >= 1)
  127.             {
  128.                 fps = fps_accum;
  129.                 fps_accum = 0;
  130.                 fps_time = t;
  131.             }
  132.         }
  133.     }
  134.  
  135.     outfile.close();
  136.  
  137.     return 0;
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment