-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.cpp
More file actions
97 lines (84 loc) · 1.87 KB
/
clock.cpp
File metadata and controls
97 lines (84 loc) · 1.87 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
#include <cmath>
#include <iostream>
#include <string>
#include <sstream>
#include "clock.h"
#include "gameData.h"
#include "ioMod.h"
Clock& Clock::getInstance() {
static Clock clock;
return clock;
}
Clock::Clock() :
started(false),
paused(false),
FRAME_CAP_ON(Gamedata::getInstance().getXmlBool("frameCapOn")),
PERIOD(Gamedata::getInstance().getXmlInt("period")),
frames(0),
timeAtStart(0),
timeAtPause(0),
currTicks(0), prevTicks(0), ticks(0)
{
startClock();
}
Clock::Clock(const Clock& c) :
started(c.started),
paused(c.paused),
FRAME_CAP_ON(c.FRAME_CAP_ON),
PERIOD(c.PERIOD),
frames(c.frames),
timeAtStart(c.timeAtStart), timeAtPause(c.timeAtPause),
currTicks(c.currTicks), prevTicks(c.prevTicks), ticks(c.ticks)
{
startClock();
}
void Clock::toggleSloMo() {
throw( std::string("Slow motion is not implemented yet") );
}
unsigned int Clock::getTicks() const {
if (paused) return timeAtPause;
else return SDL_GetTicks() - timeAtStart;
}
unsigned int Clock::getElapsedTicks() {
if (paused) return 0;
currTicks = getTicks();
ticks = currTicks-prevTicks;
if ( FRAME_CAP_ON ) {
// DO DELAY SO NOT TO OCCUPY CPU
if ( ticks < PERIOD ) return 0;
prevTicks = currTicks;
return ticks;
}
else {
prevTicks = currTicks;
return ticks;
}
}
int Clock::getFps() const {
if ( getSeconds() > 0 ) return frames/getSeconds();
else return 0;
}
void Clock::incrFrame() {
if ( !paused ) {
++frames;
}
}
void Clock::startClock() {
started = true;
paused = false;
frames = 0;
timeAtPause = timeAtStart = SDL_GetTicks();
prevTicks = 0;
}
void Clock::pause() {
if( started && !paused ) {
timeAtPause = SDL_GetTicks() - timeAtStart;
paused = true;
}
}
void Clock::unpause() {
if( started && paused ) {
timeAtStart = SDL_GetTicks() - timeAtPause;
paused = false;
}
}