lugre_timer.h

Go to the documentation of this file.
00001 /*
00002 http://www.opensource.org/licenses/mit-license.php  (MIT-License)
00003 
00004 Copyright (c) 2007 Lugre-Team
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a copy
00007 of this software and associated documentation files (the "Software"), to deal
00008 in the Software without restriction, including without limitation the rights
00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, subject to the following conditions:
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022 THE SOFTWARE.
00023 */
00024 #ifndef LUGRE_TIMER_H
00025 #define LUGRE_TIMER_H
00026 
00027 #include "lugre_listener.h"
00028 #include <set>
00029 #include <list>
00030 #include <vector>
00031 
00032 namespace Lugre {
00033 
00040 class cTimer { public :
00041     static size_t       miTimeSinceLastFrame;
00042     static size_t       miLastFrameTime;
00043     static size_t       miCurFrameNum;
00044     static float        mfPhysStepTime;
00045     
00047     enum {
00048         kListenerEvent_Timeout,
00049         kListenerEvent_Interval,
00050         kListenerEvent_FrameInterval
00051     };
00052     enum { kMaxFrameIntervalExp = 24 }; // a callback every 1<<24 frames (more than 3 days at 60fps) should be enough, use timeout if you really need that...
00053     
00054     inline static cTimer* GetSingletonPtr (cTimer* pSetSingleton=0) {
00055         static cTimer* mpSingleton = 0;
00056         if (pSetSingleton) mpSingleton = pSetSingleton;
00057         return mpSingleton;
00058     }
00059     
00060             cTimer  (const size_t iTime);
00061     virtual ~cTimer ();
00062     
00063     void    StartFrame  (const size_t iTime); 
00064     
00066     class cTimerRegistration { public:
00067         size_t                  miTime;
00068         size_t                  miInterval; 
00069         size_t                  miFrameInterval; 
00070         size_t                  miIntervalCount; 
00071         cSmartPtr< cListener >  mpListener;
00072         void*                   miUserData;
00073         bool                    mbIsAlive;
00074         
00075         cTimerRegistration  (cListener* pListener,void* iUserData,const size_t iTime,const size_t iInterval=0,const size_t iFrameInterval=0) :
00076             mpListener(pListener), miUserData(iUserData), miTime(iTime), miInterval(iInterval), miFrameInterval(iFrameInterval), miIntervalCount(0), mbIsAlive(true) {
00077             //printf("\n\ncTimerRegistration(pListener=%#08x,iUserData=%d,iTime=%d,iInterval=%d,iFrameInterval=%d)\n\n",pListener,iUserData,iTime,iInterval,iFrameInterval);
00078         }
00079         
00081         bool            Trigger     (const size_t iCurTime,const size_t iEvent);
00082         inline  void    Cancel      () { mbIsAlive = false; }
00083     };
00084     
00085     // registration
00086     cTimerRegistration*   RegisterTimeoutListener       (cListener* pListener,const size_t iTimeOut,            void* userdata = 0);
00087     cTimerRegistration*   RegisterIntervalListener      (cListener* pListener,const size_t iInterval,           void* userdata = 0);
00088     cTimerRegistration*   RegisterFrameIntervalListener (cListener* pListener,const size_t iFrameIntervalExp,   void* userdata = 0);
00089     
00090     // FrameInterval timing
00091     inline  bool    IsCurFrameInInterval        (const size_t iInterval) { return IsFrameInInterval(iInterval,miCurFrameNum); }
00092     static inline   size_t  GetIntervalStart    (const size_t iInterval) { return (iInterval-1)/2; }
00093     static inline   bool    IsFrameInInterval   (const size_t iInterval,const size_t iFrame) {
00094         return ((iFrame+iInterval-GetIntervalStart(iInterval)) % iInterval) == 0;
00095     }
00096     
00097     // removal of registrations is done only during iteration, so no search is required, the timeouts are sorted so iteration is short
00098     struct cTimerRegistrationCompare { bool operator()(cTimerRegistration* a, cTimerRegistration* b) { return a->miTime < b->miTime; } };
00099     std::multiset<          cTimerRegistration*,cTimerRegistrationCompare>  mlTimeouts;
00100     std::list<              cTimerRegistration*>                            mlIntervals;
00101     std::vector< std::list< cTimerRegistration*>* >                         mlFrameIntervals;
00102 };
00103 
00104 };
00105 
00106 #endif

Generated on Wed May 23 06:00:13 2012 for cpp by  doxygen 1.5.6