Cocos2d-x3.2游戏的核心循环在Application,怎样处理FPS不稳
今天天氣非常陰,立即要下雨了,陳吃早點功夫?qū)扅c東西,
一場秋雨一場寒,十場秋雨要穿棉,各位從今往后多穿點
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();
//看到了嗎,事實上全部游戲就在這個循環(huán)中
while (!glview->windowShouldClose()) //假設(shè)沒退出,比方用戶按下了home 就退出了
{
lastTime = getCurrentMillSecond(); //獲取當(dāng)前系統(tǒng)時間
director->mainLoop(); //1.處理游戲畫圖和游戲邏輯
glview->pollEvents(); //2.處理游戲交互
curTime = getCurrentMillSecond(); //3.以上1.2必然要消耗一定的時間并且動畫和怪物越多可能耗時越大所以curTime - lastTime就是這次計算消耗的時間
if (curTime - lastTime <
_animationInterval) //假設(shè)curTime - lastTime < _animationInterval
//說明在60/1秒內(nèi)完畢了這一幀的計算再讓cup sleep FPS剩下時間來保證幀率的穩(wěn)定
{
usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime)*1000));
}
/////////假設(shè)想知道游戲中是否有不穩(wěn)定怎樣呢? 增加例如以下代碼
else
{
//........
}
}
/////////////////你明確了嗎 親
/* 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;
}
總結(jié)
以上是生活随笔為你收集整理的Cocos2d-x3.2游戏的核心循环在Application,怎样处理FPS不稳的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在部队如果不服降衍降职怎么办?
- 下一篇: Django(九)admin相关知识