lugre_timer.cpp

Go to the documentation of this file.
00001 #include "lugre_prefix.h"
00002 #include "lugre_timer.h"
00003 
00004 
00005 
00006 
00007 namespace Lugre {
00008     
00009 size_t      cTimer::miTimeSinceLastFrame = 1;
00010 size_t      cTimer::miLastFrameTime = 0;
00011 size_t      cTimer::miCurFrameNum = 0;
00012 float       cTimer::mfPhysStepTime = 0.0;
00013 
00014 cTimer::cTimer(const size_t iTime) { PROFILE
00015     miLastFrameTime = iTime;
00016     // printf("\n\ncTimer(%d)\n\n",iTime);
00017 }
00018 
00019 cTimer::~cTimer() { PROFILE
00020     // TODO : release mem, but it is a global class so doesn't really matter for now...
00021     //for (std::map<size_t,cListenable*>::itor=mlIntervals.begin();itor!=mlIntervals.end();++itor) delete (*itor).second;
00022     //mlIntervals.clear();
00023 }
00024 
00025 void    cTimer::StartFrame  (const size_t iTime) { PROFILE
00026     ++miCurFrameNum;
00027     miTimeSinceLastFrame = iTime-miLastFrameTime;
00028     miLastFrameTime = iTime;
00029     mfPhysStepTime = float(miTimeSinceLastFrame)/1000.0;
00030     
00031     
00032     bool                    bRes;
00033     cTimerRegistration*     pReg;
00034     std::multiset<          cTimerRegistration*,cTimerRegistrationCompare>::iterator    itorTimeouts;
00035     std::list<              cTimerRegistration*>::iterator                              itorIntervals;
00036     std::list<              cTimerRegistration*>::iterator                              itorFrameIntervals;
00037     std::list<              cTimerRegistration*>*                                       pFrameIntervalList;
00038     
00039     // timeouts
00040     size_t iEraseCounter = 0;
00041     for (itorTimeouts=mlTimeouts.begin();itorTimeouts!=mlTimeouts.end();++itorTimeouts) {
00042         pReg = (*itorTimeouts);
00043         assert(pReg);
00044         if (pReg->miTime > iTime) break;
00045         pReg->Trigger(iTime,kListenerEvent_Timeout);
00046         // always remove timeouts after trigger, result doesn't matter
00047         delete pReg;
00048         ++iEraseCounter;
00049     }
00050     //printf("\n\ncTimer::StartFrame %d of %d being erased\n\n",iEraseCounter,mlTimeouts.size());
00051     mlTimeouts.erase(mlTimeouts.begin(),itorTimeouts); // erase all from start to break
00052     // as timeout values have to be GREATER than zero, no freshly inserted timeouts will be triggered directly after insertion
00053     
00054     // intervals
00055     for (itorIntervals=mlIntervals.begin();itorIntervals!=mlIntervals.end();) {
00056         // kListenerEvent_Interval
00057         pReg = (*itorIntervals);
00058         assert(pReg);
00059         if (pReg->miTime > iTime) {
00060             // not triggered
00061             ++itorIntervals;
00062         } else {
00063             bRes = pReg->Trigger(iTime,kListenerEvent_Interval);
00064             if (bRes) {
00065                 // keep current entry
00066                 ++itorIntervals;
00067             } else {
00068                 // remove current entry
00069                 mlIntervals.erase(itorIntervals++);
00070             }
00071         }
00072     }
00073     
00074     // frame intervals
00075     int i;
00076     for (i=0;i<mlFrameIntervals.size();++i) {
00077         if (!IsCurFrameInInterval(1<<i)) continue;
00078         pFrameIntervalList = mlFrameIntervals[i];
00079         if (!pFrameIntervalList) continue;
00080         //printf("FrameInterval %d / %d is active and has list\n",i,mlFrameIntervals.size());
00081         for (itorFrameIntervals=pFrameIntervalList->begin();itorFrameIntervals!=pFrameIntervalList->end();) {
00082             pReg = (*itorFrameIntervals);
00083             assert(pReg);
00084             bRes = pReg->Trigger(iTime,kListenerEvent_FrameInterval);
00085             if (bRes) {
00086                 // keep current entry
00087                 ++itorFrameIntervals;
00088             } else {
00089                 // remove current entry
00090                 pFrameIntervalList->erase(itorFrameIntervals++);
00091             }
00092         }
00093     }
00094 }
00095 
00096 
00097 bool    cTimer::cTimerRegistration::Trigger     (const size_t iCurTime,const size_t iEvent) { PROFILE
00098     //printf("\n\ncTimer::cTimerRegistration::Trigger(miTime=%d<=%d,%d) mbIsAlive=%d mpListener=%#08x\n\n",miTime,iCurTime,iEvent,mbIsAlive?1:0,(int)*mpListener);
00099     if (!*mpListener) return false;
00100     while (mbIsAlive && miTime <= iCurTime) {
00101         (*mpListener)->Listener_Notify(0,iEvent,static_cast<void*>(this),static_cast<void*>(miUserData));   
00102         if (miInterval == 0 && miFrameInterval == 0) return false;
00103         miTime += miInterval;
00104         ++miIntervalCount;
00105         if (miFrameInterval > 0) break;
00106     } ;
00107     return mbIsAlive;
00108 }
00109 
00111 cTimer::cTimerRegistration* cTimer::RegisterTimeoutListener         (cListener* pListener,const size_t iTimeOut,    void* userdata) { PROFILE
00112     cTimerRegistration* x = new cTimerRegistration(pListener,userdata,miLastFrameTime+((iTimeOut>0)?iTimeOut:1));
00113     mlTimeouts.insert(x);
00114     return x;
00115 }
00116 
00117 cTimer::cTimerRegistration* cTimer::RegisterIntervalListener        (cListener* pListener,const size_t iInterval,   void* userdata) { PROFILE
00118     cTimerRegistration* x = new cTimerRegistration(pListener,userdata,miLastFrameTime+iInterval,iInterval);
00119     mlIntervals.push_front(x);
00120     return x;
00121 }
00122 
00125 cTimer::cTimerRegistration* cTimer::RegisterFrameIntervalListener   (cListener* pListener,const size_t iFrameIntervalExp,void* userdata) { PROFILE
00126     assert( iFrameIntervalExp <= kMaxFrameIntervalExp);
00127     if (    iFrameIntervalExp >  kMaxFrameIntervalExp) { printf("cTimer::RegisterFrameIntervalListener illegal iFrameIntervalExp=%d\n",iFrameIntervalExp); return 0; }
00128     cTimerRegistration* x = new cTimerRegistration(pListener,userdata,miLastFrameTime,0,1<<iFrameIntervalExp);
00129     
00130     size_t minsize = iFrameIntervalExp+1;
00131     if (mlFrameIntervals.size() < minsize) 
00132         mlFrameIntervals.resize(minsize);
00133     
00134     std::list< cTimerRegistration*>*    pList = mlFrameIntervals[iFrameIntervalExp];
00135     if (!pList) { pList = new std::list< cTimerRegistration*>(); mlFrameIntervals[iFrameIntervalExp] = pList; }
00136     pList->push_front(x);
00137     return x;
00138 }
00139 
00141 /*
00142 class Bla : public cListener { public : 
00143     virtual void Listener_Notify (cListenable* pTarget,const size_t eventcode = 0,void* param = 0,void* userdata = 0) {
00144         printf("Bla1 eventcode=%d param=%#08x userdata=%#08x\n",eventcode,static_cast<int>(param),static_cast<int>(userdata));
00145     }
00146 };
00147 mpTimer->RegisterTimeoutListener            (new Bla(),8000,1);
00148 mpTimer->RegisterIntervalListener       (new Bla(),1500,2);
00149 mpTimer->RegisterFrameIntervalListener  (new Bla(),4,2); // every 2^4 = 16
00150 */
00151 
00152 };

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