我尝试使用以下代码在Cocos2d-x中设置应用程序中的最大FPS: CCDirector::sharedDirector()-setAnimationInterval(1.0 / 30); 它适用于iOS,但是当我在三个Android设备上测试它时,它会被忽略,并以标准间隔
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中.
