-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cc
More file actions
48 lines (37 loc) · 864 Bytes
/
Copy pathtimer.cc
File metadata and controls
48 lines (37 loc) · 864 Bytes
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
#include "timer.h"
#include <thread>
#include <iostream>
Counter::Counter(float time) : time(time) { }
float Counter::get_time() const {
return time;
}
void Counter::set_time(float time) {
this->time = time;
}
void Counter::tick(float seconds) {
if (time > 0.0) {
time -= seconds;
}
}
ActionCounter::ActionCounter(float time, std::function<void(void)> action)
: Counter(time), action(action) { }
void ActionCounter::tick(float seconds) {
if ( get_time() > 0.0f) {
Counter::tick(seconds);
if ( get_time() <= 0.0f) {
action();
}
}
}
void Timer::reset() {
start = SDL_GetTicks64();
}
void Timer::tick_and_delay(float tick_time) {
end = SDL_GetTicks64();
Uint64 elapse = end - start;
SDL_Delay(1000.0f * tick_time - elapse);
tick(tick_time);
}
void Timer::tick(float tick_time) {
time += tick_time;
}