- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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

Go to the documentation of this file.

    00001 /* ###################################### */
    00002 /* ###     Frost Engine, by Kalith    ### */
    00003 /* ###################################### */
    00004 /*            Profiler source             */
    00005 /*                                        */
    00006 /*  ## : A class that manages code        */
    00007 /*  profiling.                            */
    00008 /*                                        */
    00009
    00010 #include "frost_utils_timemanager.h"
    00011 #include "frost_utils_profiler.h"
    00012 #include "frost_utils_stdhelper.h"
    00013 #include "frost_utils_log.h"
    00014 #include "frost_utils_math.h"
    00015 #include "frost_utils_timers.h"
    00016
    00017 #ifdef WIN32
    00018     #include <sys/time.h>
    00019     #include <sys/timeb.h>
    00020 #else
    00021     #include <sys/time.h>
    00022 #endif
    00023
    00024 using namespace std;
    00025
    00026 namespace Frost
    00027 {
    00028     const s_str TimeManager::CLASS_NAME = "TimeManager";
    00029
    00030     TimeManager::TimeManager()
    00031     {
    00032         dDelta_ = 0.017;
    00033         uiWorstFPS_ = s_uint::INF;
    00034         pGetTimeFunction_ = nullptr;
    00035     }
    00036
    00037     TimeManager::~TimeManager()
    00038     {
    00039     }
    00040
    00041     void TimeManager::SetGetTimeFunction( GetTimeFunction pFunc )
    00042     {
    00043         pGetTimeFunction_ = pFunc;
    00044     }
    00045
    00046     void TimeManager::Initialize()
    00047     {
    00048         pFPSTimer_ = s_refptr<Timer>(new Timer(Timer::START_NOW));
    00049         dLastTime_ = dCurTime_ = dStart_ = dLast_ = GetTime();
    00050
    00051         dProfileTime_ = 0.0;
    00052         uiFPS_ = 0;
    00053         dDelta_ = 0.017;
    00054         uiAvgFPS_ = 0;
    00055         uiFrameNbr_ = 0;
    00056         uiBestFPS_ = 0;
    00057         uiWorstFPS_ = s_uint::INF;
    00058     }
    00059
    00060     void TimeManager::Update()
    00061     {
    00062         // Get delta time
    00063         dLastTime_ = dCurTime_;
    00064         dCurTime_ = GetTime();
    00065         dDelta_ = dCurTime_ - dLastTime_;
    00066
    00067         // Update FPS
    00068         static s_uint uiFrameRateCount = 0;
    00069         static s_uint uiFrameCount = 0;
    00070         ++uiFrameCount;
    00071
    00072         if (dDelta_ < 0.0001)
    00073             uiFrameRateCount += 10000;
    00074         else
    00075             uiFrameRateCount += s_uint(1.0/dDelta_);
    00076
    00077         if (pFPSTimer_ && pFPSTimer_->GetElapsed() > 1.0)
    00078         {
    00079             pFPSTimer_->Zero();
    00080
    00081             uiFPS_ = uiFrameRateCount/uiFrameCount;
    00082             uiFrameRateCount = 0;
    00083             uiFrameCount = 0;
    00084
    00085             uiAvgFPS_ += uiFPS_;
    00086             ++uiFrameNbr_;
    00087
    00088             uiBestFPS_ = s_uint::Max(uiBestFPS_, uiFPS_);
    00089             uiWorstFPS_ = s_uint::Min(uiWorstFPS_, uiFPS_);
    00090         }
    00091     }
    00092
    00093     s_double TimeManager::GetDelta() const
    00094     {
    00095         if (dDelta_ > 0.0001)
    00096             return dDelta_;
    00097         else
    00098         {
    00099             if (uiFPS_ != 0)
    00100                 return 1.0/uiFPS_.Get();
    00101             else
    00102                 return 0.0;
    00103         }
    00104     }
    00105
    00106     const s_uint& TimeManager::GetFPS() const
    00107     {
    00108         return uiFPS_;
    00109     }
    00110
    00111     s_uint TimeManager::GetAverageFPS() const
    00112     {
    00113         if (uiFrameNbr_ == 0)
    00114             return s_uint(s_uint::INTEGER_NAN);
    00115         else
    00116             return uiAvgFPS_/uiFrameNbr_;
    00117     }
    00118
    00119     const s_uint& TimeManager::GetBestFPS() const
    00120     {
    00121         return uiBestFPS_;
    00122     }
    00123
    00124     const s_uint& TimeManager::GetWorstFPS() const
    00125     {
    00126         return uiWorstFPS_;
    00127     }
    00128
    00129     s_double TimeManager::GetTime() const
    00130     {
    00131         if (pGetTimeFunction_)
    00132             return (*pGetTimeFunction_)();
    00133         else
    00134             return 0.0;
    00135     }
    00136
    00137     s_str TimeManager::GetPlayTime() const
    00138     {
    00139         s_double dTime = GetTime();
    00140         s_uint uiHours(s_double::RoundDown(dTime/3600.0));
    00141         dTime -= s_double(uiHours)*3600.0;
    00142         s_uint uiMinutes(s_double::RoundDown(dTime/60.0));
    00143         dTime -= s_double(uiMinutes)*60.0;
    00144         s_uint uiSeconds(s_double::RoundDown(dTime));
    00145         dTime -= s_double(uiSeconds);
    00146         s_uint uiMilliseconds(s_double::RoundDown(dTime*1000));
    00147
    00148         s_str sTime = "[";
    00149         sTime += s_str::Convert(uiHours, 2) + ":";
    00150         sTime += s_str::Convert(uiMinutes, 2) + ":";
    00151         sTime += s_str::Convert(uiSeconds, 2) + ":";
    00152         sTime += s_str::Convert(uiMilliseconds, 3) + "]";
    00153
    00154         return sTime;
    00155     }
    00156
    00157     s_ptr<Profiler> TimeManager::GetProfiler( const s_uint& uiGroup, const s_str& sName, const s_bool& bRecord )
    00158     {
    00159         if (bProfiling_ == true)
    00160         {
    00161             bProfiled_ = true;
    00162
    00163             if (!lProfilerList_.Find(sName))
    00164             {
    00165                 lProfilerList_[sName] = Profiler(uiGroup, sName, bRecord);
    00166             }
    00167
    00168             return &lProfilerList_[sName];
    00169         }
    00170         else
    00171             return nullptr;
    00172     }
    00173
    00174     void TimeManager::SetProfiling( const s_bool& bProfiling )
    00175     {
    00176         // Update total profiling time
    00177         if (bProfiling && !bProfiling_)
    00178             dLast_ = GetTime();
    00179         else if (!bProfiling && bProfiling_)
    00180             dProfileTime_ = GetTime() - dLast_;
    00181
    00182         bProfiling_ = bProfiling;
    00183     }
    00184
    00185     void TimeManager::Print( const s_uint& uiGroup )
    00186     {
    00187         if (bProfiled_)
    00188         {
    00189             if (bProfiling_)
    00190             {
    00191                 dProfileTime_ = GetTime() - dLast_;
    00192                 bProfiling_ = false;
    00193             }
    00194
    00195             Log("\n");
    00196             if (!uiGroup.IsValid())
    00197                 Log("# Profiling info : "+ s_str::Convert(dProfileTime_, 0, 2) +" s of profiling");
    00198             else
    00199                 Log("# Profiling info for group ["+ uiGroup +"] : "+ dProfileTime_ +" s of profiling");
    00200             map<s_str, Profiler>::iterator iter;
    00201             foreach (iter, lProfilerList_)
    00202             {
    00203                 s_ptr<Profiler> pProfiler = &iter->second;
    00204                 if ( (pProfiler->GetGroup() == uiGroup) || (!uiGroup.IsValid()) )
    00205                     pProfiler->PrintProfile(dProfileTime_);
    00206             }
    00207             Log("# end.");
    00208         }
    00209     }
    00210
    00211     s_uint TimeManager::GetYear() const
    00212     {
    00213         time_t iTimestamp = time(nullptr);
    00214         return localtime(&iTimestamp)->tm_year + 1900;
    00215     }
    00216
    00217     s_uint TimeManager::GetMonth() const
    00218     {
    00219         time_t iTimestamp = time(nullptr);
    00220         return localtime(&iTimestamp)->tm_mon + 1;
    00221     }
    00222
    00223     s_uint TimeManager::GetDayName() const
    00224     {
    00225         time_t iTimestamp = time(nullptr);
    00226         return localtime(&iTimestamp)->tm_wday + 1;
    00227     }
    00228
    00229     s_uint TimeManager::GetDay() const
    00230     {
    00231         time_t iTimestamp = time(nullptr);
    00232         return localtime(&iTimestamp)->tm_mday;
    00233     }
    00234
    00235     s_uint TimeManager::GetHour() const
    00236     {
    00237         time_t iTimestamp = time(nullptr);
    00238         return localtime(&iTimestamp)->tm_hour;
    00239     }
    00240
    00241     s_uint TimeManager::GetMinutes() const
    00242     {
    00243         time_t iTimestamp = time(nullptr);
    00244         return localtime(&iTimestamp)->tm_min;
    00245     }
    00246
    00247     s_uint TimeManager::GetSeconds() const
    00248     {
    00249         time_t iTimestamp = time(nullptr);
    00250         return localtime(&iTimestamp)->tm_sec;
    00251     }
    00252
    00253     s_uint TimeManager::GetMicroseconds() const
    00254     {
    00255     #ifdef WIN32
    00256         timeb tb;
    00257         ftime(&tb);
    00258         return (uint)tb.millitm*1000;
    00259     #else
    00260         timeval tv;
    00261         gettimeofday(&tv, nullptr);
    00262         return tv.tv_usec;
    00263     #endif
    00264     }
    00265 }