Fix main loop, add example and cleanup

This commit is contained in:
2026-05-04 21:28:22 +01:00
parent 55f4b4d933
commit a51eb903c4
8 changed files with 127 additions and 128 deletions
+40 -24
View File
@@ -1,3 +1,6 @@
#ifndef APP_H
#define APP_H
#include "binding/ruby.h"
#include "localization/localization.h"
#include "utils.h"
@@ -25,11 +28,10 @@ struct Element {
}
};
struct Image : Element {
struct EImage : Element {
Animation animation;
float alpha = 1.0f;
Image(Animation animation) : animation(std::move(animation)) {}
EImage(Animation animation) : animation(std::move(animation)) {}
Vec2<float> size(Window *window) override {
if (animation.frames.empty())
@@ -42,24 +44,24 @@ struct Image : Element {
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);
window.draw(animation.frames[frame_index], position, z, rotation, scale);
}
};
struct Text : Element {
struct EText : 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)
EText(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 {
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)
@@ -86,12 +88,12 @@ struct ERect : Element {
ERect(Vec2<float> shape, Color color) : shape(shape), color(color) {}
void render(Window &window, uint64_t abs_time) override {
void render(Window &window, uint64_t) override {
window.draw_rect(Rect::from_size(position, shape), color, z, rotation);
}
};
extern Pool<std::unique_ptr<Element>> element_pool;
inline Pool<Element *> element_pool;
struct Scene {
std::vector<int> element_ids;
@@ -120,7 +122,7 @@ struct Scene {
void click(Window *window, Vec2<float> position) {
for (auto it = element_ids.rbegin(); it != element_ids.rend(); ++it) {
Element *e = element_pool[*it].get();
Element *e = element_pool[*it];
Vec2<float> size = e->size(window);
Rect rect = Rect::from_size(e->position, size * e->scale);
if (rect.contains(position)) {
@@ -139,31 +141,39 @@ struct Scene {
}
};
extern Pool<std::unique_ptr<Scene>> scene_pool;
inline Pool<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>();
App(Vec2<float> 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"
std::unique_ptr<Image> image = std::make_unique<Image>(loading_animation);
int image_id = element_pool.acquire(std::move(image));
Scene *loading_scene = new Scene{};
EImage *image = new EImage(loading_animation);
int image_id = element_pool.acquire(image);
loading_scene->add_element(image_id);
scene_pool.acquire(std::move(loading_scene));
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();
uint64_t abs_time = window.get_time();
Scene *scene = scene_pool[current_scene_id].get();
scene->update_scene(dt);
Scene *scene = scene_pool[current_scene_id];
Event e;
while (window.poll(e)) {
@@ -173,6 +183,7 @@ struct App {
break;
case Event::MOUSE_BUTTON_DOWN:
scene->click(&window, e.mouse_button.position);
printf("DT: %lu ms\n", dt);
break;
default:
break;
@@ -180,10 +191,15 @@ struct App {
window.consume(e);
}
scene->render(window, abs_time);
scene->update_scene(dt);
scene->render(window, window.get_time());
window.render();
window.clear();
}
}
};
} // namespace app
} // namespace app
#endif