您的位置:首页 > 其它

获取间隔时间

2014-10-12 17:44 169 查看
头文件 DeltaTime.h

namespace CoreTools
{
CoreToolsExport int64_t GetTimeInMicroseconds();
CoreToolsExport double GetTimeInSeconds();
}

源文件 DeltaTime.cpp

#include "DeltaTime.h"

#ifdef __APPLE__
#include <sys/time.h>
namespace CoreTools
{
timeval g_Initial;
bool g_InitializedTime = false;
}
#else
#include <sys/timeb.h>
namespace CoreTools
{
long g_InitialSecond = 0;
long g_InitialMicrosecond = 0;
bool g_InitializedTime = false;
}
#endif // __APPLE__

int64_t CoreTools
::GetTimeInMicroseconds ()
{
#ifdef __APPLE__
if (!g_InitializedTime)
{
g_InitializedTime = true;
gettimeofday(&g_Initial, 0);
}

struct timeval currentTime;
gettimeofday(¤tTime, 0);

struct timeval deltaTime;
timersub(¤tTime, &g_Initial, &deltaTime);

return 1000000 * static_cast<int64_t>(deltaTime.tv_sec) + deltaTime.tv_usec;
#else
struct timeb currentTime;

if (!g_InitializedTime)
{
g_InitializedTime = true;
ftime(¤tTime);
g_InitialSecond = static_cast<long>(currentTime.time);
g_InitialMicrosecond = 1000 * currentTime.millitm;
}

ftime(¤tTime);
long currentSecond = static_cast<long>(currentTime.time);
long currentMicrosecond = 1000 * currentTime.millitm;
long deltaSecond = currentSecond - g_InitialSecond;
long deltaMicrosecond = currentMicrosecond - g_InitialMicrosecond;
if (deltaMicrosecond < 0)
{
deltaMicrosecond += 1000000;
--deltaSecond;
}

return 1000000 * static_cast<int64_t>(deltaSecond) + deltaMicrosecond;
#endif // __APPLE__
}

double CoreTools
::GetTimeInSeconds()
{
int64_t microseconds = GetTimeInMicroseconds();
return 1e-06 * microseconds;
}

头文件CustomTime.h

namespace CoreTools
{
class CoreToolsExport CustomTime
{
public:
typedef CustomTime ClassType;

public:
CustomTime();

#ifdef _DEBUG
bool IsValid() const;
#endif // _DEBUG

void ResetCurrentTime();
double GetElapsedTime();
double GetNowTime();

private:
void MarkTimeThisTick();

private:
double m_StartTime;
double m_CurrentTime;
double m_TimeLastTick;
};
}

源文件CustomTime.cpp
#include "CustomTime.h"
#include "DeltaTime.h"
#include "CoreTools/ClassInvariant/CoreToolsClassInvariant.h"
#include "CoreTools/Macro/ClassInvariantMacro.h"

CoreTools::CustomTime::CustomTime()
:m_StartTime(GetTimeInSeconds()),
m_CurrentTime(0.0),m_TimeLastTick(0.0)
{
SELF_CLASS_IS_VALID;
}

#ifdef _DEBUG
bool CoreTools::CustomTime
::IsValid() const
{
if(-0.0001 <= m_StartTime &&
-0.0001 <= m_CurrentTime &&
-0.0001 <= m_TimeLastTick)
return true;
else
return false;
}
#endif // _DEBUG

void CoreTools::CustomTime
::ResetCurrentTime()
{
CLASS_IS_VALID;

m_StartTime = GetTimeInSeconds();
m_CurrentTime = 0.0;
m_TimeLastTick = 0.0;
}

double CoreTools::CustomTime
::GetElapsedTime()
{
CLASS_IS_VALID;

MarkTimeThisTick();

return m_TimeLastTick;
}

double CoreTools::CustomTime
::GetNowTime()
{
CLASS_IS_VALID;

MarkTimeThisTick();

return m_CurrentTime;
}

// private
void CoreTools::CustomTime
::MarkTimeThisTick()
{
double newTime = GetTimeInSeconds() - m_StartTime;

m_TimeLastTick = newTime - m_CurrentTime;
m_CurrentTime = newTime;

if(m_TimeLastTick <= 0.0)
m_TimeLastTick = 0.0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: