MezzanineEngine
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
Mezzanine
src
timer.cpp
1
// © Copyright 2010 - 2014 BlackTopp Studios Inc.
2
/* This file is part of The Mezzanine Engine.
3
4
The Mezzanine Engine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
The Mezzanine Engine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
/* The original authors have included a copy of the license specified above in the
18
'Docs' folder. See 'gpl.txt'
19
*/
20
/* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21
Build professional software and charge for their product.
22
23
However there are some practical restrictions, so if your project involves
24
any of the following you should contact us and we will try to work something
25
out:
26
- DRM or Copy Protection of any kind(except Copyrights)
27
- Software Patents You Do Not Wish to Freely License
28
- Any Kind of Linking to Non-GPL licensed Works
29
- Are Currently In Violation of Another Copyright Holder's GPL License
30
- If You want to change our code and not add a few hundred MB of stuff to
31
your distribution
32
33
These and other limitations could cause serious legal problems if you ignore
34
them, so it is best to simply contact us or the Free Software Foundation, if
35
you have any questions.
36
37
Joseph Toppi - toppij@gmail.com
38
John Blackwood - makoenergy02@gmail.com
39
*/
40
#ifndef _timer_cpp
41
#define _timer_cpp
42
43
#include "timer.h"
44
#include "crossplatform.h"
45
46
namespace
Mezzanine
47
{
48
Timer::Timer
() :
49
StartStamp(0),
50
CurrentTime(0),
51
InitialTime(0)
52
{ }
53
54
Timer::~Timer
()
55
{ }
56
57
void
Timer::Update
()
58
{
59
if
( this->
StartStamp
!= 0 ) {
60
MaxInt
CurrentStamp =
crossplatform::GetTimeStamp
();
61
this->
CurrentTime
+= (CurrentStamp - this->
StartStamp
);
62
this->
StartStamp
= CurrentStamp;
63
}
64
}
65
66
///////////////////////////////////////////////////////////////////////////////
67
// Utility
68
69
void
Timer::SetCurrentTime
(
const
Whole
Current)
70
{ this->
CurrentTime
=
static_cast<
MaxInt
>
( Current ); }
71
72
void
Timer::SetCurrentTimeInMilliseconds
(
const
Whole
Current)
73
{ this->
CurrentTime
=
static_cast<
MaxInt
>
( Current ) * 1000; }
74
75
Whole
Timer::GetCurrentTime
()
76
{ this->
Update
();
return
static_cast<
Whole
>
( this->
CurrentTime
); }
77
78
Whole
Timer::GetCurrentTimeInMilliseconds
()
79
{
return
this->
GetCurrentTime
() * 0.001; }
80
81
void
Timer::SetInitialTime
(
const
Whole
Initial)
82
{ this->
InitialTime
=
static_cast<
MaxInt
>
( Initial ); }
83
84
void
Timer::SetInitialTimeInMilliseconds
(
const
Whole
Initial)
85
{ this->
InitialTime
=
static_cast<
MaxInt
>
( Initial ) * 1000; }
86
87
Whole
Timer::GetInitialTime
()
const
88
{
return
static_cast<
Whole
>
( this->
InitialTime
); }
89
90
Whole
Timer::GetInitialTimeInMilliseconds
()
const
91
{
return
this->
GetInitialTime
() * 0.001; }
92
93
void
Timer::Start
()
94
{
95
if
( this->
IsStopped
() ) {
96
this->
StartStamp
=
crossplatform::GetTimeStamp
();
97
}
98
}
99
100
void
Timer::Stop
()
101
{
102
if
( !this->
IsStopped
() ) {
103
this->
StartStamp
= 0;
104
}
105
}
106
107
bool
Timer::IsStopped
()
108
{
109
this->
Update
();
110
return
this->
StartStamp
== 0;
111
}
112
113
void
Timer::Reset
()
114
{
115
this->
StartStamp
=
crossplatform::GetTimeStamp
();
116
this->
CurrentTime
= this->
InitialTime
;
117
}
118
119
Timer::TimerType
Timer::GetType
()
const
120
{
121
return
Timer::Normal
;
122
}
123
124
///////////////////////////////////////////////////////////////////////////////
125
// GoalTimer Methods
126
127
GoalTimer::GoalTimer
() :
128
GoalTime(0),
129
ResetAtGoal(false)
130
{ }
131
132
GoalTimer::~GoalTimer
()
133
{ }
134
135
///////////////////////////////////////////////////////////////////////////////
136
// Utility
137
138
void
GoalTimer::SetAutoReset
(
const
bool
AutoReset)
139
{ this->
ResetAtGoal
= AutoReset; }
140
141
Boolean
GoalTimer::GetAutoReset
()
const
142
{
return
this->
ResetAtGoal
; }
143
144
void
GoalTimer::SetGoalTime
(
const
Whole
Goal)
145
{ this->
GoalTime
=
static_cast<
MaxInt
>
( Goal ); }
146
147
void
GoalTimer::SetGoalTimeInMilliseconds
(
const
Whole
Goal)
148
{ this->
GoalTime
=
static_cast<
MaxInt
>
( Goal ) * 1000; }
149
150
Whole
GoalTimer::GetGoalTime
()
const
151
{
return
static_cast<
Whole
>
( this->
GoalTime
); }
152
153
Whole
GoalTimer::GetGoalTimeInMilliseconds
()
const
154
{
return
this->
GetGoalTime
() * 0.001; }
155
156
///////////////////////////////////////////////////////////////////////////////
157
// StopWatchTimer Methods
158
159
StopWatchTimer::StopWatchTimer
()
160
{ }
161
162
StopWatchTimer::~StopWatchTimer
()
163
{ }
164
165
void
StopWatchTimer::Update
()
166
{
167
if
( this->
StartStamp
!= 0 ) {
168
MaxInt
CurrentStamp =
crossplatform::GetTimeStamp
();
169
if
( CurrentStamp - this->
StartStamp
> this->
CurrentTime
) {
170
this->
CurrentTime
= 0;
171
}
else
{
172
this->
CurrentTime
-= ( CurrentStamp - this->
StartStamp
);
173
}
174
this->
StartStamp
= CurrentStamp;
175
}
176
177
if
( this->
GoalReached
() ) {
178
this->
StartStamp
= 0;
179
if
( this->
ResetAtGoal
) {
180
this->
Reset
();
181
}
182
}
183
}
184
185
Boolean
StopWatchTimer::GoalReached
()
186
{
return
this->
CurrentTime
<= this->
GoalTime
; }
187
188
///////////////////////////////////////////////////////////////////////////////
189
// Utility
190
191
Timer::TimerType
StopWatchTimer::GetType
()
const
192
{
return
Timer::StopWatch
; }
193
194
///////////////////////////////////////////////////////////////////////////////
195
// AlarmTimer Methods
196
197
AlarmTimer::AlarmTimer
()
198
{ }
199
200
AlarmTimer::~AlarmTimer
()
201
{ }
202
203
void
AlarmTimer::Update
()
204
{
205
this->
Timer::Update
();
206
if
( this->
GoalReached
() ) {
207
this->
StartStamp
= 0;
208
if
( this->
ResetAtGoal
) {
209
this->
Reset
();
210
}
211
}
212
}
213
214
Boolean
AlarmTimer::GoalReached
()
215
{
return
this->
CurrentTime
>= this->
GoalTime
; }
216
217
///////////////////////////////////////////////////////////////////////////////
218
// Utility
219
220
Timer::TimerType
AlarmTimer::GetType
()
const
221
{
return
Timer::Alarm
; }
222
}
//Mezzanine
223
224
#endif
Generated on Mon Jan 6 2014 20:58:06 for MezzanineEngine by
1.8.4