From ae522200206dc430101ba20f27f8a3f194e0ee88 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 31 Jul 2023 15:41:09 +0900 Subject: [PATCH] Impl. getting delta-time per frame Using temporary variable to cache dt to display, will be removed later. --- .gitignore | 2 ++ src/game.cc | 20 ++++++++++++++++++-- src/game.h | 7 +++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index fc27882..cf39983 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /demo_0 /objdir/ +compile_commands.json +/.cache/ diff --git a/src/game.cc b/src/game.cc index 41ce4ae..c290b9d 100644 --- a/src/game.cc +++ b/src/game.cc @@ -1,15 +1,31 @@ #include "game.h" +// standard library includes +#include + // third party includes #include -Game::Game() {} +Game::Game() + : prev_time(std::chrono::steady_clock::now()), TEMP_cached_dt(0.0F) {} -void Game::update() {} +void Game::update() { + auto next_time = std::chrono::steady_clock::now(); + auto duration = std::chrono::duration_cast( + next_time - prev_time); + prev_time = next_time; + update_impl(((float)duration.count()) / 1000000); +} void Game::draw() { + std::string dt_string = + std::string("Delta-time: ") + std::to_string(TEMP_cached_dt); + BeginDrawing(); ClearBackground(BLACK); DrawText("Testing...", 100, 100, 30, RAYWHITE); + DrawText(dt_string.c_str(), 100, 140, 30, RAYWHITE); EndDrawing(); } + +void Game::update_impl(float dt) { TEMP_cached_dt = dt; } diff --git a/src/game.h b/src/game.h index 84f5e9f..413641b 100644 --- a/src/game.h +++ b/src/game.h @@ -1,6 +1,9 @@ #ifndef JUMPARTIFACT_DOT_COM_DEMO_GAME_H_ #define JUMPARTIFACT_DOT_COM_DEMO_GAME_H_ +// standard library includes +#include + class Game { public: Game(); @@ -17,6 +20,10 @@ class Game { void draw(); private: + void update_impl(float dt); + + std::chrono::steady_clock::time_point prev_time; + float TEMP_cached_dt; }; #endif