aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_context.c
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-10 00:28:14 -0400
committeriamcheeseman <[email protected]>2026-05-10 00:28:14 -0400
commit576bd27e11ec70bdbd1b9a644d2e227b57586337 (patch)
tree2dc41e3f88bb95568f73ee372fc35512afcbcf9b /teensy/teensy_context.c
parent9ed5698b3c74c7ce1784d3bebe2aa73d5a0c319d (diff)
i ain't splitting alla this up
Diffstat (limited to 'teensy/teensy_context.c')
-rw-r--r--teensy/teensy_context.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/teensy/teensy_context.c b/teensy/teensy_context.c
new file mode 100644
index 0000000..4a812dd
--- /dev/null
+++ b/teensy/teensy_context.c
@@ -0,0 +1,74 @@
+#include "teensy_context.h"
+#include "teensy_renderer.h"
+
+#include <math.h>
+#include <time.h>
+
+#include "teensy_platform.h"
+
+struct ty_ctx ctx;
+extern struct ty_renderer r;
+
+bool is_init(void)
+{
+ int scr_size = ctx.hints.scr_width * ctx.hints.scr_height;
+ return scr_size > 0;
+}
+
+void ty_init(struct ty_hints hints)
+{
+ ty_init_mem();
+
+ ctx.hints = hints;
+ ctx.ticrate = 1.0 / hints.ticrate;
+ ctx.prev_tic_ts = 0;
+
+ ty_platform_init(&ctx);
+ ty_init_renderer();
+}
+
+void ty_deinit(void)
+{
+ assert(is_init());
+ ty_platform_deinit();
+ ty_deinit_renderer();
+ ty_deinit_mem();
+}
+
+bool ty_is_game_running(void)
+{
+ assert(is_init());
+ return !ty_platform_os_wants_quit();
+}
+
+double ty_get_time(void)
+{
+ assert(is_init());
+ return ty_platform_get_time();
+}
+
+int ty_tick(void)
+{
+ assert(is_init());
+
+ double current_time = ty_get_time();
+ double time_since_tic = current_time - ctx.prev_tic_ts;
+
+ int tics = time_since_tic / ctx.ticrate;
+ if (tics > 0) {
+ ctx.prev_tic_ts = current_time;
+ ty_platform_frame(r.screen);
+ }
+ return tics;
+}
+
+// TODO: Find a better place for this
+void ty_sleep(uint64_t ms)
+{
+ assert(is_init());
+
+ struct timespec ts;
+ ts.tv_sec = ms / 1000;
+ ts.tv_nsec = (ms % 1000) * 1000000;
+ nanosleep(&ts, NULL);
+}