Search code examples
c++cocos2d-xcocos2d-x-3.0

Why it is so hard to draw a red rectangle Sprite on white background Layer with cocos2d-x?


Damn it! I want to do the simplest thing in the world via cocos2d-x and and I have problems. I need a white layer on that a red rectangle. First I have found out the in order to color a layer I have to derive from cocos2d::LayerColor and LayerColor::initWithColor(Color4B(255, 255, 255, 255)) then I learnt that in order to draw a rectangle I should override draw method like this:

void HelloWorld::draw()
{
DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
DrawPrimitives::drawRect(Point(100,100), Point(400,400));
}

This things work separatlely, but they don't work together. I guess when I override draw, then all drawing reduces to these two function calls, hence the background of my layer becomes black. Where is the solution of this simplest thing?

EDIT:

I have tried to call LayerColor::draw in the overridden one like this:

void HelloWorld::draw()
{
    LayerColor::draw();
DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
DrawPrimitives::drawRect(Point(100,100), Point(400,400));
}

Didn't help. Tried to derive from sprite and addChild to the layer like this:

class BoardView : public Sprite 
{
public:
    BoardView() : Sprite() 
    {

    }

    virtual void draw() override 
    {
        DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
        DrawPrimitives::drawRect(Point(100,100), Point(400,400));
    }
};

But this didn't work either! How can I do this simplest thing? Did I miss something?


Solution

  • I found the tests for the beta here: http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d1/d89/namespacecocos2d_1_1_draw_primitives.html#a196155c1b3410485d0b77379acf22e64

    I created a simple test class to test it:

    #ifndef __ccxtest__DrawTest__
    #define __ccxtest__DrawTest__
    
    #include <iostream>
    #include "cocos2d.h"
    
    class DrawTestLayer : public cocos2d::Layer
    {
    public:
        CREATE_FUNC(DrawTestLayer);
    
    protected:
        void draw();
    };
    
    #endif /* defined(__ccxtest__DrawTest__) */
    

    And the implementation:

    #include "DrawTest.h"
    
    USING_NS_CC;
    
    void DrawTestLayer::draw()
    {
    glLineWidth(1);
    DrawPrimitives::setDrawColor4B(255,255,255,255);
    DrawPrimitives::setPointSize(1);
    
    // Anti-Aliased
    glEnable(GL_LINE_SMOOTH);
    
    // filled poly
    glLineWidth(1);
    Point filledVertices[] = { Point(10,120), Point(50,120), Point(50,170), Point(25,200), Point(10,170) };
    DrawPrimitives::drawSolidPoly(filledVertices, 5, Color4F(0.5f, 0.5f, 1, 1 ) );
    }
    

    Finally in the init of the parent scene/layer:

    auto myLayer = DrawTestLayer::create();
    this->addChild(myLayer);
    

    And the result is a 5 point polygon. I tested this on a generic hello world project, and the background image, colors, fonts, etc showed through.

    Edit: how to show/hide the drawing:

    void DrawTestLayer::draw()
    {
        if ( drawFlagBoolean ) // controlled by timer or scheduled action
        {
            // draw code here
            // every frame primitives are drawn
        }
    }