From 6a58b4a8ba878281f7bddba60c8092843d9574fa Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Thu, 11 Sep 2025 23:21:37 +0100 Subject: [PATCH] feat(engine): Implement delta time calculation. --- src/main.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index c448ce5..6a98fbf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,9 @@ #include #include #include +#include +#include +#include #include const int SCREEN_WIDTH = 800; @@ -59,7 +62,21 @@ int main(int argc, char* argv[]) { bool is_running = true; SDL_Event event; + /* Delta time calculations. */ + Uint64 perf_frequency = SDL_GetPerformanceFrequency(); + Uint64 last_counter = SDL_GetPerformanceCounter(); + double delta_time = 0; + + /* FPS counter for window title. */ + char window_title[256]; + double time_since_title_update = 0.0; + while(is_running) { + /* Calc delta time. */ + Uint64 current_counter = SDL_GetPerformanceCounter(); + delta_time = (double)(current_counter-last_counter) / (double)perf_frequency; + last_counter = current_counter; + /* Event handling. */ while(SDL_PollEvent(&event)) { if(event.type == SDL_EVENT_QUIT) { @@ -67,6 +84,14 @@ int main(int argc, char* argv[]) { } } + /* Update window title with FPS counter. */ + time_since_title_update += delta_time; + if(time_since_title_update > 0.25) { + snprintf(window_title, 256, "Bettola - FPS: %.2f", 1.0/delta_time); + SDL_SetWindowTitle(window, window_title); + time_since_title_update = 0.0; + } + /* Render that b*tch. */ glClearColor(0.1f, 0.1f, 0.3f, 1.0f); /* Dark blue or something. */ glClear(GL_COLOR_BUFFER_BIT);