diff --git a/core/platform/linux/CCApplication-linux.cpp b/core/platform/linux/CCApplication-linux.cpp index 8127fc12c7..c6a5f780be 100644 --- a/core/platform/linux/CCApplication-linux.cpp +++ b/core/platform/linux/CCApplication-linux.cpp @@ -27,6 +27,7 @@ THE SOFTWARE. #include #include #include +#include #include "base/CCDirector.h" #include "base/ccUtils.h" #include "platform/CCFileUtils.h" @@ -36,17 +37,7 @@ NS_CC_BEGIN // sharedApplication pointer Application* Application::sm_pSharedApplication = nullptr; -static int32_t getCurrentMillSecond() -{ - int32_t lLastTime; - struct timeval stCurrentTime; - - gettimeofday(&stCurrentTime, NULL); - lLastTime = stCurrentTime.tv_sec * 1000 + stCurrentTime.tv_usec * 0.001; // milliseconds - return lLastTime; -} - -Application::Application() : _animationInterval(1.0f / 60.0f * 1000.0f) +Application::Application() : _animationInterval(16666667) { CC_ASSERT(!sm_pSharedApplication); sm_pSharedApplication = this; @@ -67,8 +58,7 @@ int Application::run() return 0; } - int32_t lastTime = 0L; - int32_t curTime = 0L; + std::chrono::steady_clock::time_point lastTime{}; auto director = Director::getInstance(); auto glview = director->getOpenGLView(); @@ -78,15 +68,20 @@ int Application::run() while (!glview->windowShouldClose()) { - lastTime = getCurrentMillSecond(); + lastTime = std::chrono::steady_clock::now(); director->mainLoop(); glview->pollEvents(); - curTime = getCurrentMillSecond(); - if (curTime - lastTime < _animationInterval) + auto interval = std::chrono::steady_clock::now() - lastTime; + if (interval < _animationInterval) { - usleep((_animationInterval - curTime + lastTime) * 1000); + auto waitDuration = _animationInterval - interval; + std::this_thread::sleep_for(waitDuration); + } + else + { + std::this_thread::yield(); } } /* Only work on Desktop @@ -107,7 +102,8 @@ int Application::run() void Application::setAnimationInterval(float interval) { // TODO do something else - _animationInterval = interval * 1000.0f; + _animationInterval = + std::chrono::nanoseconds{static_cast(std::nano::den * interval)}; } void Application::setResourceRootPath(std::string_view rootResDir) diff --git a/core/platform/linux/CCApplication-linux.h b/core/platform/linux/CCApplication-linux.h index 73708edcc7..cc71d8efe2 100644 --- a/core/platform/linux/CCApplication-linux.h +++ b/core/platform/linux/CCApplication-linux.h @@ -28,6 +28,7 @@ THE SOFTWARE. #include "platform/CCCommon.h" #include "platform/CCApplicationProtocol.h" #include +#include NS_CC_BEGIN class Rect; @@ -104,7 +105,7 @@ public: virtual Platform getTargetPlatform() override; protected: - int32_t _animationInterval; // micro second + std::chrono::nanoseconds _animationInterval; // nano seconds std::string _resourceRootPath; static Application* sm_pSharedApplication; diff --git a/core/platform/mac/CCApplication-mac.h b/core/platform/mac/CCApplication-mac.h index bea95c4a8a..f7ca888228 100644 --- a/core/platform/mac/CCApplication-mac.h +++ b/core/platform/mac/CCApplication-mac.h @@ -28,6 +28,7 @@ THE SOFTWARE. #include "platform/CCCommon.h" #include "platform/CCApplicationProtocol.h" #include +#include NS_CC_BEGIN @@ -99,7 +100,7 @@ public: protected: static Application* sm_pSharedApplication; - int32_t _animationInterval; // micro second + std::chrono::nanoseconds _animationInterval; // nano second std::string _resourceRootPath; std::string _startupScriptFilename; }; diff --git a/core/platform/mac/CCApplication-mac.mm b/core/platform/mac/CCApplication-mac.mm index d981f2c22b..eae12fea1b 100644 --- a/core/platform/mac/CCApplication-mac.mm +++ b/core/platform/mac/CCApplication-mac.mm @@ -26,6 +26,7 @@ THE SOFTWARE. #import #import #include +#include #include "platform/CCApplication.h" #include "platform/CCFileUtils.h" @@ -36,19 +37,9 @@ THE SOFTWARE. NS_CC_BEGIN -static int32_t getCurrentMillSecond() -{ - int32_t lLastTime = 0; - struct timeval stCurrentTime; - - gettimeofday(&stCurrentTime, NULL); - lLastTime = stCurrentTime.tv_sec * 1000 + stCurrentTime.tv_usec * 0.001; // milliseconds - return lLastTime; -} - Application* Application::sm_pSharedApplication = nullptr; -Application::Application() : _animationInterval(1.0f / 60.0f * 1000.0f) +Application::Application() : _animationInterval(16666667) { CCASSERT(!sm_pSharedApplication, "sm_pSharedApplication already exist"); sm_pSharedApplication = this; @@ -68,8 +59,7 @@ int Application::run() return 1; } - int32_t lastTime = 0L; - int32_t curTime = 0L; + std::chrono::steady_clock::time_point lastTime{}; auto director = Director::getInstance(); auto glview = director->getOpenGLView(); @@ -79,15 +69,20 @@ int Application::run() while (!glview->windowShouldClose()) { - lastTime = getCurrentMillSecond(); + lastTime = std::chrono::steady_clock::now(); director->mainLoop(); glview->pollEvents(); - curTime = getCurrentMillSecond(); - if (curTime - lastTime < _animationInterval) + auto interval = std::chrono::steady_clock::now() - lastTime; + if (interval < _animationInterval) { - usleep(static_cast((_animationInterval - curTime + lastTime) * 1000)); + auto waitDuration = _animationInterval - interval; + std::this_thread::sleep_for(waitDuration); + } + else + { + std::this_thread::yield(); } } @@ -109,7 +104,8 @@ int Application::run() void Application::setAnimationInterval(float interval) { - _animationInterval = interval * 1000.0f; + _animationInterval = + std::chrono::nanoseconds{static_cast(std::nano::den * interval)}; } Application::Platform Application::getTargetPlatform()