-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimer.cpp
74 lines (63 loc) · 1.37 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
66
67
68
69
70
71
72
// ----------------------------------------------------
// Timer.cpp
// Body for CPU Tick Timer class
// ----------------------------------------------------
#include "Timer.h"
double Timer::frequency = 0;
// ---------------------------------------------
Timer::Timer()
{
if (frequency == 0)
frequency = SDL_GetPerformanceFrequency();
Start();
}
// ---------------------------------------------
void Timer::Start()
{
running = true;
started_at = SDL_GetTicks();
}
// ---------------------------------------------
void Timer::Stop()
{
running = false;
stopped_at = SDL_GetTicks();
}
// ---------------------------------------------
void Timer::Resume()
{
running = true;
started_at = SDL_GetTicks() - (stopped_at - started_at);
}
//-----------------------------------------------
void Timer::Reset()
{
running = false;
stopped_at = SDL_GetTicks();
resume_at = SDL_GetTicks();
}
// ---------------------------------------------
Uint32 Timer::Read()
{
if(running == true)
{
return SDL_GetTicks() - started_at;
}
else
{
return stopped_at - started_at;
}
}
double Timer::ReadMs() const
{
return 1000.0 * (double(SDL_GetTicks() - started_at) / double(frequency));
}
// ---------------------------------------------
double Timer::ReadTicks() const
{
return SDL_GetTicks() - started_at;
}
float Timer::ReadSec() const
{
return float(SDL_GetTicks() - started_at) / 1000.0f;
}