#ifndef APP_H #define APP_H #include "binding/ruby.h" #include "localization/localization.h" #include "utils.h" #include "window/window.h" enum Type { IMAGE, TEXT, RECT }; namespace app { struct Element { Type type; Vec2 position = {0, 0}; float rotation = 0; Vec2 scale = {1, 1}; float alpha = 1.0f; float z = 0; bool click_through = false; Ruby::Block on_click; Element(Type type) : type(type) {} virtual ~Element() = default; virtual void render(Window &window, uint64_t abs_time) = 0; virtual Vec2 size(Window *window) = 0; void click() { printf("Running click handler\n"); on_click.call(); } }; struct EImage : Element { uint64_t animation_id = UINT64_MAX; EImage(uint64_t animation_id) : Element(Type::IMAGE), animation_id(animation_id) { animation_pool.use(animation_id); } ~EImage() { if (animation_id != UINT64_MAX) animation_pool.release(animation_id); } Vec2 size(Window *window) override { Animation &animation = *animation_pool[animation_id]; if (animation.frames.empty()) return {0, 0}; return window->get_image_size(animation.frames[0]); } void render(Window &window, uint64_t abs_time) override { Animation &animation = *animation_pool[animation_id]; 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, alpha); } }; struct EText : Element { Localization::Key key; uint64_t font_id = UINT64_MAX; Color color = {255, 255, 255}; mrb_value params = mrb_nil_value(); ~EText() { if (text_id != UINT64_MAX) text_pool.release(text_id); if (font_id != UINT64_MAX) font_pool.release(font_id); if (!mrb_nil_p(params)) mrb_gc_unregister(Ruby::mrb, params); } EText(Localization::Key key, uint64_t font_id, Color color, mrb_value params) : Element(Type::TEXT), key(key), font_id(font_id), color(color), params(params) { if (!mrb_nil_p(params)) mrb_gc_protect(Ruby::mrb, params); font_pool.use(font_id); } void reload_params() { // Force re-render by resetting last_key (without affecting this frame) // this can be changed to use a dirty flag or version counter if needed last_key = UINT16_MAX; } Vec2 size(Window *window) override { if (text_id == UINT64_MAX) return {0, 0}; 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 == UINT64_MAX || last_color != color ) { std::string localized_str = Localization::get(Ruby::mrb, key, params); if (text_id != UINT64_MAX) text_pool.release(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; last_color = color; } window.write(text_id, position, z, rotation, scale, alpha); } private: uint64_t text_id = UINT64_MAX; Color last_color = color; Localization::LanguageCode last_language = UINT32_MAX; Localization::Key last_key = UINT16_MAX; }; struct ERect : Element { Vec2 shape = {0, 0}; Color color = {255, 255, 255}; Vec2 size(Window *) override { return shape; }; ERect(Vec2 shape, Color color) : Element(Type::RECT), shape(shape), color(color) {} void render(Window &window, uint64_t) override { window.draw_rect(Rect::from_size(position, shape), color, z, rotation, scale, alpha); } }; inline Pool element_pool; struct Scene { std::vector element_ids; Ruby::Block update; ~Scene() { for (const uint64_t &id : element_ids) element_pool.release(id); } void add_element(uint64_t 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(uint64_t 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); printf("running: update, dt: %lu\n", delta_time); update.call(1, &dt_val); } } void click(Vec2 position) { std::vector clicked_elements = elements_at(position); for (const uint64_t &id : clicked_elements) element_pool[id]->click(); } #warning "This does not take rotation into account \ reference and use reverse-transform on point then AABB check instead" std::vector elements_at(Vec2 position) { std::vector result; 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)) { result.push_back(*it); if (e->click_through) continue; else break; } } return result; } void render(Window &window, uint64_t abs_time) { for (const uint64_t &e : element_ids) element_pool[e]->render(window, abs_time); } }; inline Pool scene_pool; struct App { uint64_t current_scene_id = 0; bool running = true; Vec2 mouse_position = {-1, -1}; std::array down_keys{}; std::vector pressed_keys; std::vector released_keys; Vec2 scroll = {0, 0}; std::string text = ""; void exit() { running = false; } void switch_to_scene(uint64_t scene_id) { scene_pool.release(current_scene_id); scene_pool.use(scene_id); current_scene_id = scene_id; } App(Vec2 size, const char *title) { window.update(size, title); Scene *loading_scene = new Scene{}; 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]; pressed_keys.clear(); released_keys.clear(); scroll = {0, 0}; text = ""; Event e; while (window.poll(e)) { switch (e.type) { case Event::QUIT: running = false; break; case Event::MOUSE_BUTTON_DOWN: scene->click(e.mouse_button.position); down_keys[e.mouse_button.button] = true; pressed_keys.push_back(e.mouse_button.button); break; case Event::MOUSE_BUTTON_UP: down_keys[e.mouse_button.button] = false; released_keys.push_back(e.mouse_button.button); break; case Event::MOUSE_MOVE: mouse_position = e.mouse_move.position; break; case Event::MOUSE_LEAVE: mouse_position = {-1, -1}; break; case Event::KEY_DOWN: down_keys[e.key.key] = true; pressed_keys.push_back(e.key.key); break; case Event::KEY_UP: down_keys[e.key.key] = false; released_keys.push_back(e.key.key); break; case Event::SCROLL: scroll = e.scroll; break; case Event::TEXT_INPUT: text += *e.text_input; delete e.text_input; break; default: break; } } scene->update_scene(dt); scene->render(window, window.get_time()); window.render(); window.clear(); } } }; } // namespace app inline app::App *app_ = nullptr; #endif