1
0

Created new type cTickTime and rewrote cWorld::TickThread to use it

This commit is contained in:
Tycho 2015-01-11 01:54:18 +00:00
parent 52d307e3b0
commit 4f75b94c99
2 changed files with 8 additions and 6 deletions

View File

@ -419,6 +419,8 @@ std::unique_ptr<T> make_unique(Args&&... args)
return std::unique_ptr<T>(new T(args...));
}
// a tick is 50 ms
using cTickTime = std::chrono::duration<int, std::ratio_multiply<std::chrono::milliseconds::period, std::ratio<50>>>;
#ifndef TOLUA_TEMPLATE_BIND
#define TOLUA_TEMPLATE_BIND(x)
@ -436,3 +438,4 @@ std::unique_ptr<T> make_unique(Args&&... args)

View File

@ -233,8 +233,7 @@ cWorld::cTickThread::cTickThread(cWorld & a_World) :
void cWorld::cTickThread::Execute(void)
{
auto LastTime = std::chrono::steady_clock::now();
static const auto msPerTick = std::chrono::milliseconds(50);
auto TickTime = std::chrono::steady_clock::duration(50);
auto TickTime = std::chrono::duration_cast<std::chrono::milliseconds>(cTickTime(1));
while (!m_ShouldTerminate)
{
@ -242,12 +241,12 @@ void cWorld::cTickThread::Execute(void)
auto msec = std::chrono::duration_cast<std::chrono::milliseconds>(NowTime - LastTime).count();
auto LastTickMsec = std::chrono::duration_cast<std::chrono::duration<int>>(TickTime).count();
m_World.Tick(static_cast<float>(msec), LastTickMsec);
TickTime = std::chrono::steady_clock::now() - NowTime;
TickTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - NowTime);
if (TickTime < msPerTick)
if (TickTime < cTickTime(1))
{
// Stretch tick time until it's at least msPerTick
std::this_thread::sleep_for(msPerTick - TickTime);
// Stretch tick time until it's at least 1 tick
std::this_thread::sleep_for(cTickTime(1) - TickTime);
}
LastTime = NowTime;