Spinning Topp Logo BlackTopp Studios
inc
timer.cpp
1 // © Copyright 2010 - 2016 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
47 {
48  Mezzanine::MaxInt CountUpFunction(const Mezzanine::MaxInt Current, const Mezzanine::MaxInt Delta)
49  {
50  return Current + Delta;
51  }
52 
53  Mezzanine::MaxInt CountDownFunction(const Mezzanine::MaxInt Current, const Mezzanine::MaxInt Delta)
54  {
55  Mezzanine::MaxInt Result = Current - Delta;
56  if( Result < 0 )
57  Result = 0;
58  return Result;
59  }
60 }
61 
62 namespace Mezzanine
63 {
65  LastStamp(0),
66  CurrentTime(0)
67  { this->TimerCounter = &CountUpFunction; }
68 
70  { }
71 
73  {
74  if( this->LastStamp != 0 ) {
75  MaxInt CurrentStamp = crossplatform::GetTimeStamp();
76  this->CurrentTime = this->TimerCounter(this->CurrentTime,CurrentStamp - this->LastStamp);
77  this->LastStamp = CurrentStamp;
78 
79  // If we've reached the end of a countdown then stop the timer. But don't actually call Stop(), as that will cause an infinite loop.
80  if( this->CurrentTime == 0 && this->GetCountMode() == Mezzanine::CM_CountDown )
81  this->LastStamp = 0;
82  }
83  return this->CurrentTime;
84  }
85 
86  ///////////////////////////////////////////////////////////////////////////////
87  // Utility
88 
89  void Timer::SetCurrentTime(const Whole Current)
90  { this->CurrentTime = static_cast<MaxInt>( Current ); }
91 
93  { this->CurrentTime = static_cast<MaxInt>( Current ) * 1000; }
94 
96  { return static_cast<Whole>( this->UpdateTime() ); }
97 
99  { return this->GetCurrentTime() * 0.001; }
100 
102  {
103  if( Mode == Mezzanine::CM_CountDown ) this->TimerCounter = &CountDownFunction;
104  else this->TimerCounter = &CountUpFunction;
105  }
106 
108  {
109  if( this->TimerCounter == &CountDownFunction ) return Mezzanine::CM_CountDown;
110  else return Mezzanine::CM_CountUp;
111  }
112 
114  {
115  if( this->LastStamp == 0 )
117  }
118 
119  void Timer::Stop()
120  {
121  if( this->LastStamp != 0 ) {
122  this->UpdateTime();
123  this->LastStamp = 0;
124  }
125  }
126 
127  void Timer::Reset(const Whole StartTime)
128  {
130  this->SetCurrentTime(StartTime);
131  }
132 
134  {
135  this->UpdateTime();
136  return ( this->LastStamp != 0 );
137  }
138 
140  {
141  this->UpdateTime();
142  return ( this->LastStamp == 0 );
143  }
144 
146  {
147  StringStream TimeStream;
148  switch( Format )
149  {
151  {
152  TimeStream << this->GetCurrentTime();
153  break;
154  }
156  {
157  TimeStream << this->GetCurrentTimeInMilliseconds();
158  break;
159  }
161  {
162  TimeStream << ( this->GetCurrentTimeInMilliseconds() / 1000 );
163  break;
164  }
166  {
167  Whole Milli = this->GetCurrentTimeInMilliseconds();
168  Whole Seconds = ( Milli / 1000 );
169  Whole Remainder = ( Milli % 1000 );
170  if( Remainder < 10 ) {
171  TimeStream << Seconds << ".00" << Remainder;
172  }else if( Remainder < 100 ) {
173  TimeStream << Seconds << ".0" << Remainder;
174  }else{
175  TimeStream << Seconds << "." << Remainder;
176  }
177  break;
178  }
180  {
181  Whole Seconds = ( this->GetCurrentTimeInMilliseconds() / 1000 );
182  Whole Minutes = Seconds / 60;
183  Whole Remainder = Seconds % 60;
184  if( Remainder < 10 ) {
185  TimeStream << Minutes << ":0" << Remainder;
186  }else{
187  TimeStream << Minutes << ":" << Remainder;
188  }
189  break;
190  }
191  default:
192  {
193  break;
194  }
195  }
196  return TimeStream.str();
197  }
198 }//Mezzanine
199 
200 #endif
Mezzanine::CountMode GetCountMode() const
Gets the mode the timer is using to increment time.
Definition: timer.cpp:107
~Timer()
Class Destructor.
Definition: timer.cpp:69
Whole GetCurrentTime()
Gets the Current time in Microseconds.
Definition: timer.cpp:95
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
MaxInt CurrentTime
The current amount of microseconds that has elapsed since starting to track time. ...
Definition: timer.h:79
MaxInt UpdateTime()
Updates the current time being tracked by this timer.
Definition: timer.cpp:72
Timer()
Class Constructor.
Definition: timer.cpp:64
Boole IsStopped()
Gets whether or not this Timer is currently running.
Definition: timer.cpp:139
void SetCurrentTime(const Whole Current)
Sets the current time in Microseconds.
Definition: timer.cpp:89
CountMode
A simple enum describing how the timer should increment time.
Definition: timer.h:58
Outputs the current time in "minutes:seconds".
Definition: timer.h:54
The timer counts up, meaning the Timer current time is showing time elapsed.
Definition: timer.h:60
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Definition: datatypes.h:176
Boole IsTicking()
Gets whether or not this Timer is currently running.
Definition: timer.cpp:133
CountFunct TimerCounter
A pointer to the function currently doing the counting for this Timer.
Definition: timer.h:82
Whole GetCurrentTimeInMilliseconds()
Gets the Current time in Milliseconds.
Definition: timer.cpp:98
Outputs the current time in "seconds.milliseconds". Milliseconds are out to 3 digits.
Definition: timer.h:53
Outputs the current time in seconds.
Definition: timer.h:52
void Start()
Activates the Timer.
Definition: timer.cpp:113
void SetCountMode(const Mezzanine::CountMode Mode)
Sets the mode the timer should use to increment time.
Definition: timer.cpp:101
void SetCurrentTimeInMilliseconds(const Whole Current)
Sets the current time in Milliseconds. The time that resetting sets the Timer to. ...
Definition: timer.cpp:92
MaxInt LastStamp
The time stamp from when the last time the Timer was updated.
Definition: timer.h:76
String GetTimeAsText(const Mezzanine::TimeFormat Format)
Gets the current time of this Timer as a string.
Definition: timer.cpp:145
Outputs the current time in microseconds.
Definition: timer.h:50
void Reset(const Whole StartTime=0)
Sets the current time to an initial value and stops the Timer if it is running.
Definition: timer.cpp:127
The timer counts down, meaning the Timer current time is showing remaining time.
Definition: timer.h:61
long long MaxInt
A large integer type suitable for compile time math and long term microsecond time keeping...
Definition: datatypes.h:190
void Stop()
Deactivates the Timer.
Definition: timer.cpp:119
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
Outputs the current time in milliseconds.
Definition: timer.h:51
MaxInt GetTimeStamp()
Get a timestamp.
TimeFormat
An enum describing how the text output of a timer should be formatted.
Definition: timer.h:48
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159