MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 {
49  StartStamp(0),
50  CurrentTime(0),
51  InitialTime(0)
52  { }
53 
55  { }
56 
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 
73  { this->CurrentTime = static_cast<MaxInt>( Current ) * 1000; }
74 
76  { this->Update(); return static_cast<Whole>( this->CurrentTime ); }
77 
79  { return this->GetCurrentTime() * 0.001; }
80 
81  void Timer::SetInitialTime(const Whole Initial)
82  { this->InitialTime = static_cast<MaxInt>( Initial ); }
83 
85  { this->InitialTime = static_cast<MaxInt>( Initial ) * 1000; }
86 
88  { return static_cast<Whole>( this->InitialTime ); }
89 
91  { return this->GetInitialTime() * 0.001; }
92 
93  void Timer::Start()
94  {
95  if( this->IsStopped() ) {
97  }
98  }
99 
100  void Timer::Stop()
101  {
102  if( !this->IsStopped() ) {
103  this->StartStamp = 0;
104  }
105  }
106 
108  {
109  this->Update();
110  return this->StartStamp == 0;
111  }
112 
114  {
116  this->CurrentTime = this->InitialTime;
117  }
118 
120  {
121  return Timer::Normal;
122  }
123 
124  ///////////////////////////////////////////////////////////////////////////////
125  // GoalTimer Methods
126 
128  GoalTime(0),
129  ResetAtGoal(false)
130  { }
131 
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 
148  { this->GoalTime = static_cast<MaxInt>( Goal ) * 1000; }
149 
151  { return static_cast<Whole>( this->GoalTime ); }
152 
154  { return this->GetGoalTime() * 0.001; }
155 
156  ///////////////////////////////////////////////////////////////////////////////
157  // StopWatchTimer Methods
158 
160  { }
161 
163  { }
164 
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 
186  { return this->CurrentTime <= this->GoalTime; }
187 
188  ///////////////////////////////////////////////////////////////////////////////
189  // Utility
190 
192  { return Timer::StopWatch; }
193 
194  ///////////////////////////////////////////////////////////////////////////////
195  // AlarmTimer Methods
196 
198  { }
199 
201  { }
202 
204  {
205  this->Timer::Update();
206  if( this->GoalReached() ) {
207  this->StartStamp = 0;
208  if( this->ResetAtGoal ) {
209  this->Reset();
210  }
211  }
212  }
213 
215  { return this->CurrentTime >= this->GoalTime; }
216 
217  ///////////////////////////////////////////////////////////////////////////////
218  // Utility
219 
221  { return Timer::Alarm; }
222 }//Mezzanine
223 
224 #endif