首页 > 其他 > 详细

cocos2dx 3.x避免空customCommand

时间:2015-01-26 13:27:19      阅读:368      评论:0      收藏:0      [点我收藏+]

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个zorder相同的对象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个zorder相同的对象a,则drawCall数量为1。

cocos2dx 3.x避免空customCommand

原文:http://www.cnblogs.com/wantnon/p/4249914.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!