cocos2d-x在android真机上设置帧率无效的问题
通过setAnimationInterval设置帧频时,发现在android下没有效果的
在Cocos2dxRenderer .java文件里面找到了onDrawFrame这个函数。里面有句注释 :
FPS controlling algorithm is not accurate, and it will slow down FPS on some devices. So comment FPS controlling code.
这个函数里的大部分代码都被注释掉了
1 public void onDrawFrame(final GL10 gl) { 2 3 /* 4 * FPS controlling algorithm is not accurate, and it will slow down FPS 5 * on some devices. So comment FPS controlling code. 6 */ 7 8 /* 9 final long nowInNanoSeconds = System.nanoTime(); 10 final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds; */ 11 12 // should render a frame when onDrawFrame() is called or there is a 13 // "ghost" 14 Cocos2dxRenderer.nativeRender(); 15 16 /* 17 // fps controlling if (interval < Cocos2dxRenderer.sAnimationInterval) { try { 18 // because we render it before, so we should sleep twice time interval 19 Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) /Cocos2dxRenderer.NANOSECONDSPERMICROSECOND); 20 } catch (final Exception e) { } 21 } 22 23 this.mLastTickInNanoSeconds = nowInNanoSeconds; 24 */ 25 26 }
经过google后暂时找到了解决方案,是否有问题需要待测试:
1 private long renderingElapsedTime; 2 3 @Override 4 public void onDrawFrame(final GL10 gl) { 5 /* 6 * FPS controlling algorithm is not accurate, and it will slow down FPS 7 * on some devices. So comment FPS controlling code. 8 */ 9 10 try { 11 if (renderingElapsedTime< Cocos2dxRenderer.sAnimationInterval) { 12 Thread.sleep((Cocos2dxRenderer.sAnimationInterval - renderingElapsedTime) / NANOSECONDSPERMICROSECOND); 13 } 14 } catch (InterruptedException e) { 15 e.printStackTrace(); 16 } 17 18 // Get the timestamp when rendering started 19 long renderingStartedTimestamp = System.nanoTime(); 20 21 // should render a frame when onDrawFrame() is called or there is a 22 // "ghost" 23 Cocos2dxRenderer.nativeRender(); 24 25 // Calculate the elapsed time during rendering 26 renderingElapsedTime = (System.nanoTime() - renderingStartedTimestamp); 27 }