forked from GuillemFP/DoubleDragon3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.h
111 lines (89 loc) · 1.71 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef TIMER_H
#define TIMER_H
#include "SDL/include/SDL_timer.h"
enum TimerState
{
OFF,
RUNNING,
PAUSED
};
class Timer
{
public:
Timer(Uint32 max_time = 0) : max_time(max_time) {};
~Timer() {};
void Start() { Start(max_time); }
void Start(float max_time) { Start((Uint32)max_time * 1000); }
void Start(Uint32 max_time)
{
if (state == OFF)
{
start_time = SDL_GetTicks();
this->max_time = max_time;
state = RUNNING;
}
}
void Stop()
{
start_time = 0;
accumulated_time = 0;
state = OFF;
}
void Reset()
{
Stop();
Start(max_time);
}
void Pause()
{
if (state == RUNNING)
{
accumulated_time += SDL_GetTicks() - start_time;
start_time = 0;
state = PAUSED;
}
}
void Resume()
{
if (state == PAUSED)
{
start_time = SDL_GetTicks();
state = RUNNING;
}
}
Uint32 GetTimeInMs() const { return accumulated_time + CurrentTime(); }
float GetTimeInSeconds() const { return ((float) GetTimeInMs()/1000.0f); }
Uint32 GetCounterInMs() const { return max_time - GetTimeInMs(); }
int GetCounterInS() const { return (GetCounterInMs() / 1000); }
TimerState GetState() const { return state; }
Uint32 GetMaxTimeInMs() const { return max_time; }
void DoubleMaxTime() { max_time *= 2; }
bool MaxTimeReached() const
{
if (max_time > 0)
{
return (accumulated_time + CurrentTime() >= max_time);
}
else
return false;
}
void SetMaxTime(Uint32 max_time)
{
this->max_time = max_time;
Stop();
}
private:
Uint32 CurrentTime() const
{
if (state == PAUSED || state == OFF)
return 0;
else
return SDL_GetTicks() - start_time;
}
private:
Uint32 start_time = 0;
Uint32 accumulated_time = 0;
Uint32 max_time = 0;
TimerState state = OFF;
};
#endif // !TIMER_H