From 9619843cbed783774f9593e7ab6fc0b6efd4a420 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Tue, 4 Aug 2026 01:01:55 +0200 Subject: [PATCH] Changed time from ms to ns and showing fps to header bar when debug. --- src/display.c | 33 ++++++++++++++++++++++++++++++++- src/main.c | 6 +++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/display.c b/src/display.c index d59f092..7de419d 100644 --- a/src/display.c +++ b/src/display.c @@ -3,6 +3,7 @@ #include #include #include "asset_manager.h" +#include "game.h" #include "errors.h" SDL_Window *window = NULL; @@ -16,7 +17,7 @@ inline int display_init(void) return EXIT_FAILURE; } - if(!SDL_SetAppMetadata("2D_Engine", "1.0", "2D_Engine")) + if(!SDL_SetAppMetadata("2D_Engine", "1.0", "2D_Engine_SDL2_Core")) { error_printf("Failed to set app metadata : %s", SDL_GetError()); return EXIT_FAILURE; @@ -94,6 +95,36 @@ inline int display_clear(void) inline void display_update(void) { SDL_RenderPresent(renderer); + +#if DEBUG >= 1 + float fps = 1000.0f / game.delta_time_ms; + static float max_fps_10s = 0.0f; + static float low_fps_10s = 10000000.0f; + + if(fps > max_fps_10s) max_fps_10s = fps; + if(fps < low_fps_10s) low_fps_10s = fps; + + static float fps_update_delta_time = 0.0f; + fps_update_delta_time += game.delta_time_ms; + + if(fps_update_delta_time >= 100.0f) + { + fps_update_delta_time = 0.0f; + char title[100]; + sprintf(title, "2D_Engine | cur: %.2ffps | max: %.2ffps | low: %.2ffps", fps, max_fps_10s, low_fps_10s); + SDL_SetWindowTitle(window, title); + } + + static float fps_max_low_update_delta_time = 0.0f; + fps_max_low_update_delta_time += game.delta_time_ms; + + if(fps_max_low_update_delta_time > 10000.0f) + { + fps_max_low_update_delta_time = 0.0f; + max_fps_10s = 0.0f; + low_fps_10s = 10000000.0f; + } +#endif // DEBUG >= 1 } int display_draw_texture(texture_t *texture, rect_t *src_rect, rect_t *dst_rect, bool flip) diff --git a/src/main.c b/src/main.c index 69e4c75..02d9a04 100644 --- a/src/main.c +++ b/src/main.c @@ -7,9 +7,9 @@ game_t game; static inline void update_time(void) { - static uint32_t last_clock_state = 0; - uint32_t start_clock = SDL_GetTicks(); - game.delta_time_ms = (float)(start_clock - last_clock_state); + static uint64_t last_clock_state = 0; + uint64_t start_clock = SDL_GetTicksNS(); + game.delta_time_ms = SDL_NS_TO_MS((float)(start_clock - last_clock_state)); last_clock_state = start_clock; }