-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
143 lines (114 loc) · 3.05 KB
/
Timer.cpp
File metadata and controls
143 lines (114 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "Timer.h"
#include <iostream>
#include <numeric>
#include <iostream>
#include <fstream>
#include "SDL.h"
using namespace dae;
Timer::Timer()
{
const uint64_t countsPerSecond = SDL_GetPerformanceFrequency();
m_SecondsPerCount = 1.0f / static_cast<float>(countsPerSecond);
}
void Timer::Reset()
{
const uint64_t currentTime = SDL_GetPerformanceCounter();
m_BaseTime = currentTime;
m_PreviousTime = currentTime;
m_StopTime = 0;
m_FPSTimer = 0.0f;
m_FPSCount = 0;
m_IsStopped = false;
}
void Timer::Start()
{
const uint64_t startTime = SDL_GetPerformanceCounter();
if (m_IsStopped)
{
m_PausedTime += (startTime - m_StopTime);
m_PreviousTime = startTime;
m_StopTime = 0;
m_IsStopped = false;
}
}
void Timer::StartBenchmark(int numFrames)
{
if (m_BenchmarkActive)
{
std::cout << "(Benchmark already running)";
return;
}
m_BenchmarkActive = true;
m_BenchmarkAvg = 0.f;
m_BenchmarkHigh = FLT_MIN;
m_BenchmarkLow = FLT_MAX;
m_BenchmarkFrames = numFrames;
m_BenchmarkCurrFrame = 0;
m_Benchmarks.clear();
m_Benchmarks.resize(m_BenchmarkFrames);
std::cout<< "**BENCHMARK STARTED**\n";
}
void Timer::Update()
{
if (m_IsStopped)
{
m_FPS = 0;
m_ElapsedTime = 0.0f;
m_TotalTime = (float)(((m_StopTime - m_PausedTime) - m_BaseTime) * m_BaseTime);
return;
}
const uint64_t currentTime = SDL_GetPerformanceCounter();
m_CurrentTime = currentTime;
m_ElapsedTime = (float)((m_CurrentTime - m_PreviousTime) * m_SecondsPerCount);
m_PreviousTime = m_CurrentTime;
if (m_ElapsedTime < 0.0f)
m_ElapsedTime = 0.0f;
if (m_ForceElapsedUpperBound && m_ElapsedTime > m_ElapsedUpperBound)
{
m_ElapsedTime = m_ElapsedUpperBound;
}
m_TotalTime = (float)(((m_CurrentTime - m_PausedTime) - m_BaseTime) * m_SecondsPerCount);
//FPS LOGIC
m_FPSTimer += m_ElapsedTime;
++m_FPSCount;
if (m_FPSTimer >= 1.0f)
{
m_dFPS = m_FPSCount / m_FPSTimer;
m_FPS = m_FPSCount;
m_FPSCount = 0;
m_FPSTimer = 0.0f;
if (m_BenchmarkActive)
{
m_Benchmarks[m_BenchmarkCurrFrame] = m_dFPS;
m_BenchmarkLow = std::min(m_BenchmarkLow, m_dFPS);
m_BenchmarkHigh = std::max(m_BenchmarkHigh, m_dFPS);
++m_BenchmarkCurrFrame;
if(m_BenchmarkCurrFrame >= m_BenchmarkFrames)
{
m_BenchmarkActive = false;
m_BenchmarkAvg = std::accumulate(m_Benchmarks.begin(), m_Benchmarks.end(), 0.f) / float(m_BenchmarkFrames);
//print
std::cout << "**BENCHMARK FINISHED**\n";
std::cout << ">> HIGH = " << m_BenchmarkHigh << std::endl;
std::cout << ">> LOW = " << m_BenchmarkLow << std::endl;
std::cout << ">> AVG = " << m_BenchmarkAvg << std::endl;
//file save
std::ofstream fileStream("benchmark.txt");
fileStream << "FRAMES = " << m_BenchmarkCurrFrame << std::endl;
fileStream << "HIGH = " << m_BenchmarkHigh << std::endl;
fileStream << "LOW = " << m_BenchmarkLow << std::endl;
fileStream << "AVG = " << m_BenchmarkAvg << std::endl;
fileStream.close();
}
}
}
}
void Timer::Stop()
{
if (!m_IsStopped)
{
const uint64_t currentTime = SDL_GetPerformanceCounter();
m_StopTime = currentTime;
m_IsStopped = true;
}
}