debug: Added debug flag and fps state
authorLukas Krickl <lukas@krickl.dev>
Thu, 26 Feb 2026 12:58:08 +0000 (13:58 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 26 Feb 2026 12:58:08 +0000 (13:58 +0100)
Added debug font renderer. Currently no font is actually loaded until
there is a good way to just grab any system font.
Added u_string.h with sprintf functions.
Added debug rendering.
Removed surface locking. This should be OK since there is only a single
thread.

makefile
src/lrts.c
src/lrts_impl.h
src/p_pc/u_string.c [new file with mode: 0644]
src/p_platform.h
src/p_r_sdl/p_draw.c
src/p_r_sdl/p_init.c
src/p_r_sdl/p_r_sdl.h [new file with mode: 0644]
src/r_render.c
src/u_defs.h
src/u_string.h [new file with mode: 0644]

index 9fbcdf6a1cc5b5848e28a8084600629c51a85262..93b12fc82092a8df0661a8a0c755ae26ff577d04 100644 (file)
--- a/makefile
+++ b/makefile
@@ -3,7 +3,7 @@ TEST_NAME=test$(NAME)
 DBGCFLAGS=-g
 DBGLDFLAGS=
 CFLAGS=-Wall -Wextra -Werror -pedantic $(DBGCFLAGS) -std=c89
-LIBS=-lSDL3
+LIBS=-lSDL3 -lSDL3_ttf
 LDFLAGS=$(DBGLDFLAGS) $(LIBS)
 
 INSTALL_DIR=/usr/local
index 30caf044f2d3755bae1786c3f0dac7c3a9ddb873..aebc6e3740ce0e3c1159738c5afe94bd88f5b22e 100644 (file)
@@ -91,6 +91,9 @@ void lrts_init(void) {
 
        state->frame_count = 0;
        state->tick_count = 0;
+#ifdef LRTS_DEBUG
+       state->debug = LRTS_TRUE;
+#endif
 }
 
 int lrts_main(int argc, char **argv) {
index b9815a6fba095a99a5ee1f96b66d095d32e0f821..43821dcc41505a8f8ce2fe724e0baa5b86b583d1 100644 (file)
@@ -18,6 +18,7 @@
 #include "p_pc/u_assert.c"
 #include "p_posix/p_init.c"
 #include "p_posix/p_getopt.c"
+#include "p_pc/u_string.c"
 #else
 #error "No platform is provided"
 #endif
diff --git a/src/p_pc/u_string.c b/src/p_pc/u_string.c
new file mode 100644 (file)
index 0000000..7d1fd18
--- /dev/null
@@ -0,0 +1,18 @@
+#include "../u_string.h"
+#include <string.h>
+
+int u_vsprintf(char *buffer, const char *fmt, va_list args) {
+               return vsprintf(buffer, fmt, args);
+}
+
+
+int u_sprintf(char *buffer, const char *fmt, ...) {
+               int res = 0;
+               va_list args;
+               
+               va_start(args, fmt);
+               res = vsprintf(buffer, fmt, args);
+               va_end(args);
+
+               return res;
+}
index fa02758e55f64c1692eb069e037334ad7a3c4ad1..5b08a35c11ef221e0f32f2315d676fbd68d9d636 100644 (file)
@@ -57,6 +57,11 @@ u64 p_ticks_per_second(void);
  */
 void p_delay(u64 time);
 
+/**
+ * Drwas debug text to the screen
+ */
+void p_draw_debug_text(u32 x, u32 y, const char *fmt, ...);
+
 
 /**
  * polls input events 
index fdb0c0320d00b399d7423c3b527d2b72e8a7defc..e27e3d703ef24045fff4334ad56339c40fa807c1 100644 (file)
@@ -1,7 +1,9 @@
 #include "../p_draw.h"
 #include "p_window.h"
 #include "../u_assert.h"
-
+#include <stdarg.h>
+#include "p_r_sdl.h"
+#include "../u_string.h"
 
 u64 p_get_ticks(void) {
        return SDL_GetTicksNS();
@@ -16,11 +18,10 @@ u64 p_ticks_per_second(void) {
 }
 
 void p_draw_begin(void) {
-       SDL_LockSurface(r_target);
+       SDL_FillSurfaceRect(r_target, NULL, 0x000000);
 }
 
 void p_draw_end(void) {
-       SDL_UnlockSurface(r_target);
 }
 
 void p_draw_present(void) {
@@ -36,3 +37,24 @@ void p_draw_present(void) {
        SDL_UpdateWindowSurface(p_main_window);
 }
 
+
+void p_draw_debug_text(u32 x, u32 y, const char *fmt, ...) {
+       char buffer[2048];
+       va_list args;
+       TTF_Text *t;
+
+       va_start(args, fmt);
+       vsprintf(buffer, fmt, args);
+       va_end(args);
+       
+       /* if there is no font just print the info... 
+        * this might spam a lot but at least we can still read the data
+        */
+       if (p_r_sdl_debug_font == LRTS_NULL) {
+               u_log(U_LOG_DEBUG, "%s\n", buffer);
+               return;
+       }
+       
+       t = TTF_CreateText(p_r_sdl_text_engine, p_r_sdl_debug_font, buffer, strlen(buffer));
+       TTF_DrawSurfaceText(t, x, y, r_target);
+}
index 4f3f8556b8eee99faae2376fe43b18dfe1f0d2ba..3a749140b2870e75ef5fc6810901ec1f1fe29c0f 100644 (file)
@@ -1,5 +1,6 @@
 #include "../p_platform.h"
 #include <SDL3/SDL.h>
+#include <SDL3_ttf/SDL_ttf.h>
 #include "p_window.h"
 #include "../u_defs.h"
 #include "../u_log.h"
@@ -7,6 +8,9 @@
 
 #include "p_window.c"
 
+TTF_TextEngine *p_r_sdl_text_engine;
+TTF_Font *p_r_sdl_debug_font;
+
 #define DEFAULT_COLOR 0x222222FF
 
 int p_render_init(void) {
@@ -26,6 +30,23 @@ int p_render_init(void) {
                exit(-1);
        }
 
+       if (!TTF_Init()) {
+               u_log(U_LOG_ERR, "Failed to init ttf: %s\n", SDL_GetError());
+               exit(-1);
+       }
+
+       p_r_sdl_text_engine = TTF_CreateSurfaceTextEngine();
+       if (p_r_sdl_text_engine == LRTS_NULL) {
+               u_log(U_LOG_ERR, "Failed to init ttf text engine: %s\n", SDL_GetError());
+               exit(-1);
+       }
+       
+       /* TODO: find a way to get a default font on any platform... */
+       p_r_sdl_debug_font = TTF_OpenFont("", 14);
+       if (p_r_sdl_debug_font == LRTS_NULL) {
+               u_log(U_LOG_WARN, "Failed to open debug font\n", SDL_GetError());
+       }
+
        SDL_SetSurfaceColorKey(SDL_GetWindowSurface(p_main_window), LRTS_TRUE, R_COLOR_TRANSPARENT);
        u_log(U_LOG_DEBUG, "Init successful\n");
 
@@ -54,6 +75,8 @@ int p_render_init_framebuffer(void) {
 int p_renderer_finish(void) {
        SDL_DestroySurface(r_target);
        SDL_DestroyWindow(p_main_window);
+       TTF_CloseFont(p_r_sdl_debug_font);
+       TTF_DestroySurfaceTextEngine(p_r_sdl_text_engine);
        SDL_Quit();
        u_log(U_LOG_DEBUG, "Exiting...\n");
        return 0;
diff --git a/src/p_r_sdl/p_r_sdl.h b/src/p_r_sdl/p_r_sdl.h
new file mode 100644 (file)
index 0000000..5b3b57e
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef P_R_SDL_H__
+#define P_R_SDL_H__
+
+#include <SDL3_ttf/SDL_ttf.h>
+
+extern TTF_Font *p_r_sdl_debug_font;
+extern TTF_TextEngine *p_r_sdl_text_engine;
+
+#endif
index 8585a29778cd1304e1b87f06ce3a6a80516752f1..80519141c04e2359b11b65e3ae071a152f457bff 100644 (file)
@@ -25,19 +25,29 @@ void r_draw_pixel(struct r_framebuffer *fb, i32 x, i32 y, r_color color) {
  *pixel = color;
 }
 
-void r_render_frame() {
+void r_render_debug(void) {
+       struct lrts_state *state = lrts_state();
+
+       p_draw_debug_text(0, 0, "FPS: %d", state->fps);
+}
+
+void r_render_frame(void) {
        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);
+       if (state->debug) {
+               r_render_debug();
+       }
 
        p_draw_end();
+
+
        p_draw_present();
        u_timer_end(&u_fps_timer);
 
        state->frame_count++;
-       u_cap_fps();
+       state->fps = u_cap_fps();
 }
index 4d5b1297206f5a0e30cbe7861e9a39638215519e..68a2e0ad6561ff8e749a36db70819287f0f86d63 100644 (file)
@@ -59,6 +59,8 @@ struct lrts_config {
 struct lrts_state {
        u32 frame_count;
        u32 tick_count;
+       lrts_bool debug;
+       u32 fps;
 };
 
 
@@ -82,4 +84,6 @@ struct u_vec2 lrts_get_screen_res(void);
 
 #define LRTS_UNUSED(x) (void)(x)
 
+
+
 #endif
diff --git a/src/u_string.h b/src/u_string.h
new file mode 100644 (file)
index 0000000..3b191b7
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef U_STRING_H__
+#define U_STRING_H__
+
+#include <stdarg.h>
+
+int u_vsprintf(char *buffer, const char *fmt, va_list args);
+int u_sprintf(char *buffer, const char *fmt, ...);
+
+#endif