After the game was developed using Cocos2d-x 3.0, it was found that the game was very hot on Android phones, and on Meizu 2, almost worried that the phone would explode ~ ~ ~ One of the measures taken is to reduce the frame rate because the game is not very high for frame rate requirements.
The students who have done cocos2d development should know that the way to modify the frame rate on the Win32 platform is very simple, which is to modify it in the AppDelegate.cpp file:
1 |
director->setAnimationInterval( 1.0 / 40 ); |
But this kind of modification in the export Android apk to the real machine test, found in the lower left corner of the debugging information is still realistic 60~65 frame, completely unaffected, after the online search, the minor Android changes need to modify the cocos2dx-x generated Java code changes, Specific in file: Cocos2dxrenderer.java
You can see the following property settings and overridden methods in your code:
12 |
private static long sAnimationInterval = ( long ) ( 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND); public void onDrawFrame( final GL10 gl) {} |
If you want to modify the frame rate of the Android platform, you can modify the code to improve it by the following methods :
1. Change the frame rate to change 60 to 40
1 |
private static long sAnimationInterval = ( long ) ( 1.0 / 40 * Cocos2dxRenderer.NANOSECONDSPERSECOND); |
2. Add an attribute variable
1 |
private long renderingElapsedTime = 0 ; |
3. Overriding the rendering method Ondrawframe
1234567891011121314151617181920 |
@Override
public
void
onDrawFrame(
final
GL10 gl) {
try
{
if
(renderingElapsedTime * NANOSECONDSPERMICROSECOND < Cocos2dxRenderer.sAnimationInterval) {
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - renderingElapsedTime * NANOSECONDSPERMICROSECOND) / NANOSECONDSPERMICROSECOND);
}
}
catch
(InterruptedException e) {
e.printStackTrace();
}
// 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);
}
|
The code will calculate the time spent in rendering, so the resulting frame rate should be very accurate.
Note that cocos2dxrenderer in the Ondrawframe official has a good frame rate algorithm, but was commented out, note text said there is a certain bug, the frame rate is not accurate, in fact, most of the situation can be used normally, so it is best not to use the ~
After doing this, the frame rate of the game is around 38~41, and the problem of heat is basically solved.
Finally, if you want a better, more accurate frame rate algorithm, you can also reply with a message ...
Cocos2d-x 3.0 Modify Android platform frame rate FPS-solve the problem of hot hair burning in game running phone