-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTimer.cpp
60 lines (54 loc) · 1.4 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
// Onut
#include <onut/Timing.h>
#include <onut/Timer.h>
namespace onut
{
void Timer::start(float duration, const std::function<void()>& callback)
{
m_progress = 0.f;
m_duration = duration;
m_callback = callback;
oUpdater->registerTarget(this);
}
void Timer::start(std::chrono::steady_clock::duration duration, const std::function<void()>& callback)
{
auto durationUs = std::chrono::duration_cast<std::chrono::microseconds>(duration);
auto durationS = static_cast<float>(static_cast<double>(durationUs.count()) / 1000000.0);
start(durationS, callback);
}
/**
Stop. Value will stay where it is. So you can get the time it was stopped by calling: getProgress
*/
void Timer::stop(bool callCallback)
{
oUpdater->unregisterTarget(this);
if (callCallback)
{
auto callback = m_callback;
m_callback = nullptr;
callback();
}
}
/**
Get the current time in the timer's progress. In seconds.
*/
float Timer::getProgress() const
{
return m_progress;
}
/**
Check if the timer is running
*/
bool Timer::isRunning() const
{
return isUpdateTargetRegistered();
}
void Timer::update()
{
m_progress += ODT;
if (m_progress >= m_duration)
{
stop(true);
}
}
}