From a023fcefd28312d7055709b77de5ec7b74c508fb Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 8 Feb 2026 18:30:16 -0800 Subject: Report performance counters every one second --- src/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 86ab2ae..d0696d0 100644 --- a/src/main.c +++ b/src/main.c @@ -72,12 +72,14 @@ typedef struct State { SDL_Window* window; void* gfx_mem; swgfx* gfx; + Uint64 last_tick; + R stats_elapsed; Camera camera; CameraController camera_controller; - Uint64 last_tick; Model* model; size_t numTextures; sgImage textures[MaxTextures]; + sgCounters last_stats; } State; static bool StateWrite(const State* state, FILE* file) { @@ -121,10 +123,34 @@ static bool StateLoad(const char* path, State* state) { return result; } +static inline R frac(R a) { return a - (R)((int)a); } + static sgVec3 SgVec3FromMathVec3(vec3 v) { return (sgVec3){v.x, v.y, v.z}; } +static void ShowStats(const sgCounters* last_stats, const sgCounters* stats, R time_elapsed) { + assert(last_stats); + assert(stats); + if (stats->frames == last_stats->frames) { + return; + } + assert(time_elapsed >= 0.f); + + const uint64_t frames_elapsed = stats->frames - last_stats->frames; +#define RATE_PER_SEC(STAT) (int)((R)(stats->STAT - last_stats->STAT) / time_elapsed) +#define RATE_PER_FRAME(STAT) (int)((stats->STAT - last_stats->STAT) / frames_elapsed) + const int fps = RATE_PER_SEC(frames); + const int pps = RATE_PER_SEC(pixels); + const int tps = RATE_PER_SEC(triangles3); + const int ppf = RATE_PER_FRAME(pixels); + const int tpf = RATE_PER_FRAME(triangles3); +#undef RATE_PER_FRAME +#undef RATE_PER_SEC + + printf("fps: %d, pps: %d, tps: %d, ppf: %d, tpf: %d\n", fps, pps, tps, ppf, tpf); +} + static CameraCommand CameraCommandFromInput( const bool* keyboard_state, const SDL_MouseButtonFlags mouse_flags) { assert(keyboard_state); @@ -190,6 +216,14 @@ static bool Update(State* state, R dt) { const CameraCommand cmd = CameraCommandFromInput(keyboard_state, mouse_flags); UpdateCamera(&state->camera_controller, dt, mouse, cmd, &state->camera); + state->stats_elapsed += dt; + if (state->stats_elapsed >= 1.f) { + const sgCounters stats = sgGetCounters(state->gfx); + ShowStats(&state->last_stats, &stats, state->stats_elapsed); + state->last_stats = stats; + state->stats_elapsed = frac(state->stats_elapsed); + } + return true; } @@ -401,6 +435,7 @@ static bool Initialize(State* state) { }; state->last_tick = SDL_GetPerformanceCounter(); + state->stats_elapsed = 0.f; return true; } @@ -436,13 +471,18 @@ static void Shutdown(State* state) { } } -static R GetDeltaTime(State* state) { - assert(state); +static R GetDeltaTimeSec(Uint64 before, Uint64 after) { constexpr Uint64 NS_IN_SEC = 1'000'000'000; - const Uint64 this_tick = SDL_GetPerformanceCounter(); const Uint64 freq = SDL_GetPerformanceFrequency(); - const Uint64 elapsed_ns = (this_tick - state->last_tick) * NS_IN_SEC / freq; + const Uint64 elapsed_ns = (after - before) * NS_IN_SEC / freq; const R elapsed_sec = (R)elapsed_ns / (R)NS_IN_SEC; + return elapsed_sec; +} + +static R GetFrameDeltaTime(State* state) { + assert(state); + const Uint64 this_tick = SDL_GetPerformanceCounter(); + const R elapsed_sec = GetDeltaTimeSec(state->last_tick, this_tick); state->last_tick = this_tick; return elapsed_sec; } @@ -532,7 +572,7 @@ int main() { // Draw and update if needed. if (success && running) { - const R dt = GetDeltaTime(&state); + const R dt = GetFrameDeltaTime(&state); success = Update(&state, dt); } if (success && running) { -- cgit v1.2.3