Optimize FPS control algorithm under Android.

This commit is contained in:
Reck Hou 2014-06-16 16:05:40 +08:00
parent c4d4d6b34e
commit bc5f4d9d59
1 changed files with 20 additions and 21 deletions

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.
*/
if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {
Cocos2dxRenderer.nativeRender();
} else {
final long now = System.nanoTime();
final long interval = now - this.mLastTickInNanoSeconds;
/*
final long nowInNanoSeconds = System.nanoTime();
final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;
*/
// should render a frame when onDrawFrame() is called or there is a
// "ghost"
Cocos2dxRenderer.nativeRender();
/*
// 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) {
if (interval < Cocos2dxRenderer.sAnimationInterval) {
try {
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}
/*
* 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;
}
this.mLastTickInNanoSeconds = nowInNanoSeconds;
*/
}
// ===========================================================