Repository navigation
Expand file tree
/
Copy pathTime.cpp
More file actions
84 lines (77 loc) · 1.43 KB
/
Copy pathTime.cpp
File metadata and controls
84 lines (77 loc) · 1.43 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
#include "Time.h"
namespace Time
{
namespace
{
std::vector<std::shared_ptr<TimeTracker>> trackers;
float deltaTime = 0.0f;
auto last = std::chrono::steady_clock::now();
float average[75] = { 0.0f }; int i = 0;
}
std::shared_ptr<TimeTracker> RequestTracker(float period)
{
auto tracker = std::make_shared<TimeTracker>(period);
trackers.emplace_back(tracker);
return tracker;
}
void Time::CalculateDeltaTime()
{
const auto old = last;
last = std::chrono::steady_clock::now();
const std::chrono::duration<float> t = last - old;
deltaTime = t.count();
for (auto t : trackers)
t->CalculateTime();
average[i] = (1.0f / deltaTime);
i = (i + 1) % 75;
}
const float GetDeltatime()
{
return deltaTime;
}
const float GetTime()
{
return std::chrono::duration<float>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
float FPS()
{
float sum = 0.0f;
for (int x = 0; x < 75; x++)
{
sum += average[x];
}
return sum / 75.0f;
}
TimeTracker::TimeTracker(float _limit)
:
period(_limit)
{
timePassed = 0.0f;
}
void TimeTracker::CalculateTime()
{
timePassed += deltaTime;
}
float TimeTracker::GetPeriod()
{
return period;
}
bool TimeTracker::Check()
{
if (timePassed >= 2 * period)
{
timePassed -= period;
return true;
}
return false;
}
bool TimeTracker::CheckOnce()
{
if (timePassed >= period)
{
timePassed = 0.0f;
return true;
}
return false;
}
}