From 86c0be9dbbc675bad5de47781364c61b29f9bf11 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Mon, 16 Sep 2024 18:33:33 +0200 Subject: [PATCH] Implement simple standard timing utilities --- Engine/Utils/Timers.hpp | 35 +++++++++++++++++++++++++++++++++++ Game.cpp | 15 ++++++++++++--- Game.hpp | 7 ++++--- ProtoTank.cpp | 16 ++++++++++------ srcs.list | 1 + 5 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 Engine/Utils/Timers.hpp diff --git a/Engine/Utils/Timers.hpp b/Engine/Utils/Timers.hpp new file mode 100644 index 0000000..725691b --- /dev/null +++ b/Engine/Utils/Timers.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include + +class SysTimer final { +public: + SysTimer() noexcept { + auto curr_time = std::chrono::high_resolution_clock::now(); + t_start = curr_time; + t_previousDelta = curr_time; + } + ~SysTimer() {} + + const double GetElapsedTime() const { + return (double)(std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - t_start).count()); + } + const double GetElapsedTimeS() const { + return GetElapsedTime() / 1000000; + } + const double GetDeltaTime() { + return GetDeltaTime(false); + } + const double GetDeltaTime(bool reset) { + auto curr_time = std::chrono::high_resolution_clock::now(); + double ret = std::chrono::duration_cast(curr_time - t_previousDelta).count(); + if (reset) + this->t_previousDelta = curr_time; + return ret; + } + +private: + std::chrono::time_point t_start; + std::chrono::time_point t_previousDelta; + +}; \ No newline at end of file diff --git a/Game.cpp b/Game.cpp index 6a9166f..f44c9a9 100644 --- a/Game.cpp +++ b/Game.cpp @@ -32,7 +32,7 @@ void Game::InitWindows() { gMainWindow->setMouseCursorVisible(true); } -GAME_STATUS Game::Update() { +GAME_STATUS Game::Update(SysTimer& time) { gLoopTimer.restart(); sf::Event event; @@ -53,8 +53,17 @@ GAME_STATUS Game::Update() { return GAME_QUIT; } -void Game::Render() { +void Game::Render(SysTimer& time) { + // Clear the draw buffer gMainWindow->clear(); - gDbgUI->UpdateDebugData(gMainWindow); + + // Draw the arena view + //3DRenderer::draw() + // Draw the UI above + //UI::draw() + // Draw the debug informations if enabled + if (gDbgUI != nullptr) gDbgUI->UpdateDebugData(gMainWindow); + + // Present the draw buffer gMainWindow->display(); } \ No newline at end of file diff --git a/Game.hpp b/Game.hpp index c3d5e7d..1846af6 100644 --- a/Game.hpp +++ b/Game.hpp @@ -6,6 +6,7 @@ #include #include "Engine/Graphics/DebugUI.hpp" +#include "Engine/Utils/Timers.hpp" typedef enum eGameStatus { @@ -14,7 +15,7 @@ typedef enum eGameStatus { GAME_QUIT } GAME_STATUS; -class Game final{ +class Game final { public: Game(bool dbg) noexcept(false); ~Game() {} @@ -24,8 +25,8 @@ public: Game(Game const&) = delete; Game& operator= (Game const&) = delete; - GAME_STATUS Update(); - void Render(); + GAME_STATUS Update(SysTimer& time); + void Render(SysTimer& time); private: void InitWindows(); diff --git a/ProtoTank.cpp b/ProtoTank.cpp index 4e32c06..efe48d4 100644 --- a/ProtoTank.cpp +++ b/ProtoTank.cpp @@ -1,29 +1,33 @@ #include +#include #include -//#include #include #include #include "Game.hpp" +#include "Engine/Utils/Timers.hpp" -using std::cout; -using std::endl; +using std::cout, std::endl, std::fixed, std::setprecision; int main(int argc, char** argv) { + SysTimer mSysTimer; + cout << fixed << setprecision(6); + GAME_STATUS status = GAME_INIT; - cout << "Init game instance" << endl; + cout << "[" << mSysTimer.GetElapsedTimeS() << "] Init game instance" << endl; auto arcadeGame = std::make_unique(true); for ( ;; ) { - status = arcadeGame->Update(); + status = arcadeGame->Update(mSysTimer); if (status == GAME_QUIT) break; - arcadeGame->Render(); + arcadeGame->Render(mSysTimer); } + cout << "[" << mSysTimer.GetElapsedTimeS() << "] Bye bye!" << endl; return EXIT_SUCCESS; } diff --git a/srcs.list b/srcs.list index 5725253..99c2e63 100644 --- a/srcs.list +++ b/srcs.list @@ -6,6 +6,7 @@ set(MAIN_SCRS set(UTILS_SCRS Engine/Utils/3DMaths.cpp Engine/Utils/3DMaths.hpp + Engine/Utils/Timers.hpp ) set(MISC_SCRS Engine/Misc/Console.cpp