cocos2d-x3.2 game Core loop in application, how to deal with FPS instability

Source: Internet
Author: User

Today the weather is very cloudy, it is going to rain soon, Chen eat early kung fu write something,

A cold autumn rain, 10 of the rain to wear cotton, you will henceforth wear a bit more

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();

//See, in fact all the games are in this loop

while (!glview->windowshouldclose())//If you do not exit, for example, the user presses home and exits.

{

Lasttime = Getcurrentmillsecond(); Get current system time

Director->mainloop(); 1. Working with game graphics and game logic

Glview->pollevents(); 2. Dealing with Game interactions

Curtime = Getcurrentmillsecond(); 3. the above 1.2 must consume a certain amount of time and the more animations and monsters may take a lot of time, so curtime-lasttime is how long this calculation consumes.

if (Curtime-lasttime < _animationinterval) //If Curtime-lasttime < _animationinterval

Description in 60/1 seconds to complete the calculation of this frame and then let the cup sleep fps remaining time to ensure the stability of the frame rate

{

Usleep (static_cast<useconds_t> ((_animationinterval -curtime + lasttime) *1000 ));

}

What if you want to know if there is any instability in the game? Add the following code

Else

{

//........

}

}

Do you understand, dear?

/* Only work on Desktop

* Director::mainloop is really one frame logic

* When we want to close the window, we should call Director::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;

}

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.