Implement simple standard timing utilities

This commit is contained in:
JackCarterSmith 2024-09-16 18:33:33 +02:00
parent cb5d96c669
commit 86c0be9dbb
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
5 changed files with 62 additions and 12 deletions

35
Engine/Utils/Timers.hpp Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include <chrono>
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::microseconds>(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<std::chrono::microseconds>(curr_time - t_previousDelta).count();
if (reset)
this->t_previousDelta = curr_time;
return ret;
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> t_start;
std::chrono::time_point<std::chrono::high_resolution_clock> t_previousDelta;
};

View File

@ -32,7 +32,7 @@ void Game::InitWindows() {
gMainWindow->setMouseCursorVisible(true); gMainWindow->setMouseCursorVisible(true);
} }
GAME_STATUS Game::Update() { GAME_STATUS Game::Update(SysTimer& time) {
gLoopTimer.restart(); gLoopTimer.restart();
sf::Event event; sf::Event event;
@ -53,8 +53,17 @@ GAME_STATUS Game::Update() {
return GAME_QUIT; return GAME_QUIT;
} }
void Game::Render() { void Game::Render(SysTimer& time) {
// Clear the draw buffer
gMainWindow->clear(); 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(); gMainWindow->display();
} }

View File

@ -6,6 +6,7 @@
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include "Engine/Graphics/DebugUI.hpp" #include "Engine/Graphics/DebugUI.hpp"
#include "Engine/Utils/Timers.hpp"
typedef enum eGameStatus { typedef enum eGameStatus {
@ -14,7 +15,7 @@ typedef enum eGameStatus {
GAME_QUIT GAME_QUIT
} GAME_STATUS; } GAME_STATUS;
class Game final{ class Game final {
public: public:
Game(bool dbg) noexcept(false); Game(bool dbg) noexcept(false);
~Game() {} ~Game() {}
@ -24,8 +25,8 @@ public:
Game(Game const&) = delete; Game(Game const&) = delete;
Game& operator= (Game const&) = delete; Game& operator= (Game const&) = delete;
GAME_STATUS Update(); GAME_STATUS Update(SysTimer& time);
void Render(); void Render(SysTimer& time);
private: private:
void InitWindows(); void InitWindows();

View File

@ -1,29 +1,33 @@
#include <iostream> #include <iostream>
#include <iomanip>
#include <stdexcept> #include <stdexcept>
//#include <csignal>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "Game.hpp" #include "Game.hpp"
#include "Engine/Utils/Timers.hpp"
using std::cout; using std::cout, std::endl, std::fixed, std::setprecision;
using std::endl;
int main(int argc, char** argv) { int main(int argc, char** argv) {
SysTimer mSysTimer;
cout << fixed << setprecision(6);
GAME_STATUS status = GAME_INIT; GAME_STATUS status = GAME_INIT;
cout << "Init game instance" << endl; cout << "[" << mSysTimer.GetElapsedTimeS() << "] Init game instance" << endl;
auto arcadeGame = std::make_unique<Game>(true); auto arcadeGame = std::make_unique<Game>(true);
for ( ;; ) { for ( ;; ) {
status = arcadeGame->Update(); status = arcadeGame->Update(mSysTimer);
if (status == GAME_QUIT) if (status == GAME_QUIT)
break; break;
arcadeGame->Render(); arcadeGame->Render(mSysTimer);
} }
cout << "[" << mSysTimer.GetElapsedTimeS() << "] Bye bye!" << endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -6,6 +6,7 @@ set(MAIN_SCRS
set(UTILS_SCRS set(UTILS_SCRS
Engine/Utils/3DMaths.cpp Engine/Utils/3DMaths.cpp
Engine/Utils/3DMaths.hpp Engine/Utils/3DMaths.hpp
Engine/Utils/Timers.hpp
) )
set(MISC_SCRS set(MISC_SCRS
Engine/Misc/Console.cpp Engine/Misc/Console.cpp