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
CzTimer.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_TIMER_H_)
00015 #define _CCZ_TIMER_H_
00016 
00017 #include "CzXoml.h"
00018 #include "CzTypes.h"
00019 #include "CzTime.h"
00020 #include "CzEvents.h"
00021 
00022 /**
00023  @addtogroup XOML
00024  @{
00025  */
00026  
00027 /**
00028  @class CzXomlTimer
00029 
00030  @brief Polled timer
00031 
00032  <h1>Introduction</h1>
00033 
00034  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..
00035 
00036  CzXomlTimer 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 
00037  when they expire, instead they have to be polled.
00038 
00039  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.
00040 
00041  Timers don't really have much of an overhead so you can create as many as you like.
00042 
00043  <h1>Getting the Current Time</h1>
00044 
00045  To retrieve the current time in milliseconds CzXomlTimer provides a static method:
00046 
00047  @code
00048 uint64  GetCurrentTimeMs()
00049  @endcode
00050 
00051  To retrieve the current time in UTC (universal coordinated time) CzXomlTimer provides a static method:
00052 
00053  @code
00054 int64   GetCurrentTimeUTC()
00055  @endcode
00056 
00057  <h1>Creating and Using Timers</h1>
00058 
00059  Creating a timer is a simple case of declaring or allocating a CzXomlTimer 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:
00060 
00061  @code
00062 // Create a timer that expires after 10 seconds
00063 CzXomlTimer BusyTimer;
00064 BusyTimer.setDuration(10000);
00065 
00066 // Check to see if the timer has timed out
00067 if (BusyTimer.HasTimedOut())
00068 {
00069 }
00070  @endcode
00071 
00072  Timers can be reset, stopped and started using Reset(), Stop() and Start().
00073 
00074  */
00075 
00076 class CzXomlTimer : public IzXomlResource
00077 {
00078     // Properties
00079 protected:
00080     bool        Running;                ///< Timer running status
00081     float       CurrentTime;            ///< Current timer time
00082     float       Duration;               ///< Duration of timer
00083     int         RepeatCount;            ///< Number of times to repeat before stopping, 0 means repeat forever
00084     int         OriginalRepeatCount;    ///< Original number of times to repeat before stopping
00085     bool        AutoDelete;             ///< If set to true then this timer will de4stroy itself when it times out
00086 public:
00087     void        setCurrentTime(float time)  { CurrentTime = time; }
00088     float       getCurrentTime() const      { return CurrentTime; }
00089     void        setDuration(float duration) { Duration = duration; }
00090     float       getDuration() const         { return Duration; }
00091     void        setRepeatCount(int count)   { RepeatCount = count; }
00092     int         getRepeatCount() const      { return RepeatCount; }
00093     void        setAutoDelete(bool auto_delete) { AutoDelete = auto_delete; }
00094     bool        isAutoDelete() const        { return AutoDelete; }
00095     void        start()                     { Running = true; }
00096     void        stop()                      { Running = false; }
00097     void        restart()                   { Running = true; CurrentTime = 0; RepeatCount = OriginalRepeatCount; }
00098     bool        isRunning() const           { return Running; }
00099     CzEventManager* getEventsManager()      { return EventsManager; }
00100     // Properties end
00101 
00102 protected:
00103     CzEventManager* EventsManager;              ///< List of events that the timer handles
00104     void            addEventsManager();
00105 
00106 public:
00107     CzXomlTimer() : IzXomlResource(), CurrentTime(0), Duration(0), RepeatCount(1), OriginalRepeatCount(0), AutoDelete(false), EventsManager(NULL), Running(true)
00108     {
00109         setClassType("timer");
00110     }
00111     virtual ~CzXomlTimer();
00112 
00113     // Update the timer
00114     bool        Update(float dt);
00115 
00116     // Event notification
00117     void        ProcessEventActions(unsigned int event_name);
00118     void        NotifyTimeout();
00119     void        NotifyRepeat();
00120     
00121     // Implementation of IzXomlResource interface
00122     int         LoadFromXoml(IzXomlResource* parent, bool load_children, CzXmlNode* node);
00123 
00124 };
00125 
00126 /**
00127  @class CzXomlTimerCreator
00128 
00129  @brief Creates an instance of a Timer object.
00130 
00131  */
00132 
00133 class CzXomlTimerCreator : public IzXomlClassCreator
00134 {
00135 public:
00136     CzXomlTimerCreator()
00137     {
00138         setClassName("timer");
00139     }
00140     IzXomlResource* CreateInstance(IzXomlResource* parent)  { return new CzXomlTimer(); }
00141 };
00142 
00143 /**
00144  @class CzTimersManager
00145 
00146  @brief Manager a collection of XOML timers
00147 
00148  */
00149 
00150 class CzTimersManager
00151 {
00152 public:
00153     // Public access to iteration
00154     typedef CzList<CzXomlTimer*>::iterator _Iterator;
00155     _Iterator               begin()     { return Timers.begin(); }
00156     _Iterator               end()       { return Timers.end(); }
00157 
00158 protected:
00159     // Properties
00160     CzList<CzXomlTimer*>    Timers;             // A collection of Timers
00161     IzXomlResource*         Parent;             // Parent container
00162     CzList<CzXomlTimer*>    Removals;           // Timers that require removal
00163 public:
00164     void                    addTimer(CzXomlTimer* Timer);
00165     void                    removeTimer(CzXomlTimer* Timer);
00166     void                    removeTimer(unsigned int name_hash);
00167     CzXomlTimer*            findTimer(unsigned int name_hash);
00168     CzXomlTimer*            findTimer(const char* name);
00169     void                    clearTimers();
00170     void                    setParent(IzXomlResource* scene)        { Parent = scene; }
00171     IzXomlResource*         getParent()                             { return Parent; }
00172     // Properties end
00173 
00174 public:
00175     CzTimersManager() : Parent(NULL) {}
00176     virtual ~CzTimersManager() { clearTimers(); }
00177 
00178     void    Update(float dt);
00179 
00180     // Utility
00181     static CzXomlTimer* FindTimer(const char* name, IzXomlResource* container);
00182     static CzXomlTimer* FindTimer(unsigned int name_hash, IzXomlResource* container);
00183     static CzXomlTimer* FindTimer(unsigned int name_hash, CzScene* container, unsigned int actor_hash);
00184     static CzXomlTimer* FindTimer(const char* name, CzScene* container, const char* actor_name);
00185 };
00186 
00187 
00188 
00189 
00190 /// @}
00191 
00192 #endif  // _CCZ_TIMER_H_