|
|
|
@ -42,117 +42,139 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
|
|
|
|
#ifndef hsTimer_Defined |
|
|
|
|
#define hsTimer_Defined |
|
|
|
|
|
|
|
|
|
#include "hsWide.h" |
|
|
|
|
#include <chrono> |
|
|
|
|
|
|
|
|
|
class plTimerShare |
|
|
|
|
{ |
|
|
|
|
private: |
|
|
|
|
typedef std::chrono::high_resolution_clock Clock; |
|
|
|
|
typedef std::chrono::time_point<Clock> TimePoint; |
|
|
|
|
typedef Clock::duration Duration; |
|
|
|
|
|
|
|
|
|
protected: |
|
|
|
|
mutable bool fFirstTime; |
|
|
|
|
mutable hsWide fRawTimeZero; |
|
|
|
|
mutable TimePoint fRawTimeZero; |
|
|
|
|
mutable bool fResetSmooth; |
|
|
|
|
|
|
|
|
|
enum { |
|
|
|
|
kSmoothBuffLen = 10 |
|
|
|
|
}; |
|
|
|
|
double fSmoothBuff[kSmoothBuffLen]; |
|
|
|
|
int fCurrSlot; |
|
|
|
|
|
|
|
|
|
float fSysTimeScale; |
|
|
|
|
double fRealSeconds; |
|
|
|
|
double fSysSeconds; |
|
|
|
|
float fDelSysSeconds; |
|
|
|
|
float fFrameTimeInc; |
|
|
|
|
bool fRunningFrameTime; |
|
|
|
|
float fTimeClampSecs; |
|
|
|
|
float fSmoothingClampSecs; |
|
|
|
|
bool fClamping; |
|
|
|
|
|
|
|
|
|
hsWide* FactorInTimeZero(hsWide* ticks) const; |
|
|
|
|
|
|
|
|
|
double GetSeconds() const; |
|
|
|
|
double GetMilliSeconds() const; |
|
|
|
|
|
|
|
|
|
hsWide* GetRawTicks(hsWide* ticks) const; |
|
|
|
|
|
|
|
|
|
double RawTicksToDSeconds(const hsWide& ticks); |
|
|
|
|
hsWide DSecondsToRawTicks(double secs); |
|
|
|
|
|
|
|
|
|
float GetDelSysSeconds() const { return fDelSysSeconds; } |
|
|
|
|
double GetSysSeconds() const { return fSysSeconds; } |
|
|
|
|
double IncSysSeconds(); |
|
|
|
|
double fSmoothBuff[kSmoothBuffLen]; |
|
|
|
|
int fCurrSlot; |
|
|
|
|
|
|
|
|
|
float fSysTimeScale; |
|
|
|
|
double fRealSeconds; |
|
|
|
|
double fSysSeconds; |
|
|
|
|
float fDelSysSeconds; |
|
|
|
|
float fFrameTimeInc; |
|
|
|
|
bool fRunningFrameTime; |
|
|
|
|
float fTimeClampSecs; |
|
|
|
|
float fSmoothingClampSecs; |
|
|
|
|
bool fClamping; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T = double> |
|
|
|
|
T GetSeconds() const |
|
|
|
|
{ |
|
|
|
|
typedef std::chrono::duration<T> duration_type; |
|
|
|
|
Duration d = GetRawTicks(); |
|
|
|
|
return std::chrono::duration_cast<duration_type>(d).count(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<typename T = double> |
|
|
|
|
T GetSeconds(uint64_t ticks) const |
|
|
|
|
{ |
|
|
|
|
typedef std::chrono::duration<T> duration_type; |
|
|
|
|
Duration d(ticks); |
|
|
|
|
return std::chrono::duration_cast<duration_type>(d).count(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<typename T = double> |
|
|
|
|
T GetMilliSeconds() const |
|
|
|
|
{ |
|
|
|
|
typedef std::chrono::duration<T, std::milli> duration_type; |
|
|
|
|
Duration d = GetRawTicks(); |
|
|
|
|
return std::chrono::duration_cast<duration_type>(d).count(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template<typename T = double> |
|
|
|
|
T GetMilliSeconds(uint64_t ticks) const |
|
|
|
|
{ |
|
|
|
|
typedef std::chrono::duration<T, std::milli> duration_type; |
|
|
|
|
Duration d(ticks); |
|
|
|
|
return std::chrono::duration_cast<duration_type>(d).count(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint64_t GetTicks() const; |
|
|
|
|
|
|
|
|
|
float GetDelSysSeconds() const { return fDelSysSeconds; } |
|
|
|
|
double GetSysSeconds() const { return fSysSeconds; } |
|
|
|
|
double IncSysSeconds(); |
|
|
|
|
|
|
|
|
|
void SetRealTime(bool realTime); |
|
|
|
|
bool IsRealTime() const { return !fRunningFrameTime; } |
|
|
|
|
|
|
|
|
|
void SetFrameTimeInc(float inc) { fFrameTimeInc = inc; } |
|
|
|
|
|
|
|
|
|
void SetTimeScale(float s) { fSysTimeScale = s; } |
|
|
|
|
float GetTimeScale() const { return fSysTimeScale; } |
|
|
|
|
|
|
|
|
|
void SetTimeClamp(float secs) { fTimeClampSecs = secs; } |
|
|
|
|
void SetSmoothingCap(float secs) { fSmoothingClampSecs = secs; } |
|
|
|
|
float GetTimeClamp() const { return fTimeClampSecs; } |
|
|
|
|
bool IsClamping() const { return fClamping; } |
|
|
|
|
|
|
|
|
|
void SetRealTime(bool realTime); |
|
|
|
|
bool IsRealTime() const { return !fRunningFrameTime; } |
|
|
|
|
friend class hsTimer; |
|
|
|
|
|
|
|
|
|
void SetFrameTimeInc(float inc) { fFrameTimeInc = inc; } |
|
|
|
|
|
|
|
|
|
void SetTimeScale(float s) { fSysTimeScale = s; } |
|
|
|
|
float GetTimeScale() const { return fSysTimeScale; } |
|
|
|
|
private: |
|
|
|
|
Duration GetRawTicks() const; |
|
|
|
|
|
|
|
|
|
void SetTimeClamp(float secs) { fTimeClampSecs = secs; } |
|
|
|
|
void SetSmoothingCap(float secs) { fSmoothingClampSecs = secs; } |
|
|
|
|
float GetTimeClamp() const { return fTimeClampSecs; } |
|
|
|
|
bool IsClamping() const { return fClamping; } |
|
|
|
|
|
|
|
|
|
friend class hsTimer; |
|
|
|
|
public: |
|
|
|
|
plTimerShare(); |
|
|
|
|
~plTimerShare(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class hsTimer
|
|
|
|
|
|
|
|
|
|
class hsTimer |
|
|
|
|
{ |
|
|
|
|
protected: |
|
|
|
|
static const double fPrecTicksPerSec; |
|
|
|
|
static const hsWide fRawBase; |
|
|
|
|
|
|
|
|
|
static hsWide IInitRawBase(); |
|
|
|
|
|
|
|
|
|
static plTimerShare* fTimer; |
|
|
|
|
public: |
|
|
|
|
|
|
|
|
|
static bool VerifyRawBase() { return fRawBase == IInitRawBase(); } |
|
|
|
|
static const hsWide& GetRawBase() { return fRawBase; } |
|
|
|
|
|
|
|
|
|
static hsWide* GetRawTicks(hsWide* ticks) { return fTimer->GetRawTicks(ticks); } |
|
|
|
|
public: |
|
|
|
|
template<typename T = double> |
|
|
|
|
static T GetSeconds() { return fTimer->GetSeconds<T>(); } |
|
|
|
|
|
|
|
|
|
static double GetSeconds() { return fTimer->GetSeconds(); } |
|
|
|
|
static double GetMilliSeconds() { return fTimer->GetMilliSeconds(); } |
|
|
|
|
template<typename T = double> |
|
|
|
|
static T GetSeconds(uint64_t ticks) { return fTimer->GetSeconds<T>(ticks); } |
|
|
|
|
|
|
|
|
|
static double RawTicksToDSeconds(const hsWide& ticks) { return fTimer->RawTicksToDSeconds(ticks); } |
|
|
|
|
static hsWide DSecondsToRawTicks(double secs) { return fTimer->DSecondsToRawTicks(secs); } |
|
|
|
|
template<typename T = double> |
|
|
|
|
static T GetMilliSeconds() { return fTimer->GetMilliSeconds<T>(); } |
|
|
|
|
|
|
|
|
|
static float GetDelSysSeconds() { return fTimer->GetDelSysSeconds(); } |
|
|
|
|
static double GetSysSeconds() { return fTimer->GetSysSeconds(); } |
|
|
|
|
template<typename T = double> |
|
|
|
|
static T GetMilliSeconds(uint64_t ticks) { return fTimer->GetMilliSeconds<T>(ticks); } |
|
|
|
|
|
|
|
|
|
static double IncSysSeconds() { return fTimer->IncSysSeconds(); } |
|
|
|
|
static uint64_t GetTicks() { return fTimer->GetTicks(); } |
|
|
|
|
|
|
|
|
|
static void SetRealTime(bool realTime) { fTimer->SetRealTime(realTime); } |
|
|
|
|
static bool IsRealTime() { return fTimer->IsRealTime(); } |
|
|
|
|
static float GetDelSysSeconds() { return fTimer->GetDelSysSeconds(); } |
|
|
|
|
static double GetSysSeconds() { return fTimer->GetSysSeconds(); } |
|
|
|
|
|
|
|
|
|
static void SetFrameTimeInc(float inc) { fTimer->SetFrameTimeInc(inc); } |
|
|
|
|
static double IncSysSeconds() { return fTimer->IncSysSeconds(); } |
|
|
|
|
|
|
|
|
|
static void SetTimeScale(float s) { fTimer->SetTimeScale(s); } |
|
|
|
|
static float GetTimeScale() { return fTimer->GetTimeScale(); } |
|
|
|
|
static void SetRealTime(bool realTime) { fTimer->SetRealTime(realTime); } |
|
|
|
|
static bool IsRealTime() { return fTimer->IsRealTime(); } |
|
|
|
|
|
|
|
|
|
static void SetTimeClamp(float secs) { fTimer->SetTimeClamp(secs); } |
|
|
|
|
static void SetTimeSmoothingClamp(float secs) { fTimer->SetSmoothingCap(secs); } |
|
|
|
|
static float GetTimeClamp() { return fTimer->GetTimeClamp(); } |
|
|
|
|
static bool IsClamping() { return fTimer->IsClamping(); } |
|
|
|
|
static void SetFrameTimeInc(float inc) { fTimer->SetFrameTimeInc(inc); } |
|
|
|
|
|
|
|
|
|
///////////////////////////
|
|
|
|
|
// Precision timer routines - these are stateless and implemented as statics.
|
|
|
|
|
///////////////////////////
|
|
|
|
|
static uint32_t GetPrecTickCount(); |
|
|
|
|
static double GetPrecTicksPerSec(); |
|
|
|
|
static uint32_t PrecSecsToTicks(float secs); |
|
|
|
|
static double PrecTicksToSecs(uint32_t ticks); |
|
|
|
|
static double PrecTicksToHz(uint32_t ticks); |
|
|
|
|
static void SetTimeScale(float s) { fTimer->SetTimeScale(s); } |
|
|
|
|
static float GetTimeScale() { return fTimer->GetTimeScale(); } |
|
|
|
|
|
|
|
|
|
// If you need to time something longer than 20 seconds, use this instead of
|
|
|
|
|
// the precision timer. It works the same, it just gives you full resolution.
|
|
|
|
|
static uint64_t GetFullTickCount(); |
|
|
|
|
static float FullTicksToMs(uint64_t ticks); |
|
|
|
|
static void SetTimeClamp(float secs) { fTimer->SetTimeClamp(secs); } |
|
|
|
|
static void SetTimeSmoothingClamp(float secs) { fTimer->SetSmoothingCap(secs); } |
|
|
|
|
static float GetTimeClamp() { return fTimer->GetTimeClamp(); } |
|
|
|
|
static bool IsClamping() { return fTimer->IsClamping(); } |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Pass GetTheTimer() into other process space, and then call SetTheTimer() on it.
|
|
|
|
|