Fix loop to be more accuratly timed, and added custom ruby libs inclusion setup

This commit is contained in:
2026-05-20 17:30:38 +01:00
parent 1a337608f3
commit 7d48b226ab
11 changed files with 1117 additions and 27 deletions
+13 -9
View File
@@ -54,8 +54,7 @@ struct EImage : Element {
Animation &animation = *animation_pool[animation_id];
if (animation.frames.empty())
return;
int frame_index = (int)((abs_time * animation.frame_rate) / 1000) % animation.frames.size();
int frame_index = (int)((abs_time * animation.frame_rate) / 1e9) % animation.frames.size();
window.draw(animation.frames[frame_index], position, z, rotation, scale, alpha);
}
};
@@ -166,10 +165,10 @@ struct Scene {
}
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);
}
if (mrb_nil_p(update.proc))
return;
mrb_value dt_val = mrb_float_value(Ruby::mrb, delta_time / 1e9);
update.call(1, &dt_val);
}
void click(Vec2<float> position) {
@@ -234,7 +233,7 @@ struct App {
}
void loop() {
const uint64_t target_ms = 1000 / 60;
const uint64_t TARGET_NS = 1e9 / 60;
uint64_t time_a = window.get_time();
uint64_t time_b = window.get_time();
@@ -242,8 +241,13 @@ struct App {
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);
if (frame_time <= TARGET_NS) {
struct timespec req;
req.tv_sec = 0;
req.tv_nsec = TARGET_NS - frame_time;
nanosleep(&req, nullptr);
}
time_b = window.get_time();
+10
View File
@@ -4,6 +4,8 @@
#include "pch.h"
#include "utils.h"
#include "binding/ruby_compiled.h"
#define DEF_SYM(name) inline mrb_sym sym_##name = mrb_intern_cstr(Ruby::mrb, #name);
#define SYM(name) mrb_symbol_value(sym_##name)
@@ -83,7 +85,15 @@ struct Block {
};
inline void run_string(const char *code, size_t length) {
int ai = mrb_gc_arena_save(mrb);
mrb_load_nstring(mrb, code, length);
mrb_gc_arena_restore(mrb, ai);
}
inline void load_mgems() {
int ai = mrb_gc_arena_save(mrb);
mrb_load_irep(mrb, mgems);
mrb_gc_arena_restore(mrb, ai);
}
}; // namespace Ruby
+2 -2
View File
@@ -341,8 +341,8 @@ public:
// Time
uint64_t get_time(); // returns the time in milliseconds since the window was created
uint64_t delta_time(); // returns the time in milliseconds since the last render call
uint64_t get_time(); // returns the time in nanoseconds since the window was created
uint64_t delta_time(); // returns the time in nanoseconds since the last render call
private:
// internal data and functions