Merge pull request #7078 from reckhou/v3

Optimize FPS control algorithm under Android.
This commit is contained in:
minggo 2014-07-28 14:00:55 +08:00
commit 191f7a57e1
2 changed files with 30 additions and 22 deletions

View File

@ -68,7 +68,16 @@ int Application::run()
void Application::setAnimationInterval(double interval)
{
// NYI
JniMethodInfo methodInfo;
if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxRenderer", "setAnimationInterval",
"(D)V"))
{
CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
}
else
{
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, interval);
}
}
//////////////////////////////////////////////////////////////////////////

View File

@ -82,31 +82,30 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
@Override
public void onDrawFrame(final GL10 gl) {
/*
* FPS controlling algorithm is not accurate, and it will slow down FPS
* on some devices. So comment FPS controlling code.
* No need to use algorithm in default(60 FPS) situation,
* since onDrawFrame() was called by system 60 times per second by default.
*/
/*
final long nowInNanoSeconds = System.nanoTime();
final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;
*/
// should render a frame when onDrawFrame() is called or there is a
// "ghost"
if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {
Cocos2dxRenderer.nativeRender();
} else {
final long now = System.nanoTime();
final long interval = now - this.mLastTickInNanoSeconds;
/*
// fps controlling
if (interval < Cocos2dxRenderer.sAnimationInterval) {
try {
// because we render it before, so we should sleep twice time interval
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}
this.mLastTickInNanoSeconds = nowInNanoSeconds;
/*
* Render time MUST be counted in, or the FPS will slower than appointed.
*/
final long renderStart = System.nanoTime();
Cocos2dxRenderer.nativeRender();
final long renderEnd = System.nanoTime();
final long renderInterval = renderEnd - renderStart;
this.mLastTickInNanoSeconds = renderEnd - renderInterval;
}
}
// ===========================================================