cocos2dx之2.x和3.x使用draw绘图的区别

前端之家收集整理的这篇文章主要介绍了cocos2dx之2.x和3.x使用draw绘图的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

**************************************************************************

时间:2014-11-8

作者:Sharing_Li

转载出处:http://www.jb51.cc/article/p-taablfno-rx.html

***************************************************************************

2.x的版本在draw中画直线等图形时,只要继承虚函数draw(),然后就在draw函数调用绘图函数就可以了。
但是在3.x的版本则不相同,draw()函数被标识为final,无法继承进行代码编写,需要继承带参数的draw(Renderer *renderer,const Mat4 &transform,uint32_t flags)函数,并且绘制实现不是在这个函数中,而是在带参数的draw中继续调用onDraw函数.

这一点看下官方的test例子就清楚了,详细代码如下:

声明:

class DrawPrimitivesTest : public BaseLayer
{
public:
    DrawPrimitivesTest();
    
    virtual std::string title() const override;
    virtual std::string subtitle() const override;
    virtual void draw(Renderer *renderer,uint32_t flags) override;

protected:
    void onDraw(const Mat4 &transform,uint32_t flags);
    CustomCommand _customCommand;
};


定义:

void DrawPrimitivesTest::draw(Renderer *renderer,uint32_t flags)
{
    _customCommand.init(_globalZOrder);
    _customCommand.func = CC_CALLBACK_0(DrawPrimitivesTest::onDraw,this,transform,flags);
    renderer->addCommand(&_customCommand);
}

void DrawPrimitivesTest::onDraw(const Mat4 &transform,uint32_t flags)
{
    Director* director = Director::getInstance();
    director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW,transform);
    
    //draw
    CHECK_GL_ERROR_DEBUG();
    
    // draw a simple line
    // The default state is:
    // Line Width: 1
    // color: 255,255 (white,non-transparent)
    // Anti-Aliased
    //  glEnable(GL_LINE_SMOOTH);
    DrawPrimitives::drawLine( VisibleRect::leftBottom(),VisibleRect::rightTop() );
    
    CHECK_GL_ERROR_DEBUG();
    
    // line: color,width,aliased
    // glLineWidth > 1 and GL_LINE_SMOOTH are not compatible
    // GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone
    //  glDisable(GL_LINE_SMOOTH);
    glLineWidth( 5.0f );
    DrawPrimitives::setDrawColor4B(255,255);
    DrawPrimitives::drawLine( VisibleRect::leftTop(),VisibleRect::rightBottom() );
    
    CHECK_GL_ERROR_DEBUG();
    
    // TIP:
    // If you are going to use always thde same color or width,you don't
    // need to call it before every draw
    //
    // Remember: OpenGL is a state-machine.
    
    // draw big point in the center
    DrawPrimitives::setPointSize(64);
    DrawPrimitives::setDrawColor4B(0,128);
    DrawPrimitives::drawPoint( VisibleRect::center() );
    
    CHECK_GL_ERROR_DEBUG();
    
    // draw 4 small points
    Vec2 points[] = { Vec2(60,60),Vec2(70,70),Vec2(60,60) };
    DrawPrimitives::setPointSize(4);
    DrawPrimitives::setDrawColor4B(0,255);
    DrawPrimitives::drawPoints( points,4);
    
    CHECK_GL_ERROR_DEBUG();
    
    // draw a green circle with 10 segments
    glLineWidth(16);
    DrawPrimitives::setDrawColor4B(0,255);
    DrawPrimitives::drawCircle( VisibleRect::center(),100,10,false);
    
    CHECK_GL_ERROR_DEBUG();
    
    // draw a green circle with 50 segments with line to center
    glLineWidth(2);
    DrawPrimitives::setDrawColor4B(0,50,CC_DEGREES_TO_RADIANS(90),true);
    
    CHECK_GL_ERROR_DEBUG();
    
    // draw a pink solid circle with 50 segments
    glLineWidth(2);
    DrawPrimitives::setDrawColor4B(255,255);
    DrawPrimitives::drawSolidCircle( VisibleRect::center() + Vec2(140,0),40,1.0f,1.0f);
    
    CHECK_GL_ERROR_DEBUG();
    
    // open yellow poly
    DrawPrimitives::setDrawColor4B(255,255);
    glLineWidth(10);
    Vec2 vertices[] = { Vec2(0,Vec2(50,50),Vec2(100,100),100) };
    DrawPrimitives::drawPoly( vertices,5,false);
    
    CHECK_GL_ERROR_DEBUG();
    
    // filled poly
    glLineWidth(1);
    Vec2 filledVertices[] = { Vec2(0,120),170),Vec2(25,200),Vec2(0,170) };
    DrawPrimitives::drawSolidPoly(filledVertices,Color4F(0.5f,0.5f,1,1 ) );
    
    
    // closed purble poly
    DrawPrimitives::setDrawColor4B(255,255);
    glLineWidth(2);
    Vec2 vertices2[] = { Vec2(30,130),Vec2(30,230),200) };
    DrawPrimitives::drawPoly( vertices2,3,true);
    
    CHECK_GL_ERROR_DEBUG();
    
    // draw quad bezier path
    DrawPrimitives::drawQuadBezier(VisibleRect::leftTop(),VisibleRect::center(),VisibleRect::rightTop(),50);
    
    CHECK_GL_ERROR_DEBUG();
    
    // draw cubic bezier path
    DrawPrimitives::drawCubicBezier(VisibleRect::center(),Vec2(VisibleRect::center().x+30,VisibleRect::center().y+50),Vec2(VisibleRect::center().x+60,VisibleRect::center().y-50),VisibleRect::right(),100);
    
    CHECK_GL_ERROR_DEBUG();
    
    //draw a solid polygon
    Vec2 vertices3[] = {Vec2(60,160),190),Vec2(90,160)};
    DrawPrimitives::drawSolidPoly( vertices3,4,Color4F(1,1) );
    
    // restore original values
    glLineWidth(1);
    DrawPrimitives::setDrawColor4B(255,255);
    DrawPrimitives::setPointSize(1);
    
    CHECK_GL_ERROR_DEBUG();
    
    //end draw
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}


PS:

另外,可以深入了解一下coco2d-x版本2.x和3.x之间的渲染架构区别:

http://www.jb51.cc/article/p-ugeohuhn-rx.html

猜你在找的Cocos2d-x相关文章