zoukankan      html  css  js  c++  java
  • Cocos2d-x3.2游戏的核心循环在Application,怎样处理FPS不稳

    今天天气非常阴,立即要下雨了,陈吃早点功夫写点东西,

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

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

        //看到了吗,事实上全部游戏就在这个循环中

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

        

        return 0;

    }


    void Application::setAnimationInterval(double interval)

    {

        _animationInterval = interval*1000.0f;

    }

  • 相关阅读:
    报表-普通表格中的行号
    Wyn BI-条件格式化-以分组为单位设置交替背景色
    报表-表格-背景颜色或背景图片设置
    报表-交叉分析表中的行号
    矩表中如何根据条件隐藏行、列
    仪表板中关于指标值的联动分析设置
    报表设计技巧-使用表格实现多行自由布局报表
    报表设计技巧-矩表向导让报表设计速度提升10倍以上
    容器内外的可视化元素如何设置联动关系
    报表表格数据排序显示
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4028142.html
Copyright © 2011-2022 走看看