关于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_dll drawnode : public node
{
public:
//初始化一个drawnode对象,然后被addchild添加进去就ok了
static drawnode* create();
//画实心圆,参数分别是圆心位置、圆半径、圆填充颜色,如果要画空心圆,就把圆当多边形画(这个多边形点数很多而已)
void drawdot(const vec2 &pos, float radius, const color4f &color);
//画线段,从from到to,2*radius是线段的宽度和radius是线段两头半圆形的半径
void drawsegment(const vec2 &from, const vec2 &to, float radius, const color4f &color);
//画多边形,verts为点集,count为点数,fillcolor为填充颜色,borderwidth为边缘线宽,bordercolor为边缘线颜色
void drawpolygon(vec2 *verts, int count, const color4f &fillcolor, float borderwidth, const color4f &bordercolor);
//画三角形,三人顶点及其填充色
void drawtriangle(const vec2 &p1, const vec2 &p2, const vec2 &p3, const color4f &color);
//画三次贝塞尔曲线
void drawcubicbezier(const vec2& from, const vec2& control1, const vec2& control2, const vec2& to, unsigned int segments, const color4f &color);
//画二次贝塞尔曲线
void drawquadraticbezier(const vec2& from, const vec2& control, const vec2& to, unsigned int segments, const color4f &color);
/** clear the geometry in the node's buffer. */
void clear();
/**
* @js na
* @lua na
*/
const blendfunc& getblendfunc() const;
/**
* @code
* when this function bound into js or lua,the parameter will be changed
* in js: var setblendfunc(var src, var dst)
* @endcode
* @lua na
*/
void setblendfunc(const blendfunc &blendfunc);
void ondraw(const mat4 &transform, uint32_t flags);
// 新的绘图渲染函数
virtual void draw(renderer *renderer, const mat4 &transform, uint32_t flags) override;
cc_constructor_access:
drawnode();
virtual ~drawnode();
virtual bool init();
protected:
void ensurecapacity(int count);
gluint _vao;
gluint _vbo;
int _buffercapacity;
glsizei _buffercount;
v2f_c4b_t2f *_buffer;
blendfunc _blendfunc;
customcommand _customcommand;
bool _dirty;
private:
cc_disallow_copy_and_assign(drawnode);
};
看完上面的api接口后,下面使用起来实在是太方便了。使用drawnode 类绘制图形是最简单的方法,create一个drawnode类,然后添加进场景。然后就可以愉快的绘图了:
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
//创建drawnode对象
drawnode *drawnode = drawnode::create();
//加入场景就ok
this->addchild(drawnode, 20);
//画实心圆
drawnode->drawdot(vec2(100, 100), 50, color4f(0.5,0.6,0,1));
//画线段
drawnode->drawsegment(vec2(100,100), vec2(100,220), 0.5, color4f(0,1,0,1));
// 画多边形
vec2 points[] = { vec2(s.height/4, 10), vec2(s.width - 10, s.height/5), vec2(s.width/3*2,s.height) };
drawnode->drawpolygon(points, sizeof(points)/sizeof(points[0]), color4f(1,0,0,0.5), 2, color4f(0,0,1,1));
// 画三角形
drawnode->drawtriangle(vec2(10, 10), vec2(70, 30), vec2(100, 140), color4f(ccrandom_0_1(), ccrandom_0_1(), ccrandom_0_1(), 0.5));
// 画二次贝塞尔曲线
drawnode->drawquadraticbezier(vec2(s.width - 150, s.height - 150), vec2(s.width - 70, s.height - 10), vec2(s.width - 10, s.height - 10),
10, color4f(ccrandom_0_1(), ccrandom_0_1(), ccrandom_0_1(), 0.5));
// 画三次贝塞尔曲线
draw->drawcubicbezier(vec2(s.width - 250, 40), vec2(s.width - 70, 100), vec2(s.width - 30, 250), vec2(s.width - 10, s.height - 50),
10, color4f(ccrandom_0_1(), ccrandom_0_1(), ccrandom_0_1(), 0.5));
基本用法就是这么简单,如果需要别的形状,自己组合diy。
来源网址:http://blog.csdn.net/ac_huang/article/details/39522473
