Magic_Game/core/Base/Time.cpp

87 lines
1.7 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dbase.h"
2018-04-24 20:22:41 +08:00
#include <thread>
#include <chrono>
using namespace std::chrono;
// <20><>Ϸ<EFBFBD><CFB7>ʼʱ<CABC><CAB1>
static steady_clock::time_point s_tStart;
// <20><>ǰʱ<C7B0><CAB1>
static steady_clock::time_point s_tNow;
// <20><>һ֡ˢ<D6A1><CBA2>ʱ<EFBFBD><CAB1>
static steady_clock::time_point s_tFixedUpdate;
// <20><>һ<EFBFBD>θ<EFBFBD><CEB8><EFBFBD>ʱ<EFBFBD><CAB1>
static steady_clock::time_point s_tLastUpdate;
// ÿһ֡<D2BB><D6A1><EFBFBD><EFBFBD>
static milliseconds s_tExceptedInvertal;
2018-01-30 16:45:38 +08:00
// <20><>һ֡<D2BB>뵱ǰ֡<C7B0><D6A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-04-24 13:28:21 +08:00
static unsigned int s_nInterval = 0;
2018-01-30 16:45:38 +08:00
// <20><>Ϸ<EFBFBD><CFB7>ʼʱ<CABC><CAB1>
2018-04-24 13:28:21 +08:00
static unsigned int s_nTotalTime = 0;
2018-01-30 16:45:38 +08:00
2018-02-27 21:07:43 +08:00
double e2d::Time::getTotalTime()
2018-01-30 16:45:38 +08:00
{
2018-04-24 13:28:21 +08:00
return s_nTotalTime / 1000.0;
2018-01-30 16:45:38 +08:00
}
2018-04-24 13:28:21 +08:00
unsigned int e2d::Time::getTotalTimeMilliseconds()
{
return s_nTotalTime;
}
double e2d::Time::getDeltaTime()
{
return s_nInterval / 1000.0;
}
unsigned int e2d::Time::getDeltaTimeMilliseconds()
2018-01-30 16:45:38 +08:00
{
return s_nInterval;
}
bool e2d::Time::__init()
2018-01-30 16:45:38 +08:00
{
2018-02-04 21:24:27 +08:00
s_tStart = s_tLastUpdate = s_tFixedUpdate = s_tNow = steady_clock::now();
2018-04-17 11:41:33 +08:00
s_tExceptedInvertal = milliseconds(15);
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
void e2d::Time::__uninit()
2018-01-30 16:45:38 +08:00
{
}
bool e2d::Time::__isReady()
2018-01-30 16:45:38 +08:00
{
2018-02-04 21:24:27 +08:00
return s_tExceptedInvertal < duration_cast<milliseconds>(s_tNow - s_tFixedUpdate);
2018-01-30 16:45:38 +08:00
}
void e2d::Time::__updateNow()
2018-01-30 16:45:38 +08:00
{
2018-02-04 21:24:27 +08:00
// ˢ<><CBA2>ʱ<EFBFBD><CAB1>
s_tNow = steady_clock::now();
2018-01-30 16:45:38 +08:00
}
void e2d::Time::__updateLast()
2018-01-30 16:45:38 +08:00
{
2018-02-04 21:24:27 +08:00
s_tFixedUpdate += s_tExceptedInvertal;
s_tLastUpdate = s_tNow;
s_tNow = steady_clock::now();
2018-04-24 13:28:21 +08:00
s_nInterval = static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tLastUpdate).count());
s_nTotalTime = static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tStart).count());
2018-01-30 16:45:38 +08:00
}
void e2d::Time::__sleep()
2018-01-30 16:45:38 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
2018-02-04 21:24:27 +08:00
int nWaitMS = 16 - static_cast<int>(duration_cast<milliseconds>(s_tNow - s_tFixedUpdate).count());
2018-01-30 16:45:38 +08:00
if (nWaitMS > 1)
{
2018-02-04 21:24:27 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>̣߳<DFB3><CCA3>ͷ<EFBFBD> CPU ռ<><D5BC>
std::this_thread::sleep_for(milliseconds(nWaitMS));
2018-01-30 16:45:38 +08:00
}
2018-04-01 23:08:11 +08:00
}