Refractor headers and split code into program files for app & localization files.

Split keybinds from window.h file. General cleanup
This commit is contained in:
2026-06-01 23:20:07 +01:00
parent dcb8b46245
commit c24285b0af
11 changed files with 755 additions and 646 deletions
+111
View File
@@ -0,0 +1,111 @@
#include "app/app.h"
namespace app {
void App::exit() {
running = false;
}
void App::switch_to_scene(uint64_t scene_id) {
scene_pool.release(current_scene_id);
scene_pool.use(scene_id);
current_scene_id = scene_id;
}
App::App(Vec2<float> size, const char *title) {
window.update(size, title);
Scene *loading_scene = new Scene{};
current_scene_id = scene_pool.acquire(loading_scene);
}
void App::set_fps(float fps) {
this->fps = fps;
target_frame_time = 1e9 / fps;
}
uint64_t App::get_fps() const {
return fps;
}
void App::loop() {
uint64_t frame_start = window.get_time();
uint64_t frame_end = window.get_time();
uint64_t previous_frame_end = 0;
while (running) {
frame_start = window.get_time();
uint64_t frame_time = frame_start - frame_end;
if (frame_time <= target_frame_time) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = target_frame_time - frame_time;
nanosleep(&req, nullptr);
}
previous_frame_end = frame_end;
frame_end = window.get_time();
float dt = frame_end - previous_frame_end;
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;
}
}
int ai = mrb_gc_arena_save(Ruby::mrb);
mrb_value dt_val = mrb_float_value(Ruby::mrb, dt / 1e9);
update.call(1, &dt_val);
scene->update_scene(dt_val);
mrb_gc_arena_restore(Ruby::mrb, ai);
scene->render(window.get_time());
window.render();
window.clear();
}
}
} // namespace app
+77
View File
@@ -0,0 +1,77 @@
#include "app/elements.h"
namespace app {
void Element::click() {
on_click.call();
}
// EImage
EImage::~EImage() {
if (animation_id != UINT64_MAX)
animation_pool.release(animation_id);
}
Vec2<float> EImage::size() {
Animation &animation = *animation_pool[animation_id];
if (animation.frames.empty())
return {0, 0};
return window.get_image_size(animation.frames[0]);
}
void EImage::render(uint64_t abs_time) {
Animation &animation = *animation_pool[animation_id];
if (animation.frames.empty())
return;
int frame_index = (int)((abs_time * animation.frame_rate) / 1e9) % animation.frames.size();
window.draw(animation.frames[frame_index], position, z, rotation, pivot, scale, alpha);
}
// EText
EText::~EText() {
if (text_id != UINT64_MAX)
text_pool.release(text_id);
if (font_id != UINT64_MAX)
font_pool.release(font_id);
}
Vec2<float> EText::size() {
if (text_id == UINT64_MAX)
return {0, 0};
return window.get_text_size(text_id);
}
void EText::reload_text() {
if (text_id != UINT64_MAX) {
text_pool.release(text_id);
text_id = UINT64_MAX;
}
}
void EText::render(uint64_t) {
if (
last_language != Localization::current_language
|| last_key != key || text_id == UINT64_MAX
|| last_color != color
|| params_changed
) {
std::string localized_str = Localization::get(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;
params_changed = false;
}
window.write(text_id, position, z, rotation, pivot, scale, alpha);
}
// ERect
Vec2<float> ERect::size() {
return shape;
};
void ERect::render(uint64_t) {
window.draw_rect(Rect::from(position, shape), color, z, rotation, pivot, scale, alpha);
}
} // namespace app
+91
View File
@@ -0,0 +1,91 @@
#include "app/scene.h"
namespace app {
Scene::~Scene() {
for (const uint64_t &id : element_ids)
element_pool.release(id);
}
void Scene::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 Scene::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 Scene::update_scene(mrb_value dt_val) {
if (mrb_nil_p(update.proc))
return;
update.call(1, &dt_val);
}
void Scene::click(Vec2<float> position) {
std::vector<uint64_t> clicked_elements = elements_at(position);
for (const uint64_t &id : clicked_elements)
element_pool[id]->click();
}
void Scene::render(uint64_t abs_time) {
for (const uint64_t &e : element_ids)
element_pool[e]->render(abs_time);
}
std::vector<uint64_t> Scene::elements_at(Vec2<float> position) {
std::vector<uint64_t> result;
for (auto it = element_ids.rbegin(); it != element_ids.rend(); ++it) {
Element *e = element_pool[*it];
Vec2<float> size = e->size();
Rect rect = Rect::from(size);
if (rect.contains(world_to_local(position, e))) {
if (e->click_mode == ClickMode::PASS) {
result.push_back(*it);
} else if (e->click_mode == ClickMode::BLOCK) {
result.push_back(*it);
break;
} else {
continue;
}
}
}
return result;
}
Vec2<float> Scene::world_to_local(Vec2<float> p, Element *e) {
Vec2<float> size = e->size();
Vec2<float> pivot = e->pivot * size * e->scale;
p = p - e->position;
float rad = -e->rotation * (M_PI / 180.0f);
float c = cos(rad);
float s = sin(rad);
p = p - pivot;
p = {
p.x * c - p.y * s,
p.x * s + p.y * c
};
p = p + pivot;
p = p / e->scale;
return p;
}
} // namespace app