シヴァのブログ

UnityやUE4や趣味とかいろいろ...

「Cocos2D-x」Android端末を60FPSに修正させる

端末によって、35~40フレームしか出ないことがある。
それを、60フレームに修正する方法をメモ。

※Cocos2d-xのバージョン:Cocos2d-x3.13.1


Cocosのファイル内(例:C:\cocos2d-x\cocos2d-x-3.13.1\cocos\platform\android\java\src\org\cocos2dx\lib)の「Cocos2dxRenderer.java」ファイルの「onDrawFrame」を以下のように修正する。

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

参考サイト:http://discuss.cocos2d-x.org/t/setanimationinterval-does-nothing-on-android/6419/4