Impl. getting delta-time per frame

Using temporary variable to cache dt to display, will be removed later.
This commit is contained in:
Stephen Seo 2023-07-31 15:41:09 +09:00
parent 06d882609b
commit ae52220020
3 changed files with 27 additions and 2 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
/demo_0
/objdir/
compile_commands.json
/.cache/

View 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; }

View 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