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);
}
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();
}

View File

@ -6,6 +6,7 @@
#include <SFML/Graphics/RenderWindow.hpp>
#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();

View File

@ -1,29 +1,33 @@
#include <iostream>
#include <iomanip>
#include <stdexcept>
//#include <csignal>
#include <vector>
#include <memory>
#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<Game>(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;
}

View File

@ -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