2013-03-29 22 views
4

工作,我嘗試設置最大FPS在我的Cocos2D-X與下面的代碼的應用程序:的Cocos2D-X setAnimationInterval不會在Android

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

它的工作在iOS上,但是當我在測試它三個Android設備被忽略,並以標準間隔(1/60)渲染幀。

如何使用cocos2d-x正確設置Android上的最大FPS?

回答

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中。

1

這是一件可愛的事情。但是如果你有兩個場景一起玩,並且你正在製作兩個場景30 fps,雖然pDirector->getAnimationInterval()返回1/30的間隔,但你仍然可以獲得60+ fps。

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

}

發現希望它可以幫助