【问题标题】:Is it possible to set dynamic frame-rate with cocos2d-x?是否可以使用 cocos2d-x 设置动态帧率?
【发布时间】:2014-12-19 15:52:51
【问题描述】:

我想用cocos2d-x写一个有特效的文本阅读器,所以大多数时候图形是静态的。如果我使用 cocos2d-x,它只是非常消耗电池电量。

那么是否可以通过编码来调整cocos2d-x的帧率呢?如何?我想在文本静止时降低帧率,并在向上或向下翻页时提高帧率。

或者在 Android 上实现这个目标有什么好主意? (翻页动画和更高效的文本渲染。)

【问题讨论】:

  • 在 cocos2d-iphone 中有 CCDirector animationInterval 定义了最大帧率。

标签: android text rendering cocos2d-x


【解决方案1】:

你可以使用 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();
    }
}

【讨论】:

  • 谢谢,你真的给了我一些宝贵的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2020-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多