Cocos2d-x 3.0 Modify Android platform frame rate FPS-solve the problem of hot hair burning in game running phone

Source: Internet
Author: User

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.040);

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 privatestatic long sAnimationInterval = (long) (1.0 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND);public void onDrawFrame(finalGL10 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 privatestatic long sAnimationInterval = (long) (1.0 40* Cocos2dxRenderer.NANOSECONDSPERSECOND);

2. Add an attribute variable

1 privatelongrenderingElapsedTime = 0;

3. Overriding the rendering method Ondrawframe

1234567891011121314151617181920 @OverridepublicvoidonDrawFrame(finalGL10 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    longrenderingStartedTimestamp = 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.