Cocos2d-x setAnimationInterval 在 Android 上不起作用

Cocos2d-x setAnimationInterval doesn't work on Android

本文关键字:不起作用 Android setAnimationInterval Cocos2d-x      更新时间:2023-10-16

我尝试在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行移动到sleep ->之后,sleep时间不应该包含在loopTime中。

这是一件很奇怪的事情。但是如果你有两个场景一起播放,并且你将两个场景都设置为30 fps,尽管pDirector->getAnimationInterval()返回1/30的间隔,但你仍然可以获得60+ fps。

代码运行正常…

found from http://discuss.cocos2d-x.org/t/setanimationinterval-does-nothing-on-android/6419/4

private long renderingElapsedTime;

@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.
 */
try {
    if (renderingElapsedTime * NANOSECONDSPERMICROSECOND < Cocos2dxRenderer.sAnimationInterval) {
        Thread.sleep((Cocos2dxRenderer.sAnimationInterval - renderingElapsedTime * NANOSECONDSPERMICROSECOND) / NANOSECONDSPERMICROSECOND);
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}
/*
final long nowInNanoSeconds = System.nanoTime();
final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;
*/
// Get the timestamp when rendering started
long renderingStartedTimestamp = System.currentTimeMillis();
// should render a frame when onDrawFrame() is called or there is a
// "ghost"
Cocos2dxRenderer.nativeRender();
// Calculate the elapsed time during rendering
renderingElapsedTime = (System.currentTimeMillis() - renderingStartedTimestamp);
/*
// 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;
*/

}

希望有所帮助