cocos2d-x-3.0 draw vs onDraw
Asked Answered
T

4

5

I'm using cocos2d-x v3.0 and in some test project I'm doing some custom drawing by overriding Node's draw method, but in the DrawPrimitives example provided they do something like this:

void DrawPrimitivesTest::draw()
{
    _customCommand.init(_globalZOrder);
    _customCommand.func = CC_CALLBACK_0(DrawPrimitivesTest::onDraw, this);
    Director::getInstance()->getRenderer()->addCommand(&_customCommand);
}

void DrawPrimitivesTest::onDraw()
{
    // drawing code here, why?
}

From reading the header and source files it seems like this may be some way of sending render commands straight to the renderer, is that correct?

Should I be using this method to do custom drawing? What's the difference between draw an onDraw?

EDIT:

As @Pedro Soares mentioned, since Cocos2D-X 3.0 you can't override draw() anymore. you have to use draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) instead.

Torritorricelli answered 21/3, 2014 at 2:49 Comment(0)
E
7

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.

Eighteenth answered 22/3, 2014 at 15:4 Comment(0)
C
10

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);
}
Catechist answered 28/3, 2014 at 22:34 Comment(2)
Nice, but please change MyLayer::onDraw to MyLayer::onDrawPrimitives.Tut
Is this the only benefit of overriding onDrawPrimitives. Blitting and stuff without getting bogged down in invalidation rectangles and cascading internal onDraw calls is one of the attractions of game development with cocos2d-x.Herminiahermione
E
7

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.

Eighteenth answered 22/3, 2014 at 15:4 Comment(0)
T
0

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);
}
Tother answered 24/9, 2014 at 7:21 Comment(0)
G
0

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

Galleass answered 20/11, 2014 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.