From dda30a73d03a79956f6af297914d07f77d530807 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Thu, 26 Feb 2026 20:33:56 +0100 Subject: [PATCH] debug font: wip adding debug 1bpp embedded font --- makefile | 2 +- src/lrts_impl.h | 1 + src/p_pc/u_stdio.c | 2 +- src/p_platform.h | 4 --- src/p_r_sdl/p_draw.c | 21 ------------ src/p_r_sdl/p_init.c | 23 -------------- src/p_r_sdl/p_r_sdl.h | 4 --- src/r_render.c | 8 +++-- src/u_debug.c | 74 +++++++++++++++++++++++++++++++++++++++++++ src/u_debug.h | 11 +++++++ 10 files changed, 93 insertions(+), 57 deletions(-) create mode 100644 src/u_debug.c create mode 100644 src/u_debug.h diff --git a/makefile b/makefile index 93b12fc..7d9d920 100644 --- 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 diff --git a/src/lrts_impl.h b/src/lrts_impl.h index 43821dc..9fccfc2 100644 --- a/src/lrts_impl.h +++ b/src/lrts_impl.h @@ -52,6 +52,7 @@ #include "i_input.c" #include "u_time.c" #include "t_update.c" +#include "u_debug.c" #endif diff --git a/src/p_pc/u_stdio.c b/src/p_pc/u_stdio.c index 87bcab8..11b91dd 100644 --- a/src/p_pc/u_stdio.c +++ b/src/p_pc/u_stdio.c @@ -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; diff --git a/src/p_platform.h b/src/p_platform.h index 5b08a35..dd50baa 100644 --- a/src/p_platform.h +++ b/src/p_platform.h @@ -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, ...); /** diff --git a/src/p_r_sdl/p_draw.c b/src/p_r_sdl/p_draw.c index e27e3d7..5f2ca67 100644 --- a/src/p_r_sdl/p_draw.c +++ b/src/p_r_sdl/p_draw.c @@ -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); -} diff --git a/src/p_r_sdl/p_init.c b/src/p_r_sdl/p_init.c index 3a74914..4f3f855 100644 --- a/src/p_r_sdl/p_init.c +++ b/src/p_r_sdl/p_init.c @@ -1,6 +1,5 @@ #include "../p_platform.h" #include -#include #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; diff --git a/src/p_r_sdl/p_r_sdl.h b/src/p_r_sdl/p_r_sdl.h index 5b3b57e..28abf69 100644 --- a/src/p_r_sdl/p_r_sdl.h +++ b/src/p_r_sdl/p_r_sdl.h @@ -1,9 +1,5 @@ #ifndef P_R_SDL_H__ #define P_R_SDL_H__ -#include - -extern TTF_Font *p_r_sdl_debug_font; -extern TTF_TextEngine *p_r_sdl_text_engine; #endif diff --git a/src/r_render.c b/src/r_render.c index 8051914..c7d89d0 100644 --- a/src/r_render.c +++ b/src/r_render.c @@ -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 index 0000000..7ba3918 --- /dev/null +++ b/src/u_debug.c @@ -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 index 0000000..6f89fe6 --- /dev/null +++ b/src/u_debug.h @@ -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 -- 2.30.2