-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimer.cpp
57 lines (45 loc) · 863 Bytes
/
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
#include "timer.h"
Timer::Timer(long int updateInterval) :
_time(),
_lastTime(0),
_elapsed(0),
_stopped(true),
_updateInterval(updateInterval),
_lastUpdate(0),
_frames(0),
_fps(0)
{
_time.start();
}
void Timer::start() {
if(!_stopped) {
return;
}
_time.restart();
_lastTime = 0;
_elapsed = 0;
_lastUpdate = 0;
_stopped = false;
}
void Timer::stop() {
if(_stopped) {
return;
}
_time.restart();
_stopped = true;
}
void Timer::update() {
if(_stopped) {
return;
}
long int current = _time.elapsed();
_frames += 1;
if(current - _lastUpdate >= _updateInterval) {
//@TODO: find out a better way to calculate frame rate
_fps = _frames / ((float)(current - _lastUpdate) / 1000.0);
_lastUpdate = current;
_frames = 0;
}
_elapsed = current - _lastTime;
_lastTime = current;
}