update: Added tick timing handler
authorLukas Krickl <lukas@krickl.dev>
Thu, 26 Feb 2026 11:26:58 +0000 (12:26 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 26 Feb 2026 11:26:58 +0000 (12:26 +0100)
src/lrts.c
src/lrts_impl.h
src/r_render.c
src/t_update.c [new file with mode: 0644]
src/t_update.h [new file with mode: 0644]
src/u_defs.h

index 110ed9dc85abcfa63fe89edfb08fee7b97349b78..30caf044f2d3755bae1786c3f0dac7c3a9ddb873 100644 (file)
@@ -6,14 +6,20 @@
 #include "i_input.h"
 #include "t_camera.h"
 #include "t_map.h"
+#include "t_update.h"
 #include "u_time.h"
 
 struct lrts_config lrts_global_cfg;
+struct lrts_state lrts_global_state;
 
 struct lrts_config* lrts_cfg() {
        return &lrts_global_cfg;
 }
 
+struct lrts_state* lrts_state() {
+       return &lrts_global_state;
+}
+
 void lrts_help(int argc, char **argv) {
        LRTS_UNUSED(argc);
 
@@ -66,7 +72,8 @@ struct u_vec2 lrts_get_screen_res(void) {
 }
 
 void lrts_init(void) {
-       struct lrts_config* cfg = lrts_cfg();
+       struct lrts_config *cfg = lrts_cfg();
+       struct lrts_state *state = lrts_state();
        struct u_vec2 res = lrts_get_render_res();
 
 
@@ -77,10 +84,13 @@ void lrts_init(void) {
        i_input_init();
        
        u_fps_timer = u_timer_init();
-
+       t_sim_init();
 
        t_main_camera = t_camera_init(res.x, res.y);
        t_map = t_map_init();
+
+       state->frame_count = 0;
+       state->tick_count = 0;
 }
 
 int lrts_main(int argc, char **argv) {
@@ -94,6 +104,7 @@ int lrts_main(int argc, char **argv) {
 
        while (!lrts_cfg()->exit) {
                p_poll_events();
+               t_sim_update();
                r_render_frame();
        }
 
index 49f7fd08e4f8f78b20b38db4f7d3a77a40a7cdab..b9815a6fba095a99a5ee1f96b66d095d32e0f821 100644 (file)
@@ -50,6 +50,7 @@
 #include "r_assets.c"
 #include "i_input.c"
 #include "u_time.c"
+#include "t_update.c"
 
 #endif
 
index 837ff103a3f7c92e263b2c1e8d1fa170559b6650..8585a29778cd1304e1b87f06ce3a6a80516752f1 100644 (file)
@@ -26,15 +26,18 @@ void r_draw_pixel(struct r_framebuffer *fb, i32 x, i32 y, r_color color) {
 }
 
 void r_render_frame() {
+       struct lrts_state *state = lrts_state();
+
        u_timer_start(&u_fps_timer);
        p_draw_begin();
        
 
-       t_map_draw(&t_map);
+       /* t_map_draw(&t_map); */
 
        p_draw_end();
        p_draw_present();
        u_timer_end(&u_fps_timer);
 
+       state->frame_count++;
        u_cap_fps();
 }
diff --git a/src/t_update.c b/src/t_update.c
new file mode 100644 (file)
index 0000000..472fa5d
--- /dev/null
@@ -0,0 +1,38 @@
+#include "t_update.h"
+
+struct u_timer t_sim_timer;
+u32 t_updates_this_interval;
+
+void t_sim_init(void) {
+       t_sim_timer = u_timer_init();
+       t_updates_this_interval = 0;
+       u_timer_start(&t_sim_timer);
+}
+
+/* returns how much time should pass between each tick
+ * based on the platform time information provided */
+u32 t_sim_tick_interval(void) {
+       return (u32)p_ticks_per_second() / LRTS_TPS;
+}
+
+
+void t_sim_update(void) {
+       struct lrts_state *state = lrts_state();
+
+       /* if timer has been running for more than a second 
+        * reset tick this sec and timer */
+       if (u_timer_end(&t_sim_timer) >= (u32)t_sim_tick_interval()) {
+               t_updates_this_interval = 0;
+               u_timer_start(&t_sim_timer);
+       }
+
+       /* bail if timer is not ready yet */
+       if (t_updates_this_interval) {
+               return;
+       }
+
+       t_updates_this_interval++;
+       state->tick_count++;
+       
+       /* update game simulation */
+}
diff --git a/src/t_update.h b/src/t_update.h
new file mode 100644 (file)
index 0000000..6c797a1
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef T_UPDATE_H__
+#define T_UPDATE_H__
+
+#include "u_time.h"
+
+extern struct u_timer t_sim_timer;
+
+void t_sim_init(void);
+
+/**
+ * Advances the simulation by a tick
+ * Simulation updates are capped at the 
+ * max tick rate per second.
+ */ 
+void t_sim_update(void);
+
+#endif
index f9dde6bb5b19abbab95be3eb51df31c810dd05f8..4d5b1297206f5a0e30cbe7861e9a39638215519e 100644 (file)
@@ -56,8 +56,14 @@ struct lrts_config {
        int argc;
 };
 
+struct lrts_state {
+       u32 frame_count;
+       u32 tick_count;
+};
+
 
 struct lrts_config* lrts_cfg();
+struct lrts_state* lrts_state();
 void lrts_help(int argc, char **argv);
 void lrts_version(void);
 void lrts_getopt(int argc, char **argv, struct lrts_config *cfg);