i have class that extend Sprite , i draw primitive shapes in the draw method
when i try to set the Sprite background , the shapes do show up but no background color
in short what is the proper way to create sprite with custom background and primitive
shapes drawn on the sprite ?
class Block : public Sprite
{
public:
static Block* create();
Block();
virtual ~Block();
virtual void draw();
protected:
void onDraw();
CustomCommand _customCommand;
private:
};
#endif
Block::Block()
{
}
Block::~Block()
{
}
void Block::draw()
{
_customCommand.init(0, _vertexZ);
_customCommand.func = CC_CALLBACK_0(Block::onDraw, this);
Director::getInstance()->getRenderer()->addCommand(&_customCommand);
}
void Block::onDraw()
{
kmMat4 oldMat;
kmGLGetMatrix(KM_GL_MODELVIEW, &oldMat);
kmGLLoadMatrix(&_modelViewTransform);
//draw
CHECK_GL_ERROR_DEBUG();
// draw a simple line
// The default state is:
// Line Width: 1
// color: 255,255,255,255 (white, non-transparent)
// Anti-Aliased
// glEnable(GL_LINE_SMOOTH);
DrawPrimitives::drawLine( VisibleRect::leftBottom(), VisibleRect::rightTop() );
// draw big point in the center
DrawPrimitives::setPointSize(16);
DrawPrimitives::setDrawColor4B(0,0,255,128);
DrawPrimitives::drawPoint( VisibleRect::center() );
CHECK_GL_ERROR_DEBUG();
// draw a pink solid circle with 50 segments
glLineWidth(2);
DrawPrimitives::setDrawColor4B(255, 0, 255, 255);
DrawPrimitives::drawSolidCircle( VisibleRect::center() + Point(140,0), 40, CC_DEGREES_TO_RADIANS(90), 50, 1.0f, 1.0f);
CHECK_GL_ERROR_DEBUG();
DrawPrimitives::drawCubicBezier(VisibleRect::center(), Point(VisibleRect::center().x+30,VisibleRect::center().y+50), Point(VisibleRect::center().x+60,VisibleRect::center().y-50),VisibleRect::right(),100);
CHECK_GL_ERROR_DEBUG();
//draw a solid polygon
Point vertices3[] = {Point(60,160), Point(70,190), Point(100,190), Point(90,160)};
DrawPrimitives::drawSolidPoly( vertices3, 4, Color4F(1,1,0,1) );
//end draw
kmGLLoadMatrix(&oldMat);
}
Block* Block::create()
{
auto sprite = new Block();
if (sprite && sprite->init())
{
sprite->autorelease();
}
else
{
CC_SAFE_DELETE(sprite);
}
return sprite;
}
and in the layer i init the Block class like this :
Block* pBlock = Block::create();
pBlock->setTextureRect( Rect(0, 0, 100 ,100) );
pBlock->setColor(Color3B::RED);
pBlock->setOpacity(128);
pBlock->setPosition(Point(3*sWinSize.width/4, sWinSize.height/2));
this->addChild(pBlock,20);