#ifndef APP_H #define APP_H #include "binding/ruby.h" #include "localization/localization.h" #include "utils.h" #include "window/window.h" namespace app { struct Element { Vec2 position = {0, 0}; float rotation = 0; Vec2 scale = {1, 1}; float z = 0; bool click_through = false; Ruby::Block on_click; virtual ~Element() = default; virtual void render(Window &window, uint64_t abs_time) = 0; virtual Vec2 size(Window *window) = 0; void click() { if (!mrb_nil_p(on_click.proc)) on_click.call(); } }; struct EImage : Element { Animation animation; EImage(Animation animation) : animation(std::move(animation)) {} Vec2 size(Window *window) override { if (animation.frames.empty()) return {0, 0}; return window->get_image_size(animation.frames[0]); } void render(Window &window, uint64_t abs_time) override { if (animation.frames.empty()) return; int frame_index = (int)((abs_time * animation.frame_rate) / 1000) % animation.frames.size(); window.draw(animation.frames[frame_index], position, z, rotation, scale); } }; struct EText : Element { Localization::Key key; int font_id; Color color = {255, 255, 255, 255}; EText(Localization::Key key, int font_id, Color color) : key(key), font_id(font_id), color(color) {} Vec2 size(Window *window) override { return window->get_text_size(text_id); }; void render(Window &window, uint64_t) override { if (last_language != Localization::current_language || last_key != key || text_id == -1) { std::string localized_str = Localization::get(key); if (text_id != -1) window.unload_text(text_id); text_id = window.create_text(font_id, localized_str.c_str(), localized_str.size(), color); last_language = Localization::current_language; last_key = key; } window.write(text_id, position, z, rotation, scale); } private: int text_id = -1; Localization::LanguageCode last_language = -1; Localization::Key last_key = -1; }; struct ERect : Element { Vec2 shape = {0, 0}; Color color = {255, 255, 255, 255}; Vec2 size(Window *) override { return shape; }; ERect(Vec2 shape, Color color) : shape(shape), color(color) {} void render(Window &window, uint64_t) override { window.draw_rect(Rect::from_size(position, shape), color, z, rotation); } }; inline Pool element_pool; struct Scene { std::vector element_ids; Ruby::Block update; ~Scene() { for (const int &id : element_ids) element_pool.release(id); } void add_element(int element_id) { auto it = std::lower_bound( element_ids.begin(), element_ids.end(), element_id, [](auto &a, auto &b) { auto e_a = element_pool[a]; auto e_b = element_pool[b]; return e_a->z < e_b->z || (e_a->z == e_b->z && a < b); } ); element_pool.use(element_id); element_ids.insert(it, element_id); } void delete_element(int element_id) { auto it = std::find(element_ids.begin(), element_ids.end(), element_id); if (it != element_ids.end()) { element_pool.release(element_id); element_ids.erase(it); } } void update_scene(uint64_t delta_time) { if (!mrb_nil_p(update.proc)) { mrb_value dt_val = mrb_int_value(Ruby::mrb, delta_time); update.call(1, &dt_val); } } void click(Window *window, Vec2 position) { for (auto it = element_ids.rbegin(); it != element_ids.rend(); ++it) { Element *e = element_pool[*it]; Vec2 size = e->size(window); Rect rect = Rect::from_size(e->position, size * e->scale); if (rect.contains(position)) { e->click(); if (e->click_through) continue; else break; } } } void render(Window &window, uint64_t abs_time) { for (const int &e : element_ids) element_pool[e]->render(window, abs_time); } }; inline Pool scene_pool; struct App { int current_scene_id = 0; bool running = true; void switch_to_scene(int scene_id) { scene_pool.release(current_scene_id); scene_pool.use(scene_id); current_scene_id = scene_id; } App(Vec2 size, const char *title, Animation loading_animation) { window.update(size, title); #warning "Should try to center the loading image or scale it to fit the screen" Scene *loading_scene = new Scene{}; EImage *image = new EImage(loading_animation); int image_id = element_pool.acquire(image); loading_scene->add_element(image_id); current_scene_id = scene_pool.acquire(loading_scene); } void loop() { const uint64_t target_ms = 1000 / 60; uint64_t time_a = window.get_time(); uint64_t time_b = window.get_time(); while (running) { time_a = window.get_time(); uint64_t frame_time = time_a - time_b; if (frame_time <= target_ms) usleep((target_ms - frame_time) * 1000); time_b = window.get_time(); uint64_t dt = window.delta_time(); Scene *scene = scene_pool[current_scene_id]; Event e; while (window.poll(e)) { switch (e.type) { case Event::QUIT: running = false; break; case Event::MOUSE_BUTTON_DOWN: scene->click(&window, e.mouse_button.position); break; default: break; } window.consume(e); } scene->update_scene(dt); scene->render(window, window.get_time()); window.render(); window.clear(); } } }; } // namespace app inline app::App *app_ = nullptr; #endif