-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathTimer.cpp
More file actions
114 lines (93 loc) · 1.96 KB
/
Timer.cpp
File metadata and controls
114 lines (93 loc) · 1.96 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
#include <math.h>
#include <glm/glm.hpp>
#include <GL/freeglut.h>
#include "framework.h"
#include "Timer.h"
namespace Framework
{
Timer::Timer( Type eType, float fDuration )
: m_eType(eType)
, m_secDuration(fDuration)
, m_hasUpdated(false)
, m_isPaused(false)
, m_absPrevTime(0.0f)
, m_secAccumTime(0.0f)
{
if(m_eType != TT_INFINITE)
assert(m_secDuration > 0.0f);
}
void Timer::Reset()
{
m_hasUpdated = false;
m_secAccumTime = 0.0f;
}
bool Timer::TogglePause()
{
m_isPaused = !m_isPaused;
return m_isPaused;
}
bool Timer::IsPaused() const
{
return m_isPaused;
}
void Timer::SetPause( bool pause )
{
m_isPaused = pause;
}
bool Timer::Update()
{
float absCurrTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
if(!m_hasUpdated)
{
m_absPrevTime = absCurrTime;
m_hasUpdated = true;
}
if(m_isPaused)
{
m_absPrevTime = absCurrTime;
return false;
}
float fDeltaTime = absCurrTime - m_absPrevTime;
m_secAccumTime += fDeltaTime;
m_absPrevTime = absCurrTime;
if(m_eType == TT_SINGLE)
return m_secAccumTime > m_secDuration;
return false;
}
void Timer::Rewind( float secRewind )
{
m_secAccumTime -= secRewind;
if(m_secAccumTime < 0.0f)
m_secAccumTime = 0.0f;
}
void Timer::Fastforward( float secFF )
{
m_secAccumTime += secFF;
}
float Timer::GetAlpha() const
{
switch(m_eType)
{
case TT_LOOP:
return fmodf(m_secAccumTime, m_secDuration) / m_secDuration;
case TT_SINGLE:
return glm::clamp(m_secAccumTime / m_secDuration, 0.0f, 1.0f);
}
return -1.0f; //Garbage.
}
float Timer::GetProgression() const
{
switch(m_eType)
{
case TT_LOOP:
return fmodf(m_secAccumTime, m_secDuration);
case TT_SINGLE:
return glm::clamp(m_secAccumTime, 0.0f, m_secDuration);
}
return -1.0f; //Garbage.
}
float Timer::GetTimeSinceStart() const
{
return m_secAccumTime;
}
}