Fix loop to be more accuratly timed, and added custom ruby libs inclusion setup
This commit is contained in:
+13
-9
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user