-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timer.cpp
65 lines (58 loc) · 1.63 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
62
63
64
65
#include "Timer.h"
#include <utility>
namespace
{
Timer gTimer;
}
Timer& getTimer()
{
return gTimer;
}
void Timer::scheduleSeconds(double numSeconds,
const std::function<void()>& callback)
{
scheduledTimes.emplace_back(numFrames + numSeconds * FRAMES_PER_SECOND,
callback);
}
void Timer::scheduleEveryNSeconds(double numSeconds,
const std::function<void()>& callback)
{
repeatedTimes.emplace_back(numSeconds * FRAMES_PER_SECOND,
numFrames,
callback);
}
void Timer::incrementNumFrames()
{
for (int i = static_cast<int>(scheduledTimes.size() - 1); i >= 0; i--)
{
ScheduledEvent event = scheduledTimes[i];
if (numFrames == event.mTime)
{
event.mCallback();
scheduledTimes.erase(scheduledTimes.begin() + i);
}
}
for (int i = static_cast<int>(repeatedTimes.size() - 1); i >= 0; i--)
{
auto event = repeatedTimes[i];
if (numFrames == event.mTime + event.mStartingNumFrames)
{
event.mCallback();
repeatedTimes[i].mStartingNumFrames = numFrames;
}
}
numFrames += 1;
}
ScheduledEvent::ScheduledEvent(size_t time, std::function<void()> callback) :
mTime(time),
mCallback(std::move(callback))
{
}
RecurringEvent::RecurringEvent(size_t time,
size_t startingNumFrames,
std::function<void()> callback) :
mTime(time),
mStartingNumFrames(startingNumFrames),
mCallback(std::move(callback))
{
}