Spinning Topp Logo BlackTopp Studios
inc
extendedtimer.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 _extendedtimer_cpp
41 #define _extendedtimer_cpp
42 
43 #include "extendedtimer.h"
44 #include "eventmanager.h"
45 
46 namespace Mezzanine
47 {
48  ExtendedTimer::ExtendedTimer(const Timer::TimerStyle style)
49  : Timer(style)
50  {
51  Type = Timer::Extended;
52  }
53 
55  {
56  }
57 
58  Time& ExtendedTimer::GetStruct(const ExtendedTimer::TimeStruct Struct)
59  {
60  switch(Struct)
61  {
63  return CurrentTime;
64  break;
66  return GoalTime;
67  break;
69  return InitialTime;
70  break;
71  default:
72  MEZZ_EXCEPTION(Exception::INVALID_STATE_EXCEPTION,"Invalid Timer type acquisition");
73  }
74  }
75 
76  void ExtendedTimer::Update(const Whole MicroSecondsElapsed)
77  {
78  if(Active)
79  {
80  switch(Style)
81  {
82  case Timer::Normal:
83  UpdateAsNormal(MicroSecondsElapsed);
84  break;
85  case Timer::StopWatch:
86  {
87  UpdateAsStopWatch(MicroSecondsElapsed);
88  if(GoalReached())
89  {
90  if(Callback)
91  Callback->DoCallbackItems();
92  if(ResetAtGoal)
93  Reset();
94  Stop();
95  }
96  break;
97  }
98  case Timer::Alarm:
99  {
100  UpdateAsNormal(MicroSecondsElapsed);
101  if(GoalReached())
102  {
103  if(Callback)
104  Callback->DoCallbackItems();
105  if(ResetAtGoal)
106  Reset();
107  Stop();
108  }
109  break;
110  }
111  }
112  }
113  }
114 
115  void ExtendedTimer::UpdateAsNormal(const Whole MicroSecondsElapsed)
116  {
117  CurrentTime.Microseconds+=(Integer)MicroSecondsElapsed;
118  if(!CheckMicroSeconds(ExtendedTimer::Current)) return;
119  if(!CheckMilliSeconds(ExtendedTimer::Current)) return;
120  if(!CheckSeconds(ExtendedTimer::Current)) return;
121  if(!CheckMinutes(ExtendedTimer::Current)) return;
122  if(!CheckHours(ExtendedTimer::Current)) return;
123  }
124 
125  void ExtendedTimer::UpdateAsStopWatch(const Whole MicroSecondsElapsed)
126  {
127  CurrentTime.Microseconds-=MicroSecondsElapsed;
128  if(CurrentTime.Microseconds < 0)
129  {
130  Whole GoesInto = CurrentTime.Microseconds * 0.001;
131  Whole Remainder = CurrentTime.Microseconds - GoesInto * 1000;
132  CurrentTime.Microseconds = Remainder;
133  CurrentTime.Milliseconds+=GoesInto;
134  }else{ return; /*Entresol::GetSingletonPtr()->Log("Holy **** that was a fast frame!");*/ }
135  if(CurrentTime.Milliseconds < 0)
136  {
137  Integer GoesInto = CurrentTime.Milliseconds * 0.001;
138  Integer Remainder = CurrentTime.Milliseconds - GoesInto * 1000;
139  CurrentTime.Milliseconds = Remainder;
140  CurrentTime.Seconds+=GoesInto;
141  }else{ return; }
142  if(CurrentTime.Seconds < 0)
143  {
144  Integer GoesInto = CurrentTime.Seconds / 60;
145  Integer Remainder = CurrentTime.Seconds - GoesInto * 60;
146  CurrentTime.Seconds = Remainder;
147  CurrentTime.Minutes+=GoesInto;
148  }else{ return; }
149  if(CurrentTime.Minutes < 0)
150  {
151  Integer GoesInto = CurrentTime.Minutes / 60;
152  Integer Remainder = CurrentTime.Minutes - GoesInto * 60;
153  CurrentTime.Minutes = Remainder;
154  CurrentTime.Hours+=GoesInto;
155  }else{ return; }
156  if(CurrentTime.Hours < 0)
157  {
158  Integer GoesInto = CurrentTime.Hours / 24;
159  Integer Remainder = CurrentTime.Hours - GoesInto * 24;
160  CurrentTime.Hours = Remainder;
161  CurrentTime.Days+=GoesInto;
162  }else{ return; }
163  }
164 
165  Boole ExtendedTimer::CheckMicroSeconds(const ExtendedTimer::TimeStruct Struct)
166  {
167  if(GetStruct(Struct).Microseconds > 1000)
168  {
169  Integer GoesInto = GetStruct(Struct).Microseconds * 0.001;
170  Integer Remainder = GetStruct(Struct).Microseconds - GoesInto * 1000;
171  GetStruct(Struct).Microseconds = Remainder;
172  GetStruct(Struct).Milliseconds+=GoesInto;
173  return true;
174  }else{ return false; }
175  }
176 
177  Boole ExtendedTimer::CheckMilliSeconds(const ExtendedTimer::TimeStruct Struct)
178  {
179  if(GetStruct(Struct).Milliseconds > 1000)
180  {
181  Integer GoesInto = GetStruct(Struct).Milliseconds * 0.001;
182  Integer Remainder = GetStruct(Struct).Milliseconds - GoesInto * 1000;
183  GetStruct(Struct).Milliseconds = Remainder;
184  GetStruct(Struct).Seconds+=GoesInto;
185  return true;
186  }else{ return false; }
187  }
188 
189  Boole ExtendedTimer::CheckSeconds(const ExtendedTimer::TimeStruct Struct)
190  {
191  if(GetStruct(Struct).Seconds > 60)
192  {
193  Integer GoesInto = GetStruct(Struct).Seconds / 60;
194  Integer Remainder = GetStruct(Struct).Seconds - GoesInto * 60;
195  GetStruct(Struct).Seconds = Remainder;
196  GetStruct(Struct).Minutes+=GoesInto;
197  return true;
198  }else{ return false; }
199  }
200 
201  Boole ExtendedTimer::CheckMinutes(const ExtendedTimer::TimeStruct Struct)
202  {
203  if(GetStruct(Struct).Minutes > 60)
204  {
205  Integer GoesInto = GetStruct(Struct).Minutes / 60;
206  Integer Remainder = GetStruct(Struct).Minutes - GoesInto * 60;
207  GetStruct(Struct).Minutes = Remainder;
208  GetStruct(Struct).Hours+=GoesInto;
209  return true;
210  }else{ return false; }
211  }
212 
213  Boole ExtendedTimer::CheckHours(const ExtendedTimer::TimeStruct Struct)
214  {
215  if(GetStruct(Struct).Hours > 24)
216  {
217  Integer GoesInto = GetStruct(Struct).Hours / 24;
218  Integer Remainder = GetStruct(Struct).Hours - GoesInto * 24;
219  GetStruct(Struct).Hours = Remainder;
220  GetStruct(Struct).Days+=GoesInto;
221  return true;
222  }else{ return false; }
223  }
224 
225  Boole ExtendedTimer::CheckDays(const ExtendedTimer::TimeStruct Struct)
226  {
227  return false;
228  }
229 
230  Boole ExtendedTimer::CheckAll(const ExtendedTimer::TimeStruct Struct)
231  {
232  if(CheckMicroSeconds(Struct) || CheckMilliSeconds(Struct) || CheckSeconds(Struct)
233  || CheckMinutes(Struct) || CheckHours(Struct) || CheckDays(Struct))
234  {
235  return true;
236  }else{ return false; }
237  }
238 
239  Boole ExtendedTimer::CompareCurrentAndGoal(const Integer Current, const Integer Goal)
240  {
241  return Timer::StopWatch == Style ? Current < Goal : Current > Goal;
242  /*if(Timer::StopWatch == Style)
243  {
244  return Current < Goal;
245  }else{
246  return Current > Goal;
247  }*/
248  }
249 
250  Boole ExtendedTimer::GoalReached()
251  {
252  if(CurrentTime.Days == GoalTime.Days)
253  {
254  if(CurrentTime.Hours == GoalTime.Hours)
255  {
256  if(CurrentTime.Minutes == GoalTime.Minutes)
257  {
258  if(CurrentTime.Seconds == GoalTime.Seconds)
259  {
260  if(CurrentTime.Milliseconds == GoalTime.Milliseconds)
261  {
262  if(CurrentTime.Microseconds == GoalTime.Microseconds)
263  {
264  return true;
265  }
266  else if (CompareCurrentAndGoal(CurrentTime.Microseconds,GoalTime.Microseconds))
267  { return true; }else{ return false; }
268  }
269  else if (CompareCurrentAndGoal(CurrentTime.Milliseconds,GoalTime.Milliseconds))
270  { return true; }else{ return false; }
271  }
272  else if (CompareCurrentAndGoal(CurrentTime.Seconds,GoalTime.Seconds))
273  { return true; }else{ return false; }
274  }
275  else if (CompareCurrentAndGoal(CurrentTime.Minutes,GoalTime.Minutes))
276  { return true; }else{ return false; }
277  }
278  else if (CompareCurrentAndGoal(CurrentTime.Hours,GoalTime.Hours))
279  { return true; }else{ return false; }
280  }
281  else if (CompareCurrentAndGoal(CurrentTime.Days,GoalTime.Days))
282  { return true; }else{ return false; }
283  }
284 
286  {
287  CurrentTime.Milliseconds = InitialTime.Milliseconds;
288  CurrentTime.Seconds = InitialTime.Seconds;
289  CurrentTime.Minutes = InitialTime.Minutes;
290  CurrentTime.Hours = InitialTime.Hours;
291  CurrentTime.Days = InitialTime.Days;
292  }
293 
295  {
296  ResetAtGoal = AutoReset;
297  return *this;
298  }
299 
301  {
302  GetStruct(Struct).Microseconds = MS;
303  CheckAll(Struct);
304  return *this;
305  }
306 
308  {
309  GetStruct(Struct).Milliseconds = MS;
310  CheckMilliSeconds(Struct);
311  CheckSeconds(Struct);
312  CheckMinutes(Struct);
313  CheckHours(Struct);
314  return *this;
315  }
316 
318  {
319  GetStruct(Struct).Seconds = Sec;
320  CheckSeconds(Struct);
321  CheckMinutes(Struct);
322  CheckHours(Struct);
323  return *this;
324  }
325 
327  {
328  GetStruct(Struct).Minutes = Min;
329  CheckMinutes(Struct);
330  CheckHours(Struct);
331  return *this;
332  }
333 
335  {
336  GetStruct(Struct).Hours = Hr;
337  CheckHours(Struct);
338  return *this;
339  }
340 
342  {
343  GetStruct(Struct).Days = Day;
344  return *this;
345  }
346 
348  {
349  return GetStruct(Struct).Milliseconds;
350  }
351 
353  {
354  return GetStruct(Struct).Seconds;
355  }
356 
358  {
359  return GetStruct(Struct).Minutes;
360  }
361 
363  {
364  return GetStruct(Struct).Hours;
365  }
366 
368  {
369  return GetStruct(Struct).Days;
370  }
371 }
372 
373 #endif
TimeStruct
The internal time struct to be used...
Definition: extendedtimer.h:76
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual ExtendedTimer & SetSeconds(Integer Sec, const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Sets the value for Seconds of the specified struct.
virtual ExtendedTimer & SetMinutes(Integer Min, const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Sets the value for Minutes of the specified struct.
virtual Integer GetMinutes(const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Gets the current internal Minute count.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
virtual ~ExtendedTimer()
Class destructor.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
virtual Integer GetMilliseconds(const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Gets the current internal Millisecond count.
virtual Integer GetHours(const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Gets the current internal Hour count.
virtual ExtendedTimer & SetMicroseconds(Integer MS, const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Sets the value for Microseconds of the specified struct.
An enhanced timer class that can store and track many units of time.
Definition: extendedtimer.h:72
virtual ExtendedTimer & SetDays(Integer Day, const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Sets the value for Days of the specified struct.
virtual ExtendedTimer & SetHours(Integer Hr, const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Sets the value for Hours of the specified struct.
ExtendedTimer(const Timer::TimerStyle style)
Standard Constructor.
virtual ExtendedTimer & SetMilliseconds(Integer MS, const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Sets the value for Milliseconds of the specified struct.
A container for the metrics of time relevant for the timer class.
Definition: extendedtimer.h:52
virtual Integer GetDays(const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Gets the current internal Day count.
A basic timer class to assist in timed operations.
Definition: timer.h:67
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
virtual Integer GetSeconds(const ExtendedTimer::TimeStruct Struct=ExtendedTimer::Current)
Gets the current internal Second count.
virtual void Reset()
Sets the current values to their initial values.
virtual ExtendedTimer & SetAutoReset(const Boole AutoReset)
Sets whether or not the Timer should reset if it reaches it's goal. Ex. If a stopwatch reaches 0...