Try fix fps

This commit is contained in:
halx99 2022-07-01 01:00:18 +08:00
parent 1d74df858b
commit 5f02d2a656
4 changed files with 32 additions and 38 deletions

View File

@ -27,6 +27,7 @@ THE SOFTWARE.
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <string> #include <string>
#include <thread>
#include "base/CCDirector.h" #include "base/CCDirector.h"
#include "base/ccUtils.h" #include "base/ccUtils.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
@ -36,17 +37,7 @@ NS_CC_BEGIN
// sharedApplication pointer // sharedApplication pointer
Application* Application::sm_pSharedApplication = nullptr; Application* Application::sm_pSharedApplication = nullptr;
static int32_t getCurrentMillSecond() Application::Application() : _animationInterval(16666667)
{
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)
{ {
CC_ASSERT(!sm_pSharedApplication); CC_ASSERT(!sm_pSharedApplication);
sm_pSharedApplication = this; sm_pSharedApplication = this;
@ -67,8 +58,7 @@ int Application::run()
return 0; return 0;
} }
int32_t lastTime = 0L; std::chrono::steady_clock::time_point lastTime{};
int32_t curTime = 0L;
auto director = Director::getInstance(); auto director = Director::getInstance();
auto glview = director->getOpenGLView(); auto glview = director->getOpenGLView();
@ -78,15 +68,20 @@ int Application::run()
while (!glview->windowShouldClose()) while (!glview->windowShouldClose())
{ {
lastTime = getCurrentMillSecond(); lastTime = std::chrono::steady_clock::now();
director->mainLoop(); director->mainLoop();
glview->pollEvents(); glview->pollEvents();
curTime = getCurrentMillSecond(); auto interval = std::chrono::steady_clock::now() - lastTime;
if (curTime - lastTime < _animationInterval) 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 /* Only work on Desktop
@ -107,7 +102,8 @@ int Application::run()
void Application::setAnimationInterval(float interval) void Application::setAnimationInterval(float interval)
{ {
// TODO do something else // TODO do something else
_animationInterval = interval * 1000.0f; _animationInterval =
std::chrono::nanoseconds{static_cast<std::chrono::nanoseconds::rep>(std::nano::den * interval)};
} }
void Application::setResourceRootPath(std::string_view rootResDir) void Application::setResourceRootPath(std::string_view rootResDir)

View File

@ -28,6 +28,7 @@ THE SOFTWARE.
#include "platform/CCCommon.h" #include "platform/CCCommon.h"
#include "platform/CCApplicationProtocol.h" #include "platform/CCApplicationProtocol.h"
#include <string> #include <string>
#include <chrono>
NS_CC_BEGIN NS_CC_BEGIN
class Rect; class Rect;
@ -104,7 +105,7 @@ public:
virtual Platform getTargetPlatform() override; virtual Platform getTargetPlatform() override;
protected: protected:
int32_t _animationInterval; // micro second std::chrono::nanoseconds _animationInterval; // nano seconds
std::string _resourceRootPath; std::string _resourceRootPath;
static Application* sm_pSharedApplication; static Application* sm_pSharedApplication;

View File

@ -28,6 +28,7 @@ THE SOFTWARE.
#include "platform/CCCommon.h" #include "platform/CCCommon.h"
#include "platform/CCApplicationProtocol.h" #include "platform/CCApplicationProtocol.h"
#include <string> #include <string>
#include <chrono>
NS_CC_BEGIN NS_CC_BEGIN
@ -99,7 +100,7 @@ public:
protected: protected:
static Application* sm_pSharedApplication; static Application* sm_pSharedApplication;
int32_t _animationInterval; // micro second std::chrono::nanoseconds _animationInterval; // nano second
std::string _resourceRootPath; std::string _resourceRootPath;
std::string _startupScriptFilename; std::string _startupScriptFilename;
}; };

View File

@ -26,6 +26,7 @@ THE SOFTWARE.
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import <Metal/Metal.h> #import <Metal/Metal.h>
#include <algorithm> #include <algorithm>
#include <thread>
#include "platform/CCApplication.h" #include "platform/CCApplication.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
@ -36,19 +37,9 @@ THE SOFTWARE.
NS_CC_BEGIN 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::sm_pSharedApplication = nullptr;
Application::Application() : _animationInterval(1.0f / 60.0f * 1000.0f) Application::Application() : _animationInterval(16666667)
{ {
CCASSERT(!sm_pSharedApplication, "sm_pSharedApplication already exist"); CCASSERT(!sm_pSharedApplication, "sm_pSharedApplication already exist");
sm_pSharedApplication = this; sm_pSharedApplication = this;
@ -68,8 +59,7 @@ int Application::run()
return 1; return 1;
} }
int32_t lastTime = 0L; std::chrono::steady_clock::time_point lastTime{};
int32_t curTime = 0L;
auto director = Director::getInstance(); auto director = Director::getInstance();
auto glview = director->getOpenGLView(); auto glview = director->getOpenGLView();
@ -79,15 +69,20 @@ int Application::run()
while (!glview->windowShouldClose()) while (!glview->windowShouldClose())
{ {
lastTime = getCurrentMillSecond(); lastTime = std::chrono::steady_clock::now();
director->mainLoop(); director->mainLoop();
glview->pollEvents(); glview->pollEvents();
curTime = getCurrentMillSecond(); auto interval = std::chrono::steady_clock::now() - lastTime;
if (curTime - lastTime < _animationInterval) if (interval < _animationInterval)
{ {
usleep(static_cast<useconds_t>((_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) void Application::setAnimationInterval(float interval)
{ {
_animationInterval = interval * 1000.0f; _animationInterval =
std::chrono::nanoseconds{static_cast<std::chrono::nanoseconds::rep>(std::nano::den * interval)};
} }
Application::Platform Application::getTargetPlatform() Application::Platform Application::getTargetPlatform()