Magic_Game/core/Base/Time.cpp

71 lines
1.3 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>
2018-07-03 18:42:00 +08:00
2018-04-24 20:22:41 +08:00
using namespace std::chrono;
// <20><>Ϸ<EFBFBD><CFB7>ʼʱ<CABC><CAB1>
2018-07-03 18:42:00 +08:00
steady_clock::time_point e2d::Time::_start;
2018-04-24 20:22:41 +08:00
// <20><>ǰʱ<C7B0><CAB1>
2018-07-03 18:42:00 +08:00
steady_clock::time_point e2d::Time::_now;
2018-04-24 20:22:41 +08:00
// <20><>һ֡ˢ<D6A1><CBA2>ʱ<EFBFBD><CAB1>
2018-07-03 18:42:00 +08:00
steady_clock::time_point e2d::Time::_last;
2018-05-31 20:28:50 +08:00
// <20>̶<EFBFBD><CCB6><EFBFBD>ˢ<EFBFBD><CBA2>ʱ<EFBFBD><CAB1>
2018-07-03 18:42:00 +08:00
steady_clock::time_point e2d::Time::_fixedLast;
2018-04-24 20:22:41 +08:00
// ÿһ֡<D2BB><D6A1><EFBFBD><EFBFBD>
2018-07-03 18:42:00 +08:00
milliseconds e2d::Time::_interval;
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-07-03 18:42:00 +08:00
return duration_cast<microseconds>(_now - _start).count() / 1000.0 / 1000.0;
2018-04-24 13:28:21 +08:00
}
double e2d::Time::getDeltaTime()
{
2018-07-03 18:42:00 +08:00
return duration_cast<microseconds>(_now - _last).count() / 1000.0 / 1000.0;
2018-01-30 16:45:38 +08:00
}
bool e2d::Time::__init()
2018-01-30 16:45:38 +08:00
{
2018-07-03 18:42:00 +08:00
_start = _fixedLast = _last = _now = steady_clock::now();
_interval = milliseconds(15);
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
bool e2d::Time::__isReady()
2018-01-30 16:45:38 +08:00
{
2018-07-03 18:42:00 +08:00
return _interval < duration_cast<milliseconds>(_now - _fixedLast);
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>
2018-07-03 18:42:00 +08:00
_now = steady_clock::now();
2018-01-30 16:45:38 +08:00
}
void e2d::Time::__updateLast()
2018-01-30 16:45:38 +08:00
{
2018-07-03 18:42:00 +08:00
_fixedLast += _interval;
2018-02-04 21:24:27 +08:00
2018-07-03 18:42:00 +08:00
_last = _now;
_now = steady_clock::now();
2018-01-30 16:45:38 +08:00
}
void e2d::Time::__reset()
{
2018-07-03 18:42:00 +08:00
_last = _fixedLast = _now = steady_clock::now();
}
void e2d::Time::__sleep()
2018-01-30 16:45:38 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
2018-07-03 18:42:00 +08:00
int nWaitMS = 16 - static_cast<int>(duration_cast<milliseconds>(_now - _fixedLast).count());
2018-02-04 21:24:27 +08:00
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
}