issue #304 Remove codes for controlling frame rate.

This commit is contained in:
minggo 2011-01-10 15:13:30 +08:00
parent c1b32d7305
commit 7e30615d02
10 changed files with 41 additions and 143 deletions

View File

@ -470,16 +470,7 @@ protected:
void showFPS(void) {} void showFPS(void) {}
#endif // CC_DIRECTOR_FAST_FPS #endif // CC_DIRECTOR_FAST_FPS
/** calculates delta time since last time it was called */ /** calculates delta time since last time it was called */ void calculateDeltaTime();protected:
void calculateDeltaTime();
protected:
// compute frame rate
void computeFrameRate(void);
// compute delta time between computing frame rate
void calculateFramerateDeltaTime(void);
protected:
/* The CCXEGLView, where everything is rendered */ /* The CCXEGLView, where everything is rendered */
CC_GLVIEW *m_pobOpenGLView; CC_GLVIEW *m_pobOpenGLView;
@ -493,7 +484,6 @@ protected:
int m_nFrames; int m_nFrames;
ccTime m_fAccumDt; ccTime m_fAccumDt;
ccTime m_fFrameRate; ccTime m_fFrameRate;
ccTime m_fExpectedFrameRate;
#if CC_DIRECTOR_FAST_FPS #if CC_DIRECTOR_FAST_FPS
CCLabelTTF *m_pFPSLabel; CCLabelTTF *m_pFPSLabel;
#endif #endif
@ -520,11 +510,6 @@ protected:
/* delta time since last tick to main loop */ /* delta time since last tick to main loop */
ccTime m_fDeltaTime; ccTime m_fDeltaTime;
/* last time the director compute frame rate */
struct cc_timeval *m_pLastComputeFrameRate;
/* delta time since last computing frame rate */
ccTime m_fComputeFrameRateDeltaTime;
/* whether or not the next delta time will be zero */ /* whether or not the next delta time will be zero */
bool m_bNextDeltaTimeZero; bool m_bNextDeltaTimeZero;

View File

@ -102,9 +102,6 @@ bool CCDirector::init(void)
m_bDisplayFPS = false; m_bDisplayFPS = false;
m_nFrames = 0; m_nFrames = 0;
m_pszFPS = new char[10]; m_pszFPS = new char[10];
m_fExpectedFrameRate = (ccTime)(1 / m_dAnimationInterval);
m_fComputeFrameRateDeltaTime = 0;
m_pLastComputeFrameRate = new struct cc_timeval();
m_pLastUpdate = new struct cc_timeval(); m_pLastUpdate = new struct cc_timeval();
// paused ? // paused ?
@ -148,9 +145,6 @@ CCDirector::~CCDirector(void)
// delete m_pLastUpdate // delete m_pLastUpdate
CCX_SAFE_DELETE(m_pLastUpdate); CCX_SAFE_DELETE(m_pLastUpdate);
// delete last compute time
CCX_SAFE_DELETE(m_pLastComputeFrameRate);
CCKeypadDispatcher::purgeSharedDispatcher(); CCKeypadDispatcher::purgeSharedDispatcher();
// delete fps string // delete fps string
@ -678,61 +672,22 @@ void CCDirector::mainLoop(void)
// updates the FPS every frame // updates the FPS every frame
void CCDirector::showFPS(void) void CCDirector::showFPS(void)
{ {
sprintf(m_pszFPS, "%.1f", m_fFrameRate); m_nFrames++;
m_pFPSLabel->setString(m_pszFPS); m_fAccumDt += m_fDeltaTime;
m_pFPSLabel->draw();
}
#endif // CC_DIRECTOR_FAST_FPS
void CCDirector::calculateFramerateDeltaTime(void)
{
struct cc_timeval now;
if (CCTime::gettimeofdayCocos2d(&now, NULL) != 0)
{
CCLOG("error in gettimeofday");
m_fComputeFrameRateDeltaTime = 0;
return;
}
m_fComputeFrameRateDeltaTime = (now.tv_sec - m_pLastComputeFrameRate->tv_sec) + (now.tv_usec - m_pLastComputeFrameRate->tv_usec) / 1000000.0f;
m_fComputeFrameRateDeltaTime = MAX(0, m_fComputeFrameRateDeltaTime);
*m_pLastComputeFrameRate = now;
}
void CCDirector::computeFrameRate()
{
static bool bInvoked = true;
// compute delta time
calculateFramerateDeltaTime();
// only add frames if the director really draw the scene
if (bInvoked)
{
m_nFrames++;
}
m_fAccumDt += m_fComputeFrameRateDeltaTime;
if (m_fAccumDt > CC_DIRECTOR_FPS_INTERVAL) if (m_fAccumDt > CC_DIRECTOR_FPS_INTERVAL)
{ {
m_fFrameRate = m_nFrames / m_fAccumDt; m_fFrameRate = m_nFrames / m_fAccumDt;
m_nFrames = 0;
m_fAccumDt = 0;
if (m_fFrameRate > m_fExpectedFrameRate) sprintf(m_pszFPS, "%.1f", m_fFrameRate);
{ m_pFPSLabel->setString(m_pszFPS);
bInvoked = false;
}
else
{
m_nFrames = 0;
m_fAccumDt = 0;
bInvoked = true;
}
} }
m_pFPSLabel->draw();
} }
#endif // CC_DIRECTOR_FAST_FPS
void CCDirector::showProfilers() void CCDirector::showProfilers()
@ -957,23 +912,17 @@ void CCDisplayLinkDirector::startAnimation(void)
} }
m_bInvalid = false; m_bInvalid = false;
m_pobOpenGLView->setAnimationInterval(m_dAnimationInterval);
} }
void CCDisplayLinkDirector::mainLoop(void) void CCDisplayLinkDirector::mainLoop(void)
{ {
if (! m_bInvalid) if (! m_bInvalid)
{ {
// compute frame rate drawScene();
computeFrameRate();
// control frame rate
if (m_fFrameRate <= m_fExpectedFrameRate)
{
drawScene();
// release the objects // release the objects
NSPoolManager::getInstance()->pop(); NSPoolManager::getInstance()->pop();
}
} }
} }
@ -985,7 +934,6 @@ void CCDisplayLinkDirector::stopAnimation(void)
void CCDisplayLinkDirector::setAnimationInterval(double dValue) void CCDisplayLinkDirector::setAnimationInterval(double dValue)
{ {
m_dAnimationInterval = dValue; m_dAnimationInterval = dValue;
m_fExpectedFrameRate = (ccTime)(1 / m_dAnimationInterval);
if (! m_bInvalid) if (! m_bInvalid)
{ {
stopAnimation(); stopAnimation();

View File

@ -49,6 +49,7 @@ public:
void swapBuffers(); void swapBuffers();
bool canSetContentScaleFactor(); bool canSetContentScaleFactor();
void setContentScaleFactor(float contentScaleFactor); void setContentScaleFactor(float contentScaleFactor);
void setAnimationInterval(double interval) {};
private: private:
int m_nWidth; int m_nWidth;

View File

@ -28,6 +28,7 @@
} }
-(void) startMainLoop; -(void) startMainLoop;
-(void) doCaller: (id) sender; -(void) doCaller: (id) sender;
-(void) setAnimationInterval:(double)interval;
+(id) sharedDirectorCaller; +(id) sharedDirectorCaller;
+(void) destroy; +(void) destroy;
@end @end

View File

@ -64,6 +64,11 @@ static id s_sharedDirectorCaller;
[displayLink setFrameInterval: 1]; [displayLink setFrameInterval: 1];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
} }
-(void) setAnimationInterval:(double)interval
{
[displayLink setFrameInterval: interval];
}
-(void) doCaller: (id) sender -(void) doCaller: (id) sender
{ {

View File

@ -44,6 +44,7 @@ public:
bool isOpenGLReady(); bool isOpenGLReady();
bool canSetContentScaleFactor(); bool canSetContentScaleFactor();
void setContentScaleFactor(float contentScaleFactor); void setContentScaleFactor(float contentScaleFactor);
void setAnimationInterval(double interval);
// keep compatible // keep compatible
void release(); void release();

View File

@ -60,6 +60,11 @@ bool CCXEGLView::isOpenGLReady()
{ {
[[EAGLView sharedEGLView] setContentScaleFactor:contentScaleFactor]; [[EAGLView sharedEGLView] setContentScaleFactor:contentScaleFactor];
} }
void CCXEGLView::setAnimationInterval(double interval)
{
[CCDirectorCaller setAnimationInterval: interval]
}
void CCXEGLView::release() void CCXEGLView::release()
{ {

View File

@ -100,9 +100,6 @@ bool CCDirector::init(void)
m_bDisplayFPS = false; m_bDisplayFPS = false;
m_nFrames = 0; m_nFrames = 0;
m_pszFPS = new char[10]; m_pszFPS = new char[10];
m_fExpectedFrameRate = (ccTime)(1 / m_dAnimationInterval);
m_fComputeFrameRateDeltaTime = 0;
m_pLastComputeFrameRate = new struct cc_timeval();
m_pLastUpdate = new struct cc_timeval(); m_pLastUpdate = new struct cc_timeval();
// paused ? // paused ?
@ -655,62 +652,22 @@ void CCDirector::mainLoop(void)
// updates the FPS every frame // updates the FPS every frame
void CCDirector::showFPS(void) void CCDirector::showFPS(void)
{ {
sprintf(m_pszFPS, "%.1f", m_fFrameRate); m_nFrames++;
m_pFPSLabel->setString(m_pszFPS); m_fAccumDt += m_fDeltaTime;
m_pFPSLabel->draw();
}
#endif // CC_DIRECTOR_FAST_FPS
void CCDirector::calculateFramerateDeltaTime(void)
{
struct cc_timeval now;
if (CCTime::gettimeofdayCocos2d(&now, NULL) != 0)
{
CCLOG("error in gettimeofday");
m_fComputeFrameRateDeltaTime = 0;
return;
}
m_fComputeFrameRateDeltaTime = (now.tv_sec - m_pLastComputeFrameRate->tv_sec) + (now.tv_usec - m_pLastComputeFrameRate->tv_usec) / 1000000.0f;
m_fComputeFrameRateDeltaTime = MAX(0, m_fComputeFrameRateDeltaTime);
*m_pLastComputeFrameRate = now;
}
void CCDirector::computeFrameRate()
{
static bool bInvoked = true;
// compute delta time
calculateFramerateDeltaTime();
// only add frames if the director really draw the scene
if (bInvoked)
{
m_nFrames++;
}
m_fAccumDt += m_fComputeFrameRateDeltaTime;
if (m_fAccumDt > CC_DIRECTOR_FPS_INTERVAL) if (m_fAccumDt > CC_DIRECTOR_FPS_INTERVAL)
{ {
m_fFrameRate = m_nFrames / m_fAccumDt; m_fFrameRate = m_nFrames / m_fAccumDt;
m_nFrames = 0;
m_fAccumDt = 0;
if (m_fFrameRate > m_fExpectedFrameRate) sprintf(m_pszFPS, "%.1f", m_fFrameRate);
{ m_pFPSLabel->setString(m_pszFPS);
bInvoked = false;
}
else
{
m_nFrames = 0;
m_fAccumDt = 0;
bInvoked = true;
}
} }
}
m_pFPSLabel->draw();
}
#endif // CC_DIRECTOR_FAST_FPS
void CCDirector::showProfilers() void CCDirector::showProfilers()
{ {
@ -938,17 +895,10 @@ void CCDisplayLinkDirector::mainLoop(void)
{ {
if (! m_bInvalid) if (! m_bInvalid)
{ {
// compute frame rate drawScene();
computeFrameRate();
// control frame rate
if (m_fFrameRate <= m_fExpectedFrameRate)
{
drawScene();
// release the objects // release the objects
NSPoolManager::getInstance()->pop(); NSPoolManager::getInstance()->pop();
}
} }
} }

View File

@ -57,6 +57,7 @@ public:
void swapBuffers(); void swapBuffers();
bool canSetContentScaleFactor(); bool canSetContentScaleFactor();
void setContentScaleFactor(float contentScaleFactor); void setContentScaleFactor(float contentScaleFactor);
void setAnimationInterval(double interval) {};
protected: protected:
Boolean OnPenDown(EventType* pEvent, Int32 nIndex); Boolean OnPenDown(EventType* pEvent, Int32 nIndex);

View File

@ -52,6 +52,7 @@ public:
void swapBuffers(); void swapBuffers();
bool canSetContentScaleFactor(); bool canSetContentScaleFactor();
void setContentScaleFactor(float contentScaleFactor); void setContentScaleFactor(float contentScaleFactor);
void setAnimationInterval(double interval) {};
virtual bool Create(LPCTSTR pTitle, int w, int h); virtual bool Create(LPCTSTR pTitle, int w, int h);
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);