mirror of https://github.com/axmolengine/axmol.git
Uses std::chrono::steay_clock for calculating dt.
This commit is contained in:
parent
b5de8ba0ea
commit
ddb25ca563
|
@ -127,7 +127,7 @@ bool Director::init(void)
|
||||||
_frameRate = 0.0f;
|
_frameRate = 0.0f;
|
||||||
_FPSLabel = _drawnBatchesLabel = _drawnVerticesLabel = nullptr;
|
_FPSLabel = _drawnBatchesLabel = _drawnVerticesLabel = nullptr;
|
||||||
_totalFrames = 0;
|
_totalFrames = 0;
|
||||||
_lastUpdate = new (std::nothrow) struct timeval;
|
_lastUpdate = std::chrono::steady_clock::now();
|
||||||
_secondsPerFrame = 1.0f;
|
_secondsPerFrame = 1.0f;
|
||||||
|
|
||||||
// paused ?
|
// paused ?
|
||||||
|
@ -204,9 +204,6 @@ Director::~Director(void)
|
||||||
|
|
||||||
CC_SAFE_RELEASE(_eventDispatcher);
|
CC_SAFE_RELEASE(_eventDispatcher);
|
||||||
|
|
||||||
// delete _lastUpdate
|
|
||||||
CC_SAFE_DELETE(_lastUpdate);
|
|
||||||
|
|
||||||
Configuration::destroyInstance();
|
Configuration::destroyInstance();
|
||||||
|
|
||||||
s_SharedDirector = nullptr;
|
s_SharedDirector = nullptr;
|
||||||
|
@ -343,14 +340,7 @@ void Director::drawScene()
|
||||||
|
|
||||||
void Director::calculateDeltaTime()
|
void Director::calculateDeltaTime()
|
||||||
{
|
{
|
||||||
struct timeval now;
|
auto now = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
if (gettimeofday(&now, nullptr) != 0)
|
|
||||||
{
|
|
||||||
CCLOG("error in gettimeofday");
|
|
||||||
_deltaTime = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// new delta time. Re-fixed issue #1277
|
// new delta time. Re-fixed issue #1277
|
||||||
if (_nextDeltaTimeZero)
|
if (_nextDeltaTimeZero)
|
||||||
|
@ -360,7 +350,7 @@ void Director::calculateDeltaTime()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_deltaTime = (now.tv_sec - _lastUpdate->tv_sec) + (now.tv_usec - _lastUpdate->tv_usec) / 1000000.0f;
|
_deltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - _lastUpdate).count() / 1000000.0f;
|
||||||
_deltaTime = MAX(0, _deltaTime);
|
_deltaTime = MAX(0, _deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,7 +362,7 @@ void Director::calculateDeltaTime()
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
*_lastUpdate = now;
|
_lastUpdate = now;
|
||||||
}
|
}
|
||||||
float Director::getDeltaTime() const
|
float Director::getDeltaTime() const
|
||||||
{
|
{
|
||||||
|
@ -1224,10 +1214,9 @@ void Director::calculateMPF()
|
||||||
static float prevSecondsPerFrame = 0;
|
static float prevSecondsPerFrame = 0;
|
||||||
static const float MPF_FILTER = 0.10f;
|
static const float MPF_FILTER = 0.10f;
|
||||||
|
|
||||||
struct timeval now;
|
auto now = std::chrono::steady_clock::now();
|
||||||
gettimeofday(&now, nullptr);
|
|
||||||
|
|
||||||
_secondsPerFrame = (now.tv_sec - _lastUpdate->tv_sec) + (now.tv_usec - _lastUpdate->tv_usec) / 1000000.0f;
|
_secondsPerFrame = std::chrono::duration_cast<std::chrono::microseconds>(now - _lastUpdate).count() / 1000000.0f;
|
||||||
|
|
||||||
_secondsPerFrame = _secondsPerFrame * MPF_FILTER + (1-MPF_FILTER) * prevSecondsPerFrame;
|
_secondsPerFrame = _secondsPerFrame * MPF_FILTER + (1-MPF_FILTER) * prevSecondsPerFrame;
|
||||||
prevSecondsPerFrame = _secondsPerFrame;
|
prevSecondsPerFrame = _secondsPerFrame;
|
||||||
|
@ -1378,10 +1367,7 @@ void Director::setEventDispatcher(EventDispatcher* dispatcher)
|
||||||
// so we now only support DisplayLinkDirector
|
// so we now only support DisplayLinkDirector
|
||||||
void DisplayLinkDirector::startAnimation()
|
void DisplayLinkDirector::startAnimation()
|
||||||
{
|
{
|
||||||
if (gettimeofday(_lastUpdate, nullptr) != 0)
|
_lastUpdate = std::chrono::steady_clock::now();
|
||||||
{
|
|
||||||
CCLOG("cocos2d: DisplayLinkDirector: Error on gettimeofday");
|
|
||||||
}
|
|
||||||
|
|
||||||
_invalid = false;
|
_invalid = false;
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ THE SOFTWARE.
|
||||||
|
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#include "platform/CCPlatformMacros.h"
|
#include "platform/CCPlatformMacros.h"
|
||||||
#include "base/CCRef.h"
|
#include "base/CCRef.h"
|
||||||
|
@ -589,7 +590,7 @@ protected:
|
||||||
Vector<Scene*> _scenesStack;
|
Vector<Scene*> _scenesStack;
|
||||||
|
|
||||||
/* last time the main loop was updated */
|
/* last time the main loop was updated */
|
||||||
struct timeval *_lastUpdate;
|
std::chrono::steady_clock::time_point _lastUpdate;
|
||||||
|
|
||||||
/* whether or not the next delta time will be zero */
|
/* whether or not the next delta time will be zero */
|
||||||
bool _nextDeltaTimeZero;
|
bool _nextDeltaTimeZero;
|
||||||
|
|
Loading…
Reference in New Issue