-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
104 lines (90 loc) · 1.67 KB
/
timer.cpp
File metadata and controls
104 lines (90 loc) · 1.67 KB
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
/*
#include <SDL.h>
#include <time.h>
#include "timer.h"
*/
void Timer::start()
{
//Start the timer
started = true;
paused = false;
start_t = SDL_GetTicks();
paused_t = 0;
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
//Clear the variables
start_t = 0;
paused_t = 0;
}
void Timer::pause()
{
//If the timer is running and is not paused
if(started && !paused)
{
paused = true;
paused_t = SDL_GetTicks();
start_t = 0;
}
}
void Timer::unpause()
{
if(started && paused)
{
paused = false;
start_t = SDL_GetTicks();
paused_t = 0;
}
}
void Timer::update_avg_fps()
{
++frame_counter;
if(frame_counter%update_threshold == 0)
{
avg_fps = avg_fps_sum / update_threshold;
frame_counter = 0;
avg_fps_sum = 0;
}
else
{
avg_fps_sum += get_fps();
}
}
double Timer::get_fps() const noexcept
{
return 1000.0/get_time();
}
double Timer::get_average_fps() const noexcept
{
return avg_fps;
}
bool Timer::is_started() const noexcept
{
return started;
}
bool Timer::is_paused() const noexcept
{
return paused;
}
Uint32 Timer::get_time() const noexcept
{
Uint32 time = 0;
if(started)
{
//Returns the time when paused
if(paused)
{
time = paused_t;
}
//Returns the time SINCE the timer started
else
{
time = SDL_GetTicks() - start_t;
}
}
return time;
}