Skip to content

Commit 385f966

Browse files
authored
Re-write Timer class (#15)
Re-write `Timer` class to fire a user-defined callback function at a regular user-defined interval (i.e. make `Timer` generic and thus more reusable). * Also extracted the notion of frame time from the class since that's really more of an `Engine` concern.
1 parent ae85436 commit 385f966

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

MP-APS/Engine.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Engine.h"
22

3+
#include "Timer.h"
34
#include "Input.h"
45
#include "ViewFrustum.h"
56
#include "ResourceManager.h"
@@ -31,6 +32,16 @@ void connectWindowInstanceToInput(GLFWwindow* window) {
3132
glfwSetCursorPosCallback(window, cursorPosCallback);
3233
}
3334

35+
/***********************************************************************************/
36+
double frameTimeMilliseconds(const unsigned int numFramesRendered) {
37+
return 1000.0 / static_cast<double>(numFramesRendered);
38+
}
39+
40+
/***********************************************************************************/
41+
double framesPerSecond(const double frameTimeMilliseconds) {
42+
return 1.0 / (frameTimeMilliseconds / 1000.0);
43+
}
44+
3445
/***********************************************************************************/
3546
Engine::Engine(const std::filesystem::path& configPath) {
3647

@@ -92,11 +103,28 @@ void Engine::Execute() {
92103
std::cout << "Engine initialization complete!\n";
93104
std::cout << "**************************************************\n";
94105

106+
bool hasOneSecondPassed{ false };
107+
Timer timer(1.0, [&]() {
108+
hasOneSecondPassed = true;
109+
});
110+
95111
// Main loop
112+
unsigned int numFramesRendered{ 0 };
96113
while (!m_window.ShouldClose()) {
114+
timer.Update(glfwGetTime());
115+
116+
if (hasOneSecondPassed) {
117+
const auto frameTime{ frameTimeMilliseconds(numFramesRendered) };
118+
std::cout <<
119+
"Frame time: " <<
120+
frameTime << " ms - " <<
121+
framesPerSecond(frameTime) << " fps\n";
122+
123+
numFramesRendered = 0;
124+
hasOneSecondPassed = false;
125+
}
97126

98-
m_timer.Update(glfwGetTime());
99-
const auto dt{ m_timer.GetDelta() };
127+
const auto dt{ timer.GetDelta() };
100128

101129
Input::GetInstance().Update();
102130

@@ -114,6 +142,8 @@ void Engine::Execute() {
114142
m_guiSystem.Render();
115143

116144
m_window.SwapBuffers();
145+
146+
++numFramesRendered;
117147
}
118148

119149
shutdown();

MP-APS/Engine.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
2-
#include "Timer.h"
32
#include "Camera.h"
43

54
#include "Core/WindowSystem.h"
@@ -27,7 +26,6 @@ class Engine {
2726
// Returns meshes visible by the camera.
2827
std::vector<ModelPtr> cullViewFrustum() const;
2928

30-
Timer m_timer;
3129
Camera m_camera;
3230

3331
// Core Systems

MP-APS/Timer.cpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

MP-APS/Timer.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
#pragma once
2-
#include <cstdint>
2+
3+
#include <functional>
34

45
class Timer {
6+
using TimerCallbackFunc = std::function<void()>;
57
public:
6-
Timer() noexcept;
8+
9+
Timer(const double secondsBetweenCallbackTriggers, TimerCallbackFunc callback)
10+
: m_secondsBetweenCallbackTriggers{ secondsBetweenCallbackTriggers },
11+
m_callbackFunc{ callback } {}
12+
13+
void Update(const double time) {
14+
if (time - m_prevTriggerTime >= m_secondsBetweenCallbackTriggers) {
15+
m_callbackFunc();
16+
17+
m_prevTriggerTime += m_secondsBetweenCallbackTriggers;
18+
}
719

8-
Timer(const Timer&) = delete;
9-
Timer& operator=(const Timer&) = delete;
20+
m_delta = time - m_prevTime;
21+
m_prevTime = time;
22+
}
1023

11-
void Update(const double time) noexcept;
12-
auto GetDelta() const noexcept { return m_delta; }
24+
auto GetDelta() const noexcept {
25+
return m_delta;
26+
}
1327

1428
private:
15-
double m_delta, m_lastFrame, m_lastTime;
16-
uint32_t m_nbFrames;
29+
double m_prevTriggerTime{ 0.0 }, m_prevTime{ 0.0 }, m_delta{ 0.0 };
30+
const double m_secondsBetweenCallbackTriggers;
31+
const TimerCallbackFunc m_callbackFunc;
1732
};

0 commit comments

Comments
 (0)