AppEasy Core SDK  1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
CzTime.h
Go to the documentation of this file.
00001 // 
00002 //
00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine
00004 //
00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk
00006 //
00007 // For updates, tutorials and more details check out www.appeasymobile.com
00008 //
00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin.
00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support
00011 //
00012 //
00013 
00014 #if !defined(_CCZ_TIME_H_)
00015 #define _CCZ_TIME_H_
00016 
00017 #include "CzTypes.h"
00018 
00019 /**
00020  @addtogroup Core
00021  @{
00022  */
00023  
00024 /**
00025  @class CzTimer
00026 
00027  @brief Polled timer
00028 
00029  <h1>Introduction</h1>
00030 
00031  Time plays a very  important role in app and game development. Time allows us to perform useful tasks such as time events, fire off events at regular intervals and stabilise animations etc..
00032 
00033  CzTimer provides a software based timer mechanism for timing events as well a static method for retrieving the current time in milliseconds. Timers will not automatically fire off events 
00034  when they expire, instead they have to be polled.
00035 
00036  Timers provide additional functionality for calculating how much time is left on the timer as well as how much time has expired since the timer was started.
00037 
00038  Timers don't really have much of an overhead so you can create as many as you like.
00039 
00040  <h1>Getting the Current Time</h1>
00041 
00042  To retrieve the current time in milliseconds CzTimer provides a static method:
00043 
00044  @code
00045 uint64  GetCurrentTimeMs()
00046  @endcode
00047 
00048  To retrieve the current time in UTC (universal coordinated time) CzTimer provides a static method:
00049 
00050  @code
00051 int64   GetCurrentTimeUTC()
00052  @endcode
00053 
00054  <h1>Creating and Using Timers</h1>
00055 
00056  Creating a timer is a simple case of declaring or allocating a CzTimer then setting it off going. To check the timer you then poll it to check to see if it has timed out. Here's an example:
00057 
00058  @code
00059 // Create a timer that expires after 10 seconds
00060 CzTimer BusyTimer;
00061 BusyTimer.setDuration(10000);
00062 
00063 // Check to see if the timer has timed out
00064 if (BusyTimer.HasTimedOut())
00065 {
00066 }
00067  @endcode
00068 
00069  Timers can be reset, stopped and started using Reset(), Stop() and Start().
00070 
00071  */
00072 
00073 class CzTimer
00074 {
00075 protected:
00076     bool        Started;            ///< true if timer started
00077     uint64      LastTime;           ///< Time that the timer was set (milliseconds)
00078 
00079     // Properties
00080 private:
00081     uint64      Duration;           ///< Duration of timer in milliseconds
00082     bool        AutoReset;          ///< Auto reset the timer when it times out
00083 public:
00084     void        setDuration(uint64 millseconds_duration, bool start = true);        ///< Sets the duration of the timer
00085     uint64      getDuration() const;                                                ///< Gets the duration of the timer
00086     void        setAutoReset(bool auto_reset);                                      ///< If true the timer will aito restart once it times out
00087     bool        getAutoReset() const;                                               ///< Returns auto resset status
00088     bool        hasStarted() const;                                                 ///< Returns true if the timer has started
00089     bool        hasStopped() const;                                                 ///< Returns true if the timer has stopped
00090     // Properties end
00091 
00092 public:
00093     CzTimer() : LastTime(0), Duration(0), AutoReset(false), Started(false) { }
00094     CzTimer(int millseconds_duration);
00095     virtual ~CzTimer() {    }
00096     
00097     virtual bool HasTimedOut();                                                     ///< Returns true if the timer has timed out
00098     uint64      GetElapsedTime() const;                                             ///< Return the amount of time since the timer was started
00099     uint64      GetTimeDiff(uint64 this_time) const;                                ///< returns the difference in time between the supplied time and the timers current time
00100     uint64      GetTimeDiff() const;                                                ///< returns the difference in time between the current time and the timers current time
00101     uint64      GetTimeLeft() const;                                                ///< Returns the amount of time left before the timer times out
00102     void        Reset();                                                            ///< Restarts the timer
00103     void        Start();                                                            ///< Starts the timer
00104     void        Stop();                                                             ///< Stops the timer
00105 
00106     static uint64   GetCurrentTimeMs();                                             ///< Static method that returns the current system time in millseconds
00107     static int64    GetCurrentTimeUTC();                                            ///< Static method that returns the current system UTC time
00108 
00109 };
00110 
00111 /// @}
00112 
00113 #endif  // _CCZ_TIME_H_