cocos2d-x 3.2 DrawNode 绘图API

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.2 DrawNode 绘图API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

关于Cocos2d-x 3.x 版本的绘图方法有两种:

1、使用DrawNode类绘制自定义图形。

2、继承Layer类重写draw()方法


以上两种方法都可以绘制自定义图形,根据自己的需要选择合适的方法,这里我们只讨论第一种方法,第二种方法涉及到opengl的知识,暂不讨论。

我们先来简单的看看DrawNode提供的API接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class CC_DLLDrawNode: public Node
{
:
//初始化一个DrawNode对象,然后被addChild添加进去就ok了
static DrawNode*create();
//画实心圆,参数分别是圆心位置、圆半径、圆填充颜色,如果要画空心圆,就把圆当多边形画(这个多边形点数很多而已)
void drawDot( const Vec2&pos, float radius, Color4F&color);
//画线段,从from到to,2*radius是线段的宽度和radius是线段两头半圆形的半径
drawSegment( Vec2&from,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Vec2&to,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Color4F&color);
//画多边形,verts为点集,count为点数,fillColor为填充颜色,borderWidth为边缘线宽,borderColor为边缘线颜色
drawPolygon(Vec2*verts,monospace!important; font-size:1em!important; min-height:inherit!important; color:gray!important; background:none!important">int count,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Color4F&fillColor,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">borderWidth,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Color4F&borderColor);
//画三角形,三人顶点及其填充色
drawTriangle( Vec2&p1,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Vec2&p2,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Vec2&p3,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Color4F&color);
//画三次贝塞尔曲线
drawCubicBezier( Vec2&control1,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Vec2&control2,unsigned segments,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Color4F&color);
//画二次贝塞尔曲线
drawQuadraticBezier( Vec2&control,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">Color4F&color);
/**Clearthegeometryinthenode'sbuffer.*/
clear();
/**
*@jsNA
*@luaNA
*/
BlendFunc&getBlendFunc() ;
/**
*@code
*Whenthisfunctionboundintojsorlua,theparameterwillbechanged
*Injs:varsetBlendFunc(varsrc,vardst)
*@endcode
*@luaNA
*/
setBlendFunc( BlendFunc&blendFunc);
onDraw( Mat4&transform,uint32_tflags);
//新的绘图渲染函数
virtual draw(Renderer*renderer,uint32_tflags)override;
CC_CONSTRUCTOR_ACCESS:
DrawNode();
virtual ~DrawNode();
virtual bool init();
protected :
ensureCapacity( count);
GLuint_vao;
GLuint_vbo;
_bufferCapacity;
GLsizei_bufferCount;
V2F_C4B_T2F*_buffer;
BlendFunc_blendFunc;
CustomCommand_customCommand;
_dirty;
private :
CC_DISALLOW_COPY_AND_ASSIGN(DrawNode);
};

看完上面的API接口后,下面使用起来实在是太方便了。使用DrawNode 类绘制图形是最简单的方法,create一个DrawNode类,然后添加进场景。然后就可以愉快的绘图了:

25
//创建DrawNode对象
DrawNode*drawNode=DrawNode::create();
//加入场景就OK
this ->addChild(drawNode,20);
//画实心圆
drawNode->drawDot(Vec2(100,100),50,Color4F(0.5,0.6,1));
//画线段
drawNode->drawSegment(Vec2(100,Vec2(100,220),0.5,Color4F(0,1,1));
//画多边形
Vec2points[]={Vec2(s.height/4,10),Vec2(s.width-10,s.height/5),Vec2(s.width/3*2,s.height)};
drawNode->drawPolygon(points,153)!important; background:none!important">sizeof (points)/ (points[0]),Color4F(1,0.5),2,1));
//画三角形
drawNode->drawTriangle(Vec2(10,Vec2(70,30),140),Color4F(CCRANDOM_0_1(),CCRANDOM_0_1(),0.5));
//画二次贝塞尔曲线
drawNode->drawQuadraticBezier(Vec2(s.width-150,s.height-150),Vec2(s.width-70,s.height-10),
10,0.5));
//画三次贝塞尔曲线
draw->drawCubicBezier(Vec2(s.width-250,40),Vec2(s.width-30,250),s.height-50),
基本用法就是这么简单,如果需要别的形状,自己组合DIY。


来源网址:http://www.jb51.cc/article/p-wnysnybz-kq.html

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