加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x3.2游戏的核心循环在Application,如何处理FPS不稳

发布时间:2020-12-14 21:11:37 所属栏目:百科 来源:网络整理
导读:Cocos2d-x3.2游戏的核心循环在Application,如何处理FPS不稳 今天天气很阴,马上要下雨了,陈吃早点功夫写点东西, 一场秋雨一场寒,十场秋雨要穿棉,各位从今往后多穿点 int Application ::run() { if (! applicationDidFinishLaunching ()) { return 1 ; }

Cocos2d-x3.2游戏的核心循环在Application,如何处理FPS不稳

今天天气很阴,马上要下雨了,陈吃早点功夫写点东西,

一场秋雨一场寒,十场秋雨要穿棉,各位从今往后多穿点

intApplication::run()

{

if(!applicationDidFinishLaunching())

{

return1;

}

longlastTime =0L;

longcurTime =0L;

autodirector =Director::getInstance();

autoglview = director->getOpenGLView();

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

glview->retain();

//看到了吗,其实所有游戏就在这个循环中

while(!glview->windowShouldClose()) //如果没退出,比如用户按下了home 就退出了

{

lastTime =getCurrentMillSecond(); //获取当前系统时间

director->mainLoop(); //1.处理游戏绘图和游戏逻辑

glview->pollEvents(); //2.处理游戏交互

curTime =getCurrentMillSecond(); //3.以上1.2必定要消耗一定的时间而且动画和怪物越多可能耗时越大所以curTime - lastTime就是这次计算消耗的时间

if(curTime - lastTime <_animationInterval)//如果curTime - lastTime <_animationInterval

//说明在60/1秒内完成了这一帧的计算再让cup sleep FPS剩下时间来保证帧率的稳定

{

usleep(static_cast<useconds_t>((_animationInterval- curTime + lastTime)*1000));

}

/////////如果想知道游戏中是否有不稳定如何呢? 加入如下代码

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

return0;

}


voidApplication::setAnimationInterval(doubleinterval)

{

_animationInterval= interval*1000.0f;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读