4

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

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

它在 iOS 上运行,但是当我在三个 Android 设备上测试它时,它被忽略了,并以标准间隔 (1/60) 呈现帧。

如何使用 cocos2d-x 在 Android 上正确设置最大 FPS?

4

3 回答 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 行移到睡眠后-> 睡眠时间不应包含在 loopTime 中。

于 2013-04-05T09:26:54.247 回答
1

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

于 2013-04-04T17:16:22.213 回答
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;
*/

}

希望能帮助到你

于 2014-12-05T07:12:01.587 回答