Cocos2d-x setAnimationInterval doesn't work on Android
android, c++, cocos2d-x, eclipse, frame-rate
Solution
So I've actually managed to kind of achieve it. You have to edit the Cocos2dxRenderer.java file and then clean and rebuild Cocos2d-x.
Here is the code :
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;
}
Weird thing is that when I uncommented the fps control parts that were there it did nothing, and when I wrote my version it does... Anyway, the "magic" value of 40 there gives about 35fps, but you could of course easily change it to work with values passed through setAnimationInterval();
EDIT : I moved the loopStart line to after the sleep -> the sleep time shouldn't be included in loopTime.
Problem
I try to set the max FPS in my application in Cocos2d-x with the following code: ``` CCDirector::sharedDirector()->setAnimationInterval(1.0 / 30); ``` It's working on iOS, but when I test it on three Android devices it's ignored, and renders frames with the standard interval (1/60). How can I properly set the max FPS on Android using cocos2d-x?