From bc5f4d9d598daf1de58eb007f393aea962b89614 Mon Sep 17 00:00:00 2001 From: Reck Hou Date: Mon, 16 Jun 2014 16:05:40 +0800 Subject: [PATCH] Optimize FPS control algorithm under Android. --- .../org/cocos2dx/lib/Cocos2dxRenderer.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java index a40d06019e..14cb7739c6 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java @@ -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; - */ } // ===========================================================