我尝试使用以下代码在Cocos2d-x中设置应用程序中的最大FPS:
CCDirector::sharedDirector()->setAnimationInterval(1.0 / 30);
它适用于iOS,但是当我在三个Android设备上测试它时,它会被忽略,并以标准间隔(1/60)呈现帧.
如何使用cocos2d-x在Android上正确设置最大FPS?
解决方法
所以我实际上已经成功实现了它.您必须编辑Cocos2dxRenderer.java文件,然后清理并重建Cocos2d-x.
这是代码:
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. try { if (loopRuntime < 40) { Log.wtf("RENDERER","Sleeping for == " + (40 - loopRuntime)); Thread.sleep(40 - loopRuntime); } } catch (InterruptedException e) { e.printStackTrace(); } //final long nowInNanoSeconds = System.nanoTime(); //final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds; loopStart = System.currentTimeMillis(); // should render a frame when onDrawFrame() is called or there is a // "ghost" Cocos2dxRenderer.nativeRender(); loopEnd = System.currentTimeMillis(); loopRuntime = (loopEnd - loopStart); Log.wtf("RENDERER","loopRunTime == " + loopRuntime); // 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; }
奇怪的是,当我取消注释那里的fps控制部件时它没有做任何事情,当我编写我的版本时它确实……
无论如何,40的“魔术”值给出了大约35fps,但是当然你可以很容易地改变它以使用通过setAnimationInterval()传递的值;
编辑:我将loopStart行移至睡眠后 – >休眠时间不应包含在loopTime中.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。