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

cocos2dx之2.x和3.x使用draw绘图的区别,第1张

概述************************************************************************** 时间:2014-11-8 作者:Sharing_Li 转载出处:http://www.voidcn.com/article/p-taablfno-rx.html ********************************************

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

时间: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

总结

以上是内存溢出为你收集整理的cocos2dx之2.x和3.x使用draw绘图的区别全部内容,希望文章能够帮你解决cocos2dx之2.x和3.x使用draw绘图的区别所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1009621.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存