是否可以使用cocos2d-x设置动态帧速率?

时间:2014-12-19 15:52:51

标签: android text rendering cocos2d-x

我想写一个与cocos2d-x有特殊效果的文本阅读器,所以图表静态的时间最多。如果我使用cocos2d-x,它只是大量消耗电池电量。

那么可以通过编码来调整cocos2d-x的帧速率吗?如何?我希望在文本静态时降低帧速率,并在向上或向下翻页时提高帧速率。

或者Android上这个目标的任何好主意? (翻页动画和更有效的文字渲染。)

1 个答案:

答案 0 :(得分:0)

您可以使用cocos2d :: Director :: setAnimationInterval方法更改帧速率。

https://github.com/cocos2d/cocos2d-x/blob/1361f2c6195ce338a70b65c17e3d46f38e6bcce2/cocos/base/CCDirector.h#L140-L141

/** Set the FPS value. */
virtual void setAnimationInterval(double interval) = 0;

但是,我想知道,如果你设置帧率低,你的C ++代码在分页向上或向下时不会被立即调用,因为帧率很低。因此,当用户触摸屏幕时,您可能需要修改onDrawFrame以立即调用Cocos2dxRenderer.nativeRender。

https://github.com/cocos2d/cocos2d-x/blob/1361f2c6195ce338a70b65c17e3d46f38e6bcce2/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java#L84-L106

@Override
public void onDrawFrame(final GL10 gl) {
    /*
     * No need to use algorithm in default(60 FPS) situation,
     * since onDrawFrame() was called by system 60 times per second by default.
     */
    if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {
        Cocos2dxRenderer.nativeRender();
    } else {
        final long now = System.nanoTime();
        final long interval = now - this.mLastTickInNanoSeconds;

        if (interval < Cocos2dxRenderer.sAnimationInterval) {
            try {
                Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
            } catch (final Exception e) {
            }
        }
        /*
         * Render time MUST be counted in, or the FPS will slower than appointed.
        */
        this.mLastTickInNanoSeconds = System.nanoTime();
        Cocos2dxRenderer.nativeRender();
    }
}