Cocos2d-x的setAnimationInterval在Android上无效

4
我尝试使用以下代码在Cocos2d-x应用程序中设置最大帧率:

我尝试使用以下代码在Cocos2d-x应用程序中设置最大帧率:

CCDirector::sharedDirector()->setAnimationInterval(1.0 / 30);

在iOS上可以正常工作,但在我测试的三个Android设备上被忽略了,并且使用默认帧间隔(1/60)渲染框架。

我如何在cocos2d-x中正确设置Android的最大FPS?

3个回答

5

我实际上已经成功地实现了它。您需要编辑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之后->睡眠时间不应包含在loopTime中。

1

这是一个奇怪的事情。但如果您同时播放两个场景,并使两个场景都以30帧每秒的速度运行,尽管 pDirector->getAnimationInterval() 返回1/30的间隔,但您仍然会获得60+帧每秒的效果。


0

这段代码运行良好...

来源于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;
*/

}

希望能有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接