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;
}