-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.h
80 lines (73 loc) · 1.67 KB
/
timer.h
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
73
74
75
76
77
78
79
80
#ifndef TIMER_H_
#define TIMER_H_
#include <SDL.h>
//There will be one timer in each program that keeps track of
class Timer
{
public:
Timer();
inline void start()
{
started_ = true;
paused_ = false;
ticks_at_last_call_ = SDL_GetTicks();
starting_ticks_ = ticks_at_last_call_;
}
inline void stop()
{
started_ = false;
paused_ = false;
}
inline void Timer::pause()
{
if ((started_ == true) && (paused_ == false))
{
paused_ = true;
paused_ticks_ = SDL_GetTicks() - starting_ticks_;
}
}
inline void unpause()
{
if (paused_ == true)
{
paused_ = false;
ticks_at_last_call_ = SDL_GetTicks() - paused_ticks_;
paused_ticks_ = 0;
}
}
//Call this function once per main loop.
inline void update_deltaT()
{
deltaT_ = SDL_GetTicks() - ticks_at_last_call_;
//I was having collision detection problems with tiles when the program
//was running very fast
if (deltaT_ <= 2)
{
SDL_Delay(2);
deltaT_ = SDL_GetTicks() - ticks_at_last_call_;
}
ticks_at_last_call_ = SDL_GetTicks();
//The idea is if it takes longer than 1/60th of a second to complete a loop
if (deltaT_ >= 16)
{
deltaT_ = 16;
}
//deltaT will be measured in milliseconds
}
//This is the one we use for calculating speed.
inline double deltaT_in_seconds() { return static_cast<double>(deltaT_) / 1000; }
inline int deltaT() { return deltaT_; }
inline bool started() { return started_; }
inline bool paused() { return paused_; }
inline int ticks() { return ticks_at_last_call_; }
private:
bool first_call_;
int ticks_at_last_call_;
int paused_ticks_;
bool paused_;
bool started_;
//Time since last call in seconds
int deltaT_;
int starting_ticks_;
};
#endif