-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.h
161 lines (137 loc) · 4.34 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#if !defined(__TIMER_H__)
#define __STIMER_H__
#include <list>
class TimerManager;
/**
* @class Timer
*
* @brief A Timer.
*
* Timers are objects that are used to sleep for a specified period of time then awake and notify the system that they have timed out.
* Timers can be one shot (by default) or made to repeat any number of times, including an infinite number of times.
*
* Example usage:
*
* void TimerTimedOut(Timer* timer, void* userData)
* {
* printf("Timer timed out\n");
* }
* MyTimerManager.Add(new Timer(2.0f, 5, &TimerTimedOut));
*
* The above timer will print "Timer timedout" every two seconds for 10 seconds.
*
*/
class Timer
{
public:
typedef void (*Callback)(Timer* pTimer, void* userData);
protected:
unsigned int m_NameHash; // Timer name
TimerManager* m_Manager; // Timer manager
float m_Length; // Timer length
float m_TimeLeft; // Current time left on timer
Callback m_OnTimeout; // Timer timeout callback
void* m_UserData; // Timer timeout callback user data
int m_RepeatCount; // Number of times to repeat timer
bool m_Paused; // Paused state
public:
void setManager(TimerManager* manager) { m_Manager = manager; }
unsigned int getNameHash() const { return m_NameHash; }
void setLength(float length) { m_Length = length; m_TimeLeft = length; }
void setOnTimeout(Callback callback) { m_OnTimeout = callback; }
float getLength() const { return m_Length; }
float getTimeLeft() const { return m_TimeLeft; }
void setRepeatCount(int count) { m_RepeatCount = count; }
bool isPaused() const { return m_Paused; }
public:
Timer() : m_Manager(0), m_Length(0), m_TimeLeft(0), m_OnTimeout(0), m_UserData(0), m_RepeatCount(1), m_Paused(false) {}
Timer(float length, int repeat_count = 1, Callback on_timeout = 0, void* user_data = 0);
/**
* @fn bool Timer::Update(float deltaTime = 0.0f);
*
* @brief Update the timer.
*
* @param deltaTime (optional) amount of time that has passed since Update() was last called.
*
* @return true if it succeeds, false if it fails.
*/
bool Update(float deltaTime = 0.0f);
/**
* @fn void Timer::Pause();
*
* @brief Pauses this timer.
*/
void Pause();
/**
* @fn void Timer::Resume();
*
* @brief Resumes this timer.
*/
void Resume();
};
/**
* @class TimerManager
*
* @brief Manages a collection of timers.
*/
class TimerManager
{
protected:
std::list<Timer*> m_Timers; // Timers list
public:
TimerManager();
~TimerManager();
/**
* @fn void TimerManager::Update(float deltaTime = 0.0f);
*
* @brief Updates all timers that are managed by this this timer manager.
*
* @param deltaTime (optional) time of the delta.
*/
void Update(float deltaTime = 0.0f);
/**
* @fn void TimerManager::Add(Timer* timer);
*
* @brief Adds a timer to this manager.
*
* @param timer If non-null, the timer to add.
*/
void Add(Timer* timer);
/**
* @fn void TimerManager::Cancel(Timer* timer);
*
* @brief Cancels the supplied timer and destroys it.
*
* @param timer If non-null, the timer.
*/
void Cancel(Timer* timer);
/**
* @fn void TimerManager::Clear();
*
* @brief Cancels and destroys all timers managed by this manager.
*/
void Clear();
/**
* @fn Timer* TimerManager::Find(const char* name);
*
* @brief Finds the named timer.
*
* @param name The name of the timer.
*
* @return null if it fails, else the timer.
*/
Timer* Find(const char* name);
/**
* @fn void TimerManager::Pause();
*
* @brief Pauses all timers managed by this timer manager.
*/
void Pause();
/**
* @fn void TimerManager::Resume();
*
* @brief Resumes all timers managed by this timer manager.
*/
void Resume();
};
#endif // __SCENE_H__