• cocos2dx 3.x避免空customCommand


    1,导致性能悲剧的写法:

    class A:public CCNode{

    public:

      A(){

        m_sprite=NULL;

        m_isDrawDebug=false;

      }

      virtual~A(){}

      bool init(){

        this->CCNode::init();

        //create m_sprite

        m_sprite=CCSprite::create();

        m_sprite->initWithFile("a.png");

        addChild(m_sprite);

        ....

        return true;

      }

      void draw(Renderer* renderer, const Mat4& transform, uint32_t transformFlags){

        _customCommand_drawDebug(_globalZOrder);

            _customCommand_drawDebug = CC_CALLBACK_0(A::onDrawDebug,this,transform, transformFlags);

            renderer->addCommand(&_customCommand_drawDebug);

        }

      void onDrawDebug(const Mat4 &transform, uint32_t transformFlags){

          if(m_isDrawDebug){

          //draw debug

          ...

        }

      }

    public:

      CCSprite*m_sprite;

      bool m_isDrawDebug;

      CustomCommand _customCommand_drawDebug;

    };

    上面写法,无论m_isDrawDebug是true还是false,如果场景中加1000个对象a,drawCall数量为1000。

    2,不损失性能的写法:

    class A:public CCNode{

    public:

      A(){

        m_sprite=NULL;

        m_isDrawDebug=false;

      }

      virtual~A(){}

      bool init(){

        this->CCNode::init();

        //create m_sprite

        m_sprite=CCSprite::create();

        m_sprite->initWithFile("a.png");

        addChild(m_sprite);

        ....

        return true;

      }

      void draw(Renderer* renderer, const Mat4& transform, uint32_t transformFlags){

              if(m_isDrawDebug){

                  _customCommand_drawDebug(_globalZOrder);

                  _customCommand_drawDebug = CC_CALLBACK_0(A::onDrawDebug,this,transform, transformFlags);

                  renderer->addCommand(&_customCommand_drawDebug);

              }

          }

      void onDrawDebug(const Mat4 &transform, uint32_t transformFlags){

        //draw debug

        ...

      }

    public:

      CCSprite*m_sprite;

      bool m_isDrawDebug;

      CustomCommand _customCommand_drawDebug;

    };

    上面写法当m_isDrawDebug为false时,如果场景中加1000个对象a,则drawCall数量为1。

  • 相关阅读:
    廖雪峰Java12maven基础-1maven入门-2依赖管理
    廖雪峰Java12maven基础-1maven入门-1maven介绍
    廖雪峰Java11多线程编程-4线程工具类-1ThreadLocal
    廖雪峰Java11多线程编程-3高级concurrent包-9Fork_Join
    廖雪峰Java11多线程编程-3高级concurrent包-8CompletableFuture
    廖雪峰Java11多线程编程-3高级concurrent包-7Future
    modelsim remote
    单台电脑上启动多个Modelsim图形环境窗口的简单办法(windows)
    用ModelSim仿真SDRAM操作
    通过文件读写方式实现Matlab和Modelsim的联合仿真
  • 原文地址:https://www.cnblogs.com/wantnon/p/4249914.html
Copyright © 2020-2023  润新知