-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTimer.cpp
61 lines (56 loc) · 1.33 KB
/
Timer.cpp
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
#include "Timer.h"
Timer::Timer(CKDWORD delay, std::function<void()> callback, CKDWORD tick, float time) {
m_callbacktype = ONCE;
m_callback.once = callback;
m_delaytype = TICK;
m_delay.tick = delay;
m_start.tick = tick;
}
Timer::Timer(CKDWORD delay, std::function<bool()> callback, CKDWORD tick, float time) {
m_callbacktype = LOOP;
m_callback.loop = callback;
m_delaytype = TICK;
m_delay.tick = delay;
m_start.tick = tick;
}
Timer::Timer(float delay, std::function<void()> callback, CKDWORD tick, float time) {
m_callbacktype = ONCE;
m_callback.once = callback;
m_delaytype = TIME;
m_delay.time = delay;
m_start.time = time;
}
Timer::Timer(float delay, std::function<bool()> callback, CKDWORD tick, float time) {
m_callbacktype = LOOP;
m_callback.loop = callback;
m_delaytype = TIME;
m_delay.time = delay;
m_start.time = time;
}
bool Timer::Process(CKDWORD tick, float time) {
if (m_delaytype == TICK) {
if (m_start.tick + m_delay.tick <= tick) {
if (m_callbacktype == ONCE) {
m_callback.once();
return false;
}
else {
m_start.tick = tick;
return m_callback.loop();
}
}
}
else {
if (m_start.time + m_delay.time <= time) {
if (m_callbacktype == ONCE) {
m_callback.once();
return false;
}
else {
m_start.time = time;
return m_callback.loop();
}
}
}
return true;
}