Cocos2d-x setAnimationInterval not working on Android

I am trying to install max FPS in my application in Cocos2d-x with the following code:

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

It works on iOS, but when I test it on three Android devices, it ignores and displays frames at a standard interval (1/60).

How to properly configure max FPS on Android using cocos2d-x?

+4
source share
3 answers

So I really managed to achieve this. 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; } 

The strange thing is that when I uncommented the parts of the fps control that were there, he did nothing, and when I wrote my version, it does ... Anyway, the "magic" value of 40 gives about 35 frames per second, but you Of course, you can easily change it to work with the values ​​passed through setAnimationInterval ();

EDIT: I moved the loopStart line after sleep -> sleep time should not be included in loopTime.

+5
source

This is a crazy thing. But if you have two scenes playing together and you make both scenes 30 frames per second, although pDirector->getAnimationInterval() returns an interval of 1/30, but you still get 60 + fps.

+1
source

This code works fine ...

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

}

Hope this helps

0
source

All Articles