]> git.seodisparate.com - jumpartifact.com_demo_0/commitdiff
Impl. getting delta-time per frame
authorStephen Seo <seo.disparate@gmail.com>
Mon, 31 Jul 2023 06:41:09 +0000 (15:41 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 31 Jul 2023 06:41:09 +0000 (15:41 +0900)
Using temporary variable to cache dt to display, will be removed later.

.gitignore
src/game.cc
src/game.h

index fc278822d5993cc50faf8b7effdf6bb6e28317b3..cf3998301af042d16a8a9fc96dcdb6a154ce6362 100644 (file)
@@ -1,2 +1,4 @@
 /demo_0
 /objdir/
+compile_commands.json
+/.cache/
index 41ce4aeea11f6307cc68b464ef6e16ccce08826a..c290b9daa27c5ba7c27a643d7c73329fd9b45ab2 100644 (file)
@@ -1,15 +1,31 @@
 #include "game.h"
 
+// standard library includes
+#include <string>
+
 // third party includes
 #include <raylib.h>
 
-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<std::chrono::microseconds>(
+      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; }
index 84f5e9fe25d8b92037a978770445b100a19e7aaf..413641bf966048c3c98a3e844b61081405cba148 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef JUMPARTIFACT_DOT_COM_DEMO_GAME_H_
 #define JUMPARTIFACT_DOT_COM_DEMO_GAME_H_
 
+// standard library includes
+#include <chrono>
+
 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