From: Lukas Krickl Date: Thu, 26 Feb 2026 12:58:08 +0000 (+0100) Subject: debug: Added debug flag and fps state X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=0011c44b888eae9f78755547eca4af07ebc0dfb9;p=lrts%2F.git debug: Added debug flag and fps state 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. --- diff --git a/makefile b/makefile index 9fbcdf6..93b12fc 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 +LIBS=-lSDL3 -lSDL3_ttf LDFLAGS=$(DBGLDFLAGS) $(LIBS) INSTALL_DIR=/usr/local diff --git a/src/lrts.c b/src/lrts.c index 30caf04..aebc6e3 100644 --- a/src/lrts.c +++ b/src/lrts.c @@ -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) { diff --git a/src/lrts_impl.h b/src/lrts_impl.h index b9815a6..43821dc 100644 --- a/src/lrts_impl.h +++ b/src/lrts_impl.h @@ -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 index 0000000..7d1fd18 --- /dev/null +++ b/src/p_pc/u_string.c @@ -0,0 +1,18 @@ +#include "../u_string.h" +#include + +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; +} diff --git a/src/p_platform.h b/src/p_platform.h index fa02758..5b08a35 100644 --- a/src/p_platform.h +++ b/src/p_platform.h @@ -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 diff --git a/src/p_r_sdl/p_draw.c b/src/p_r_sdl/p_draw.c index fdb0c03..e27e3d7 100644 --- a/src/p_r_sdl/p_draw.c +++ b/src/p_r_sdl/p_draw.c @@ -1,7 +1,9 @@ #include "../p_draw.h" #include "p_window.h" #include "../u_assert.h" - +#include +#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); +} diff --git a/src/p_r_sdl/p_init.c b/src/p_r_sdl/p_init.c index 4f3f855..3a74914 100644 --- a/src/p_r_sdl/p_init.c +++ b/src/p_r_sdl/p_init.c @@ -1,5 +1,6 @@ #include "../p_platform.h" #include +#include #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 index 0000000..5b3b57e --- /dev/null +++ b/src/p_r_sdl/p_r_sdl.h @@ -0,0 +1,9 @@ +#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 8585a29..8051914 100644 --- a/src/r_render.c +++ b/src/r_render.c @@ -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(); } diff --git a/src/u_defs.h b/src/u_defs.h index 4d5b129..68a2e0a 100644 --- a/src/u_defs.h +++ b/src/u_defs.h @@ -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 index 0000000..3b191b7 --- /dev/null +++ b/src/u_string.h @@ -0,0 +1,9 @@ +#ifndef U_STRING_H__ +#define U_STRING_H__ + +#include + +int u_vsprintf(char *buffer, const char *fmt, va_list args); +int u_sprintf(char *buffer, const char *fmt, ...); + +#endif