Cocos2d-x3.0 画图函数的使用

时间:2023-02-08 22:34:14

头文件中添加如下代码:

#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "VisibleRect.h"
VisibleRect.h是Cocos2d-x中TestCpp中的类,在类定义中添加如下代码:

protected:
    void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, bool transformUpdated);
    void onDraw(const cocos2d::Mat4 &transform, bool transformUpdated);
    cocos2d::CustomCommand _customCommand;
draw是渲染函数,但是真正的画图函数是onDraw,Customcommand引擎提供的渲染,共有4种,见下一篇文章。

在cpp文件中添加入下函数:

void HelloWorld::draw(cocos2d::Renderer *renderer, const Mat4 &transform, bool transformUpdated)
{   //固定格式
    _customCommand.init(1);
    _customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw, this,transform,transformUpdated);
    renderer->addCommand(&_customCommand);
}

void HelloWorld::onDraw(const Mat4 &transform, bool transformUpdated)
{
    kmGLPushMatrix();
    kmGLLoadMatrix(&transform);
    

    //直线
    CHECK_GL_ERROR_DEBUG();
    DrawPrimitives::drawLine(VisibleRect::leftTop(), VisibleRect::rightBottom());
    
    
    CHECK_GL_ERROR_DEBUG();
    
    glLineWidth( 5.0f );
    DrawPrimitives::setDrawColor4B(255,0,0,255);
    DrawPrimitives::drawLine( Point(0, 0), Point(100, 100) );

    
    // draw big point in the center
    DrawPrimitives::setPointSize(64);
    DrawPrimitives::setDrawColor4B(100, 0, 255, 128);
    DrawPrimitives::drawPoint(VisibleRect::center());
    CHECK_GL_ERROR_DEBUG();
    
    
    // draw 4 small points
    Vec2 vecs[] = { Point(60,60), Point(70,70), Point(80,80), Point(90,90) };
    DrawPrimitives::setPointSize(10);
    DrawPrimitives::setDrawColor4B(0,10,255,255);
    DrawPrimitives::drawPoints( vecs, 4);
    
    CHECK_GL_ERROR_DEBUG();


    // draw a green circle with 10 segments
    glLineWidth(10);
    DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
    DrawPrimitives::drawCircle( VisibleRect::center(), 100, 0, 200, false);
//参数依次是中心、半径、角度、段、是否位于中心   
//  void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
//  {
//  drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);
//  }

  
    CHECK_GL_ERROR_DEBUG();

    // draw a green circle with 50 segments with line to center
    glLineWidth(2);
    DrawPrimitives::setDrawColor4B(0, 255, 255, 255);
    DrawPrimitives::drawCircle( VisibleRect::center(), 150, CC_DEGREES_TO_RADIANS(90), 50, false);
    
    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);
//参数依次为:中心、半径、角度、段、X轴方向缩放、Y轴方向缩放
//    void drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY)

    
    
    CHECK_GL_ERROR_DEBUG();
    

    

    // open yellow poly
    //创建一个多边形,不封闭的
    DrawPrimitives::setDrawColor4B(255, 255, 0, 255);
    glLineWidth(5);
    Point vertices[] = { Point(10,10), Point(50,50), Point(100,50), Point(150,100), Point(200,150) };
    DrawPrimitives::drawPoly( vertices, 5, false);
    
    CHECK_GL_ERROR_DEBUG();

    
    //闭合的多边形
    // filled poly
    glLineWidth(1);
    Point filledVertices[] = { Point(0,120), Point(50,120), Point(50,170), Point(25,200), Point(0,170) };
    DrawPrimitives::drawSolidPoly(filledVertices, 5, Color4F(0.5f, 0.5f, 1, 1 ) );
 
    // closed purble poly
    DrawPrimitives::setDrawColor4B(255, 0, 255, 255);
    glLineWidth(2);
    Point vertices2[] = { Point(30,130), Point(30,230), Point(50,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(), Point(VisibleRect::center().x+30,VisibleRect::center().y+150), Point(VisibleRect::center().x+60,VisibleRect::center().y-300),Point(VisibleRect::center().x+90,VisibleRect::center().y+150),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) );
    
    CHECK_GL_ERROR_DEBUG();

    //end draw
    kmGLPopMatrix();
}
编译:1)在Mac或iSO下的使用Xcode编译;2)Android下在终端中使用cocos run -p android -j 4

结果入下图:

Cocos2d-x3.0 画图函数的使用