Advertisement
ZoriaRPG

Shadowblitz Allegro5

Oct 23rd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <string>
  2. #include <stdio>
  3. #include "allegro5/allegro.h"
  4.  
  5. ALLEGRO_BITMAP *allegro;
  6.  
  7.  
  8. ALLEGRO_EVENT_QUEUE* queue;
  9. ALLEGRO_TIMER* timer;
  10.  
  11. ALLEGRO_DISPLAY *display;
  12.  
  13.  
  14.  
  15. class App
  16. {
  17.     public:
  18.         static void init(int w = 256, int h = 240, std::string title = "Untitled", int fps = 60, bool noDisplay = false);
  19.         static void bindInit(void (*init)());
  20.         static void bindTick(void (*tick)());
  21.         static void bindDraw(void (*draw)());
  22.         static void bindFree(void (*free)());
  23.  
  24.     protected:
  25.  
  26.  
  27.     private:
  28.        
  29.         static void (*onInit)();
  30.         static void (*onTick)();
  31.         static void (*onDraw)();
  32.         static void (*onFree)();
  33.  
  34.  
  35. };
  36.  
  37.  
  38. void App::init(int w, int h, std::string title, int fps, bool noDisplay)
  39. {
  40.     if (!al_init())
  41.     {
  42.         fprintf(stderr, "failed to initalize allegro!\n");
  43.         return;
  44.     }
  45.    
  46.     al_install_keyboard();
  47.     al_install_mouse();
  48.  
  49.     if (!noDisplay)
  50.     {
  51.         bool update = false;
  52.  
  53.  
  54.         timer = al_create_timer(1.0 / fps);
  55.         if (!timer) {
  56.             fprintf(stderr, "failed to create timer!\n");
  57.             return;
  58.         }
  59.  
  60.         display = al_create_display(w, h);
  61.         if (!display)
  62.         {
  63.             fprintf(stderr, "failed to create display!\n");
  64.             if (timer != nullptr) al_destroy_timer(timer);
  65.             return;
  66.         }
  67.  
  68.         queue = al_create_event_queue();
  69.         if (!queue)
  70.         {
  71.             fprintf(stderr, "failed to create event_queue!\n");
  72.             if (timer   != nullptr) al_destroy_timer(timer);
  73.             if (display != nullptr) al_destroy_display(display);
  74.             return;
  75.         }
  76.  
  77.         al_register_event_source(queue, al_get_display_event_source(display));
  78.         al_register_event_source(queue, al_get_timer_event_source(timer));
  79.         al_start_timer(timer);
  80.  
  81.         while (true)
  82.         {
  83.             ALLEGRO_EVENT   event; //why in this loop?
  84.             ALLEGRO_TIMEOUT timeout;
  85.             al_init_timeout(&timeout, 0.06);
  86.             bool get_event = al_wait_for_event_until(&queue, &event, &timeout);
  87.  
  88.             if (get_event)
  89.             {
  90.                 if (event.type == ALLEGRO_EVENT_TIMER)
  91.                 {
  92.                     update = true;
  93.                 }
  94.  
  95.                 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  96.                 {
  97.                     break;
  98.                 }
  99.             }
  100.  
  101.             if (update)
  102.             {
  103.                 al_clear_to_color(al_map_rgb(0, 0, 0));
  104.  
  105.                 if (App::onTick != nullptr) App::onFree();
  106.                 if (App::onDraw != nullptr) App::onFree();
  107.  
  108.                 al_flip_display();
  109.             }
  110.         }
  111.     }
  112.  
  113.     if (App::onFree  != nullptr) App::onFree();
  114.     if (App::timer   != nullptr) al_destroy_timer(timer);
  115.     if (App::queue   != nullptr) al_destroy_event_queue(queue);
  116.     if (App::display != nullptr) al_destroy_display(display);
  117. }
  118.  
  119. void App::bindInit(void(*init)())
  120. {
  121.     App::onInit = init;
  122. }
  123. void App::bindTick(void(*tick)())
  124. {
  125.     App::onTick = tick;
  126. }
  127. void App::bindDraw(void(*draw)())
  128. {
  129.     App::onDraw = draw;
  130. }
  131. void App::bindFree(void(*free)())
  132. {
  133.     App::onFree = free;
  134. }
  135.  
  136. void (*App::onInit)() = nullptr;
  137. void (*App::onTick)() = nullptr;
  138. void (*App::onDraw)() = nullptr;
  139. void (*App::onFree)() = nullptr;
  140.  
  141.  
  142. int main()
  143. {
  144.    
  145.     {
  146.         App::init(256,240, "Title");
  147.         al_init_primitives_addon();
  148.         al_install_keyboard();
  149.         al_install_mouse();
  150.        
  151.     }
  152.    
  153.     al_destroy_bitmap(allegro);
  154.    
  155.     al_destroy_event_queue(queue);
  156.     al_destroy_timer(timer);
  157.     al_destroy_display(display);
  158.  
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement