Cocos2d-x3.2 game core loop in Application, how to deal with FPS instability

Source: Internet
Author: User

Cocos2d-x3.2 game core loop in Application, how to deal with FPS instability

The weather is overcast today and it is about to rain. Chen has to write something early,

One autumn rain, one cold, ten autumn rain, and more cotton from now on

Int Application: run ()

{

If (! ApplicationDidFinishLaunching ())

{

Return 1;

}

Long lastTime = 0L;

Long curTime = 0L;

Auto director = Director: getInstance ();

Auto glview = director-> getOpenGLView ();

// Retain glview to avoid glview being released in the while loop

Glview-> retain ();

// Have you seen it? In fact, all the games are in this loop.

While (! Glview-> windowShouldClose () // if it is not exited, for example, if the user presses home, it exits.

{

LastTime = getCurrentMillSecond (); // gets the current system time

Director-> mainLoop (); // 1. Process game graphics and game logic

Glview-> pollEvents (); // 2. Process game Interaction

CurTime = getCurrentMillSecond (); // 3. The above 1.2 must consume a certain amount of time and the more animations and monsters it may takeCurTime-lastTime is the time consumed by this calculation.

If (curTime-lastTime <_ animationInterval) // ifCurTime-lastTime <_ animationInterval

// This indicates that the frame calculation is completed within 60/1 seconds and the rest of the cup sleep FPS is used to ensure the stability of the frame rate.

{

Usleep (static_cast (_ AnimationInterval-curTime + lastTime) * 1000 ));

}

//// // If you want to know whether the game is unstable? Add the following code:

Else

{

//........

}

}

//// // Do you understand

/* Only work on Desktop

* Director: mainLoop is really one frame logic

* When we want to close the window, we shocould call ctor: end ();

* Then call Director: mainLoop to do release of internal resources

*/

If (glview-> isOpenGLReady ())

{

Director-> end ();

Director-> mainLoop ();

}

Glview-> release ();

Return 0;

}


Void Application: setAnimationInterval (double interval)

{

_ AnimationInterval = interval * 1000.0f;

}

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.