Files
project_misth/include/app/app.h
T
syedm 55f4b4d933 Add basic app interface
- Add pool struct for data pools
2026-05-04 15:49:54 +01:00

189 lines
5.0 KiB
C++

#include "binding/ruby.h"
#include "localization/localization.h"
#include "utils.h"
#include "window/window.h"
namespace app {
struct Element {
Vec2<float> position = {0, 0};
float rotation = 0;
Vec2<float> 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<float> size(Window *window) = 0;
void click() {
if (!mrb_nil_p(on_click.proc)) {
int x = mrb_gc_arena_save(Ruby::mrb);
on_click.call();
mrb_gc_arena_restore(Ruby::mrb, x);
}
}
};
struct Image : Element {
Animation animation;
float alpha = 1.0f;
Image(Animation animation) : animation(std::move(animation)) {}
Vec2<float> 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, alpha);
}
};
struct Text : Element {
Localization::Key key;
int font_id;
int font_size = 16;
Color color = {255, 255, 255, 255};
Text(Localization::Key key, int font_id, int font_size, Color color)
: key(key), font_id(font_id), font_size(font_size), color(color) {}
Vec2<float> size(Window *window) override {
return window->get_text_size(text_id);
};
void render(Window &window, uint64_t abs_time) 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<float> shape = {0, 0};
Color color = {255, 255, 255, 255};
Vec2<float> size(Window *) override {
return shape;
};
ERect(Vec2<float> shape, Color color) : shape(shape), color(color) {}
void render(Window &window, uint64_t abs_time) override {
window.draw_rect(Rect::from_size(position, shape), color, z, rotation);
}
};
extern Pool<std::unique_ptr<Element>> element_pool;
struct Scene {
std::vector<int> element_ids;
Ruby::Block update;
void add_element(int element_id) {
auto it = std::lower_bound(
element_ids.begin(),
element_ids.end(),
element_id,
[](auto &a, auto &b) {
return element_pool[a]->z < element_pool[b]->z || (element_pool[a]->z == element_pool[b]->z && a < b);
}
);
element_ids.insert(it, element_id);
}
void update_scene(uint64_t delta_time) {
if (!mrb_nil_p(update.proc)) {
int x = mrb_gc_arena_save(Ruby::mrb);
mrb_value dt_val = mrb_int_value(Ruby::mrb, delta_time);
update.call(1, &dt_val);
mrb_gc_arena_restore(Ruby::mrb, x);
}
}
void click(Window *window, Vec2<float> position) {
for (auto it = element_ids.rbegin(); it != element_ids.rend(); ++it) {
Element *e = element_pool[*it].get();
Vec2<float> 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);
}
};
extern Pool<std::unique_ptr<Scene>> scene_pool;
struct App {
Window window;
int current_scene_id = 0;
bool running = true;
App(Vec2<float> size, const char *title, Animation loading_animation)
: window(size, title) {
std::unique_ptr<Scene> loading_scene = std::make_unique<Scene>();
#warning "Should try to center the loading image or scale it to fit the screen"
std::unique_ptr<Image> image = std::make_unique<Image>(loading_animation);
int image_id = element_pool.acquire(std::move(image));
loading_scene->add_element(image_id);
scene_pool.acquire(std::move(loading_scene));
}
void loop() {
while (running) {
uint64_t dt = window.delta_time();
uint64_t abs_time = window.get_time();
Scene *scene = scene_pool[current_scene_id].get();
scene->update_scene(dt);
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->render(window, abs_time);
window.render();
}
}
};
} // namespace app