Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <stdio>
- #include "allegro5/allegro.h"
- ALLEGRO_BITMAP *allegro;
- ALLEGRO_EVENT_QUEUE* queue;
- ALLEGRO_TIMER* timer;
- ALLEGRO_DISPLAY *display;
- class App
- {
- public:
- static void init(int w = 256, int h = 240, std::string title = "Untitled", int fps = 60, bool noDisplay = false);
- static void bindInit(void (*init)());
- static void bindTick(void (*tick)());
- static void bindDraw(void (*draw)());
- static void bindFree(void (*free)());
- protected:
- private:
- static void (*onInit)();
- static void (*onTick)();
- static void (*onDraw)();
- static void (*onFree)();
- };
- void App::init(int w, int h, std::string title, int fps, bool noDisplay)
- {
- if (!al_init())
- {
- fprintf(stderr, "failed to initalize allegro!\n");
- return;
- }
- al_install_keyboard();
- al_install_mouse();
- if (!noDisplay)
- {
- bool update = false;
- timer = al_create_timer(1.0 / fps);
- if (!timer) {
- fprintf(stderr, "failed to create timer!\n");
- return;
- }
- display = al_create_display(w, h);
- if (!display)
- {
- fprintf(stderr, "failed to create display!\n");
- if (timer != nullptr) al_destroy_timer(timer);
- return;
- }
- queue = al_create_event_queue();
- if (!queue)
- {
- fprintf(stderr, "failed to create event_queue!\n");
- if (timer != nullptr) al_destroy_timer(timer);
- if (display != nullptr) al_destroy_display(display);
- return;
- }
- al_register_event_source(queue, al_get_display_event_source(display));
- al_register_event_source(queue, al_get_timer_event_source(timer));
- al_start_timer(timer);
- while (true)
- {
- ALLEGRO_EVENT event; //why in this loop?
- ALLEGRO_TIMEOUT timeout;
- al_init_timeout(&timeout, 0.06);
- bool get_event = al_wait_for_event_until(&queue, &event, &timeout);
- if (get_event)
- {
- if (event.type == ALLEGRO_EVENT_TIMER)
- {
- update = true;
- }
- if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- {
- break;
- }
- }
- if (update)
- {
- al_clear_to_color(al_map_rgb(0, 0, 0));
- if (App::onTick != nullptr) App::onFree();
- if (App::onDraw != nullptr) App::onFree();
- al_flip_display();
- }
- }
- }
- if (App::onFree != nullptr) App::onFree();
- if (App::timer != nullptr) al_destroy_timer(timer);
- if (App::queue != nullptr) al_destroy_event_queue(queue);
- if (App::display != nullptr) al_destroy_display(display);
- }
- void App::bindInit(void(*init)())
- {
- App::onInit = init;
- }
- void App::bindTick(void(*tick)())
- {
- App::onTick = tick;
- }
- void App::bindDraw(void(*draw)())
- {
- App::onDraw = draw;
- }
- void App::bindFree(void(*free)())
- {
- App::onFree = free;
- }
- void (*App::onInit)() = nullptr;
- void (*App::onTick)() = nullptr;
- void (*App::onDraw)() = nullptr;
- void (*App::onFree)() = nullptr;
- int main()
- {
- {
- App::init(256,240, "Title");
- al_init_primitives_addon();
- al_install_keyboard();
- al_install_mouse();
- }
- al_destroy_bitmap(allegro);
- al_destroy_event_queue(queue);
- al_destroy_timer(timer);
- al_destroy_display(display);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement