cocos2d-x-3.0 draw vs onDraw

只愿长相守 提交于 2019-11-30 04:30:12

In future, cocos2d-x 3.x renderer will be multithreaded with command pool.

draw method called by visit method, to create new command. When command is performed by command pool, onDraw is called. At this moment, commands are performed in single thread, but in overloaded onDraw method you should assume, that it will be called in another thread to simplify future migration.

There is sample on cocos2d-x RC0 package that shows how to use the DrawPrimitives on top of other layers.

On your Layer .h add the following:

private:
    void onDrawPrimitives(const kmMat4 &transform, bool transformUpdated);
    CustomCommand _customCommand;

Now in the cpp of the Layer, override the layer draw method and include the onDrawPrimitives method:

void MyLayer::onDrawPrimitives(const kmMat4 &transform, bool transformUpdated)
{
    kmGLPushMatrix();
    kmGLLoadMatrix(&transform);

    //add your primitive drawing code here
    DrawPrimitives::drawLine(ccp(0,0), ccp(100, 100));
}

void MyLayer::draw(Renderer *renderer, const kmMat4& transform, bool transformUpdated)
{
    _customCommand.init(_globalZOrder);
    _customCommand.func = CC_CALLBACK_0(MyLayer::onDrawPrimitives, this, transform, transformUpdated);
    renderer->addCommand(&_customCommand);
}

I use draw method for debugDraw Like this It may be helpful

void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    Layer::draw(renderer, transform, flags);
    Director* director = Director::getInstance();
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION );
    director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    world->DrawDebugData();
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

The draw() expression should be the same as the base class function. The draw method of Node for cocos 3.3rc is: virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!