繁体   English   中英

Cocos2d-x setAnimationInterval在Android上不起作用

[英]Cocos2d-x setAnimationInterval doesn't work on Android

我尝试使用以下代码在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控制部件时,它什么也没做,而当我编写我的版本时,它却起作用了……无论如何,那里的“ magic”值40给出了约35fps,但您当然可以轻松更改它可以与通过setAnimationInterval()传递的值一起使用;

编辑:我将loopStart行移动到睡眠后->睡眠时间不应包含在loopTime中。

这是一件令人毛骨悚然的事情。但是,如果您有两个场景一起播放,并且两个场景都以30 fps播放,尽管pDirector->getAnimationInterval()返回的间隔为pDirector->getAnimationInterval() ,但您仍然可以获得60+ fps。

此代码可以正常工作...

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;
*/

}

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM