- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/src/frost_utils_timers.cpp

Go to the documentation of this file.

    00001 /* ###################################### */
    00002 /* ###     Frost Engine, by Kalith    ### */
    00003 /* ###################################### */
    00004 /*             Timers source              */
    00005 /*                                        */
    00006 /*                                        */
    00007
    00008 #include "frost_utils_timers.h"
    00009 #include "frost_utils_timemanager.h"
    00010
    00011 using namespace std;
    00012
    00013 namespace Frost
    00014 {
    00015     const s_str PeriodicTimer::CLASS_NAME = "PeriodicTimer";
    00016     const s_str Timer::CLASS_NAME = "Timer";
    00017
    00018     PeriodicTimer::PeriodicTimer( const s_double& dDuration, StartType mType, const s_bool& bTickFirst )
    00019     {
    00020         dDuration_ = dDuration;
    00021         if (bTickFirst) dElapsed_ = dDuration;
    00022         else dElapsed_ = 0.0;
    00023         mType_ = mType;
    00024         bPaused_ = true;
    00025         bFirstTick_ = true;
    00026         if (mType == START_NOW) Start();
    00027     }
    00028
    00029     s_double PeriodicTimer::GetElapsed()
    00030     {
    00031         if (!bPaused_)
    00032             return dElapsed_ + TimeManager::GetSingleton()->GetTime() - dStart_;
    00033         else
    00034             return dElapsed_;
    00035     }
    00036
    00037     const s_double& PeriodicTimer::GetPeriod() const
    00038     {
    00039         return dDuration_;
    00040     }
    00041
    00042     const s_bool& PeriodicTimer::IsPaused() const
    00043     {
    00044         return bPaused_;
    00045     }
    00046
    00047     s_bool PeriodicTimer::Ticks()
    00048     {
    00049         if ( (mType_ == START_FIRST_TICK) && bFirstTick_ )
    00050         {
    00051             Start();
    00052             bFirstTick_ = false;
    00053         }
    00054
    00055         if (!bPaused_)
    00056         {
    00057             if ((dElapsed_ + TimeManager::GetSingleton()->GetTime() - dStart_) >= dDuration_)
    00058             {
    00059                 Zero();
    00060                 return true;
    00061             }
    00062             else
    00063                 return false;
    00064         }
    00065         else
    00066         {
    00067             if (dElapsed_ >= dDuration_)
    00068                 return true;
    00069             else
    00070                 return false;
    00071         }
    00072     }
    00073
    00074     void PeriodicTimer::Stop()
    00075     {
    00076         dElapsed_ = 0.0;
    00077         bPaused_ = true;
    00078     }
    00079
    00080     void PeriodicTimer::Start()
    00081     {
    00082         if (bPaused_)
    00083         {
    00084             dStart_ = TimeManager::GetSingleton()->GetTime();
    00085             bPaused_ = false;
    00086         }
    00087     }
    00088
    00089     void PeriodicTimer::Pause()
    00090     {
    00091         if (!bPaused_)
    00092         {
    00093             dElapsed_ += TimeManager::GetSingleton()->GetTime() - dStart_;
    00094             bPaused_ = true;
    00095         }
    00096     }
    00097
    00098     void PeriodicTimer::Zero()
    00099     {
    00100         dElapsed_ = 0.0;
    00101         dStart_ = TimeManager::GetSingleton()->GetTime();
    00102     }
    00103
    00104     Timer::Timer(const StartType& mType)
    00105     {
    00106         if (mType == START_PAUSED)
    00107             bPaused_ = true;
    00108         else
    00109             dStart_ = TimeManager::GetSingleton()->GetTime();
    00110     }
    00111
    00112     void Timer::AddTime( const s_double& dAdditionnalTime )
    00113     {
    00114         dStart_ -= dAdditionnalTime;
    00115     }
    00116
    00117     void Timer::SetTime( const s_double& dTime )
    00118     {
    00119         dElapsed_ = dTime;
    00120         dStart_ = TimeManager::GetSingleton()->GetTime();
    00121     }
    00122
    00123     s_double Timer::GetElapsed()
    00124     {
    00125         if (!bPaused_)
    00126             return dElapsed_ + TimeManager::GetSingleton()->GetTime() - dStart_;
    00127         else
    00128             return dElapsed_;
    00129     }
    00130
    00131     const s_bool& Timer::IsPaused() const
    00132     {
    00133         return bPaused_;
    00134     }
    00135
    00136     void Timer::Stop()
    00137     {
    00138         dElapsed_ = 0.0;
    00139         bPaused_ = true;
    00140     }
    00141
    00142     void Timer::Start( const s_bool& bReset )
    00143     {
    00144         dStart_ = TimeManager::GetSingleton()->GetTime();
    00145         bPaused_ = false;
    00146         if (bReset)
    00147             dElapsed_ = 0.0;
    00148     }
    00149
    00150     void Timer::Pause()
    00151     {
    00152         if (!bPaused_)
    00153         {
    00154             dElapsed_ += TimeManager::GetSingleton()->GetTime() - dStart_;
    00155             bPaused_ = true;
    00156         }
    00157     }
    00158
    00159     void Timer::Zero()
    00160     {
    00161         dElapsed_ = 0.0;
    00162         dStart_ = TimeManager::GetSingleton()->GetTime();
    00163     }
    00164 }