-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
77 lines (71 loc) · 1.67 KB
/
Timer.h
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
#ifndef FAGL_TIMER_H
#define FAGL_TIMER_H
#include "FAGL.h"
#include <map>
class Time_Manager {
private:
std::map<unsigned int, float> timestamps;
float fpsLimit = 0.0f;
bool fpsLimitEnabled = true;
float* deltaTimePointer = NULL;
unsigned int fpsTimer = 0;
public:
void UpdateTimeStamp(unsigned int i) {
timestamps[i] = glfwGetTime();
}
float TimeSinceTimeStamp(unsigned int i) {
return glfwGetTime() - timestamps[i];
}
float GetTimeStamp(unsigned int i) {
return timestamps[i];
}
float TimeSinceStart() {
return TimeSinceTimeStamp(0);
}
float TimeSinceFrame() {
return TimeSinceTimeStamp(1);
}
float NewFrame() {
float deltaTime = TimeSinceFrame();
UpdateTimeStamp(1);
return deltaTime;
}
float NewTimeStampPeriod(unsigned int i) {
float deltaTime = TimeSinceTimeStamp(i);
UpdateTimeStamp(i);
return deltaTime;
}
void EnableFPSlimit(float frameRate, float* dTime, unsigned int timer_no = 2) {
fpsLimitEnabled = true;
fpsLimit = 1.0f / frameRate;
deltaTimePointer = dTime;
fpsTimer = timer_no;
UpdateTimeStamp(fpsTimer);
}
bool IsNewFrame() {
if (fpsLimitEnabled) {
*deltaTimePointer = TimeSinceTimeStamp(fpsTimer);
UpdateTimeStamp(fpsTimer);
if (TimeSinceFrame() > fpsLimit) {
NewFrame();
return true;
}
else {
return false;
}
}
else {
return false;
}
}
Time_Manager() {
timestamps[0] = glfwGetTime();
timestamps[1] = glfwGetTime();
}
Time_Manager(float frameRate, float* dTime, unsigned int timer_no = 2) {
timestamps[0] = glfwGetTime();
timestamps[1] = glfwGetTime();
EnableFPSlimit(frameRate, dTime, timer_no);
}
};
#endif