debug font: wip adding debug 1bpp embedded font
authorLukas Krickl <lukas@krickl.dev>
Thu, 26 Feb 2026 19:33:56 +0000 (20:33 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 26 Feb 2026 19:33:56 +0000 (20:33 +0100)
makefile
src/lrts_impl.h
src/p_pc/u_stdio.c
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
src/r_render.c
src/u_debug.c [new file with mode: 0644]
src/u_debug.h [new file with mode: 0644]

index 93b12fc82092a8df0661a8a0c755ae26ff577d04..7d9d9201b2ff62f9e5774f94c321c355d02d4b97 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 -lSDL3_ttf
+LIBS=-lSDL3 
 LDFLAGS=$(DBGLDFLAGS) $(LIBS)
 
 INSTALL_DIR=/usr/local
index 43821dcc41505a8f8ce2fe724e0baa5b86b583d1..9fccfc2c381bd107a6111b78b9aeff0312a9947d 100644 (file)
@@ -52,6 +52,7 @@
 #include "i_input.c"
 #include "u_time.c"
 #include "t_update.c"
+#include "u_debug.c"
 
 #endif
 
index 87bcab88e5fd18e0c07aba2c2fe798a792968c6e..11b91ddf200faa670e339678491be61635000d9d 100644 (file)
@@ -16,7 +16,7 @@ int u_fprintf(U_FILE* stream, const char *fmt, ...) {
                va_list args;
                
                va_start(args, fmt);
-               u_vfprintf(stream, fmt, args);
+               res = u_vfprintf(stream, fmt, args);
                va_end(args);
 
                return res;
index 5b08a35c11ef221e0f32f2315d676fbd68d9d636..dd50baade74bcefe9c651200e2bbd0ba9b3e0133 100644 (file)
@@ -57,10 +57,6 @@ 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, ...);
 
 
 /**
index e27e3d703ef24045fff4334ad56339c40fa807c1..5f2ca674b4a891ea2693e6340065647805d3ab77 100644 (file)
@@ -37,24 +37,3 @@ 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 3a749140b2870e75ef5fc6810901ec1f1fe29c0f..4f3f8556b8eee99faae2376fe43b18dfe1f0d2ba 100644 (file)
@@ -1,6 +1,5 @@
 #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"
@@ -8,9 +7,6 @@
 
 #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) {
@@ -30,23 +26,6 @@ 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");
 
@@ -75,8 +54,6 @@ 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;
index 5b3b57ee1f6a31a27c21faf8410e69f87b3f49ec..28abf6964a0b787ed1e3729b3ed03b3e966644b2 100644 (file)
@@ -1,9 +1,5 @@
 #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 80519141c04e2359b11b65e3ae071a152f457bff..c7d89d0f84c8882854419118d5989798ef59ed18 100644 (file)
@@ -3,6 +3,7 @@
 #include "p_draw.h"
 #include "t_tile.h"
 #include "r_assets.h"
+#include "u_debug.h"
 
 struct r_framebuffer r_framebuffer;
 
@@ -26,9 +27,10 @@ void r_draw_pixel(struct r_framebuffer *fb, i32 x, i32 y, r_color color) {
 }
 
 void r_render_debug(void) {
-       struct lrts_state *state = lrts_state();
+       /* struct lrts_state *state = lrts_state(); */
 
-       p_draw_debug_text(0, 0, "FPS: %d", state->fps);
+       u_debug_draw_text(0, 0, "01");
+       /*u_debug_draw_text(0, 0, "FPS: %d", state->fps); */
 }
 
 void r_render_frame(void) {
@@ -37,7 +39,7 @@ void r_render_frame(void) {
        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();
        }
diff --git a/src/u_debug.c b/src/u_debug.c
new file mode 100644 (file)
index 0000000..7ba3918
--- /dev/null
@@ -0,0 +1,74 @@
+#include "u_debug.h"
+
+#include "r_render.h"
+
+#define U_DEBUG_FONT_W 8
+#define U_DEBUG_FONT_H 8
+
+const u8 debug_font_num[10 * U_DEBUG_FONT_H] = {
+       /* 0 */
+       0xFE,
+       0x82,
+       0x82,
+       0x82,
+       0x82,
+       0x82,
+       0x82,
+       0xFF,
+
+       /* 1 */
+       0x80,
+       0x80,
+       0x80,
+       0x80,
+       0x80,
+       0x80,
+       0x80,
+       0x80,
+
+       /* 2 */
+
+};
+
+r_color u_debug_colors[2] = {0x00000000, 0xFFFFFFFF};
+
+#define u_debug_draw_pixel(x, y, i, c, bit) r_draw_pixel(&r_framebuffer, (x)+7-bit, (y) + i, u_debug_colors[c[i] >> bit & 0x01]);
+
+void u_debug_draw_char(u32 x, u32 y, const u8 *c) {
+       u32 i;
+       for (i = 0; i < U_DEBUG_FONT_H; i++) {
+               u_debug_draw_pixel(x, y, i, c, 0);
+               u_debug_draw_pixel(x, y, i, c, 1);
+               u_debug_draw_pixel(x, y, i, c, 2);
+               u_debug_draw_pixel(x, y, i, c, 3);
+               u_debug_draw_pixel(x, y, i, c, 4);
+               u_debug_draw_pixel(x, y, i, c, 5);
+               u_debug_draw_pixel(x, y, i, c, 6);
+               u_debug_draw_pixel(x, y, i, c, 7);
+       }
+}
+
+void u_debug_draw_text(u32 x, u32 y, const char *fmt, ...) {
+       u32 i = 0;
+       u32 len = 0;
+       char c = 0;
+       char buffer[2048];
+       va_list args;
+
+       va_start(args, fmt);
+       len = u_vsprintf(buffer, fmt, args);
+       va_end(args);
+       
+       LRTS_UNUSED(x);
+       LRTS_UNUSED(y);
+
+       for (i = 0; i < len; i++) {
+               c = buffer[i];
+               if (c >= '0' && c <= '9') {
+                       u_debug_draw_char(x+U_DEBUG_FONT_W*2*i, y, 
+                                       debug_font_num + (u32)(c - '0') * U_DEBUG_FONT_H);
+               }
+       }
+       
+}
+
diff --git a/src/u_debug.h b/src/u_debug.h
new file mode 100644 (file)
index 0000000..6f89fe6
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef U_DEBUG_H__
+#define U_DEBUG_H__
+
+#define U_DEBUG_FONT_BYTES 8
+
+/**
+ * Daws debug text to the screen
+ */
+void u_debug_draw_text(u32 x, u32 y, const char *fmt, ...);
+
+#endif