-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cpp
136 lines (123 loc) · 3.28 KB
/
clock.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
#include "clock.h"
#include "gamedata.h"
#include "ioManager.h"
Clock& Clock::getInstance() {
if ( SDL_WasInit(SDL_INIT_VIDEO) == 0) {
throw std::string("Must init SDL before Clock");
}
static Clock instance;
return instance;
}
Clock::Clock() :
started(false),
paused(false),
sloMo(false),
framesAreCapped(Gamedata::getInstance().getXmlBool("framesAreCapped")),
frameCap(Gamedata::getInstance().getXmlInt("frameCap")),
frames(0),
tickSum(0),
sumOfAllTicks(0),
timeAtStart(0), timeAtPause(0),
currTicks(0), prevTicks(0), ticks(0), timeEachFrame(), frameRate(0), fpsAveragedOver(Gamedata::getInstance().getXmlInt("frameMax"))
{
start();
}
Clock::Clock(const Clock& c) :
started(c.started),
paused(c.paused),
sloMo(c.sloMo),
framesAreCapped(c.framesAreCapped),
frameCap(c.frameCap),
frames(c.frames),
tickSum(c.tickSum),
sumOfAllTicks(c.sumOfAllTicks),
timeAtStart(c.timeAtStart), timeAtPause(c.timeAtPause),
currTicks(c.currTicks), prevTicks(c.prevTicks), ticks(c.ticks) , timeEachFrame(c.timeEachFrame), frameRate(c.frameRate), fpsAveragedOver(c.fpsAveragedOver)
{
start();
}
void Clock::display() const {
static unsigned int lastFrames = 0;
static unsigned int oldFrames = 0;
static unsigned int seconds = getSeconds();
if ( getSeconds() > seconds ) {
seconds = getSeconds();
lastFrames = frames - oldFrames;
oldFrames = frames;
}
IOManager::getInstance()
.printMessageValueAt("seconds: ", seconds, 10, 30);
IOManager::getInstance()
.printMessageValueAt("frames in sec: ", lastFrames, 10, 50);
}
void Clock::toggleSloMo() {
if( started && !sloMo ) {
timeAtPause = SDL_GetTicks() - timeAtStart;
sloMo = true;
}
else if ( started && sloMo ) {
timeAtStart = SDL_GetTicks() - timeAtPause;
sloMo = false;
}
}
unsigned int Clock::getTicks() const {
if (paused) return timeAtPause;
else if ( sloMo ) return 1;
else return SDL_GetTicks() - timeAtStart;
}
unsigned int Clock::getElapsedTicks() {
if (paused) return 0;
else if ( sloMo ) return 1;
currTicks = getTicks();
ticks = currTicks-prevTicks;
unsigned int delay = capFrameRate();
prevTicks = currTicks + delay;
ticks += delay;
sumOfAllTicks += ticks;
return ticks;
}
unsigned int Clock::capFrameRate() const {
if ( !framesAreCapped ) return 0u;
unsigned int delay = std::max(0.0,1000.0/frameCap+0.5 - ticks);
SDL_Delay( delay );
return delay;
}
Clock& Clock::operator++() {
if ( !paused ) {
++frames;
}
return *this;
}
void Clock::start() {
started = true;
paused = false;
frames = 0;
timeAtPause = timeAtStart = SDL_GetTicks();
}
void Clock::pause() {
if( started && !paused ) {
timeAtPause = SDL_GetTicks() - timeAtStart;
paused = true;
}
}
void Clock::unpause() {
if( started && paused ) {
timeAtStart = SDL_GetTicks() - timeAtPause;
paused = false;
}
}
void Clock::computeFrameRate() {
timeEachFrame.push_back( (sumOfAllTicks) );
if (timeEachFrame.size() > fpsAveragedOver) {
timeEachFrame.pop_front();
}
if (timeEachFrame.size() > 1){
if(timeEachFrame.back() != timeEachFrame.front()){
frameRate = timeEachFrame.size() * 1000 / (timeEachFrame.back() - timeEachFrame.front());
}
}
}