-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUITimer.cpp
63 lines (47 loc) · 920 Bytes
/
GUITimer.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
#include "GUITimer.h"
namespace GUI {
void GUITimer::start() {
if(!running){
running = true;
ticks_at_start = SDL_GetTicks();
}
}
void GUITimer::stop() {
running = false;
}
void GUITimer::pause() {
paused = true;
ticks_at_pause = SDL_GetTicks() - ticks_at_start;
}
void GUITimer::unpause() {
paused = false;
}
void GUITimer::reset(){
stop();
start();
}
Uint32 GUITimer::get_time(){
if (!running) {
return 0;
}
if (paused) {
return ticks_at_pause;
}
else {
return SDL_GetTicks() - ticks_at_start;
}
}
FrameRateCapper::FrameRateCapper(int frames_per_second_cap)
:fps_cap (frames_per_second_cap), capping(false)
{ }
void FrameRateCapper::cap_frame_rate(){
capping = true;
fps.start();
}
FrameRateCapper::~FrameRateCapper(){
if (!capping) return;
if (fps.get_time() < (Uint32) (1000 / fps_cap)) {
SDL_Delay((1000 / fps_cap) - fps.get_time());
}
}
} // namespace GUI