CCDrawNode.h
13.3 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2013-2016 Chukong Technologies Inc.
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Code copied & pasted from SpacePatrol game https://github.com/slembcke/SpacePatrol
*
* Renamed and added some changes for cocos2d
*
*/
#ifndef __CCDRAWNODES_CCDRAW_NODE_H__
#define __CCDRAWNODES_CCDRAW_NODE_H__
#include "2d/CCNode.h"
#include "base/ccTypes.h"
#include "renderer/CCCustomCommand.h"
#include "math/CCMath.h"
NS_CC_BEGIN
static const int DEFAULT_LINE_WIDTH = 2;
class PointArray;
/**
* @addtogroup _2d
* @{
*/
/** @class DrawNode
* @brief Node that draws dots, segments and polygons.
* Faster than the "drawing primitives" since they draws everything in one single batch.
* @since v2.1
*/
class CC_DLL DrawNode : public Node
{
public:
/** creates and initialize a DrawNode node.
*
* @return Return an autorelease object.
*/
static DrawNode* create(float defaultLineWidth = DEFAULT_LINE_WIDTH);
/** Draw a point.
*
* @param point A Vec2 used to point.
* @param pointSize The point size.
* @param color The point color.
* @js NA
*/
void drawPoint(const Vec2& point, const float pointSize, const Color4F &color);
/** Draw a group point.
*
* @param position A Vec2 pointer.
* @param numberOfPoints The number of points.
* @param color The point color.
* @js NA
*/
void drawPoints(const Vec2 *position, unsigned int numberOfPoints, const Color4F &color);
/** Draw a group point.
*
* @param position A Vec2 pointer.
* @param numberOfPoints The number of points.
* @param pointSize The point size.
* @param color The point color.
* @js NA
*/
void drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color);
/** Draw an line from origin to destination with color.
*
* @param origin The line origin.
* @param destination The line destination.
* @param color The line color.
* @js NA
*/
void drawLine(const Vec2 &origin, const Vec2 &destination, const Color4F &color);
/** Draws a rectangle given the origin and destination point measured in points.
* The origin and the destination can not have the same x and y coordinate.
*
* @param origin The rectangle origin.
* @param destination The rectangle destination.
* @param color The rectangle color.
*/
void drawRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color);
/** Draws a polygon given a pointer to point coordinates and the number of vertices measured in points.
* The polygon can be closed or open.
*
* @param poli A pointer to point coordinates.
* @param numberOfPoints The number of vertices measured in points.
* @param closePolygon The polygon can be closed or open.
* @param color The polygon color.
*/
void drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color);
/** Draws a circle given the center, radius and number of segments.
*
* @param center The circle center point.
* @param radius The circle rotate of radius.
* @param angle The circle angle.
* @param segments The number of segments.
* @param drawLineToCenter Whether or not draw the line from the origin to center.
* @param scaleX The scale value in x.
* @param scaleY The scale value in y.
* @param color Set the circle color.
*/
void drawCircle( const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, const Color4F &color);
/** Draws a circle given the center, radius and number of segments.
*
* @param center The circle center point.
* @param radius The circle rotate of radius.
* @param angle The circle angle.
* @param segments The number of segments.
* @param drawLineToCenter Whether or not draw the line from the origin to center.
* @param color Set the circle color.
*/
void drawCircle(const Vec2 ¢er, float radius, float angle, unsigned int segments, bool drawLineToCenter, const Color4F &color);
/** Draws a quad bezier path.
*
* @param origin The origin of the bezier path.
* @param control The control of the bezier path.
* @param destination The destination of the bezier path.
* @param segments The number of segments.
* @param color Set the quad bezier color.
*/
void drawQuadBezier(const Vec2 &origin, const Vec2 &control, const Vec2 &destination, unsigned int segments, const Color4F &color);
/** Draw a cubic bezier curve with color and number of segments
*
* @param origin The origin of the bezier path.
* @param control1 The first control of the bezier path.
* @param control2 The second control of the bezier path.
* @param destination The destination of the bezier path.
* @param segments The number of segments.
* @param color Set the cubic bezier color.
*/
void drawCubicBezier(const Vec2 &origin, const Vec2 &control1, const Vec2 &control2, const Vec2 &destination, unsigned int segments, const Color4F &color);
/** Draws a Cardinal Spline path.
*
* @param config A array point.
* @param tension The tension of the spline.
* @param segments The number of segments.
* @param color Set the Spline color.
*/
void drawCardinalSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color);
/** Draws a Catmull Rom path.
*
* @param points A point array of control point.
* @param segments The number of segments.
* @param color The Catmull Rom color.
*/
void drawCatmullRom(PointArray *points, unsigned int segments, const Color4F &color);
/** draw a dot at a position, with a given radius and color.
*
* @param pos The dot center.
* @param radius The dot radius.
* @param color The dot color.
*/
void drawDot(const Vec2 &pos, float radius, const Color4F &color);
/** Draws a rectangle with 4 points.
*
* @param p1 The rectangle vertex point.
* @param p2 The rectangle vertex point.
* @param p3 The rectangle vertex point.
* @param p4 The rectangle vertex point.
* @param color The rectangle color.
*/
void drawRect(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color);
/** Draws a solid rectangle given the origin and destination point measured in points.
* The origin and the destination can not have the same x and y coordinate.
*
* @param origin The rectangle origin.
* @param destination The rectangle destination.
* @param color The rectangle color.
* @js NA
*/
void drawSolidRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color);
/** Draws a solid polygon given a pointer to CGPoint coordinates, the number of vertices measured in points, and a color.
*
* @param poli A solid polygon given a pointer to CGPoint coordinates.
* @param numberOfPoints The number of vertices measured in points.
* @param color The solid polygon color.
* @js NA
*/
void drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color);
/** Draws a solid circle given the center, radius and number of segments.
* @param center The circle center point.
* @param radius The circle rotate of radius.
* @param angle The circle angle.
* @param segments The number of segments.
* @param scaleX The scale value in x.
* @param scaleY The scale value in y.
* @param color The solid circle color.
* @js NA
*/
void drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY, const Color4F &color);
/** Draws a solid circle given the center, radius and number of segments.
* @param center The circle center point.
* @param radius The circle rotate of radius.
* @param angle The circle angle.
* @param segments The number of segments.
* @param color The solid circle color.
* @js NA
*/
void drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, const Color4F& color);
/** draw a segment with a radius and color.
*
* @param from The segment origin.
* @param to The segment destination.
* @param radius The segment radius.
* @param color The segment color.
*/
void drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color);
/** draw a polygon with a fill color and line color
* @code
* When this function bound into js or lua,the parameter will be changed
* In js: var drawPolygon(var Arrayofpoints, var fillColor, var width, var borderColor)
* In lua:local drawPolygon(local pointTable,local tableCount,local fillColor,local width,local borderColor)
* @endcode
* @param verts A pointer to point coordinates.
* @param count The number of verts measured in points.
* @param fillColor The color will fill in polygon.
* @param borderWidth The border of line width.
* @param borderColor The border of line color.
* @js NA
*/
void drawPolygon(const Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor);
/** draw a triangle with color.
*
* @param p1 The triangle vertex point.
* @param p2 The triangle vertex point.
* @param p3 The triangle vertex point.
* @param color The triangle color.
* @js NA
*/
void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &color);
/** Clear the geometry in the node's buffer. */
void clear();
/** Get the color mixed mode.
* @lua NA
*/
const BlendFunc& getBlendFunc() const;
/** Set the color mixed mode.
* @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);
// Overrides
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
virtual void visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
void setLineWidth(float lineWidth);
// Get CocosStudio guide lines width.
float getLineWidth();
/**
* When isolated is set, the position of the node is no longer affected by parent nodes.
* Which means it will be drawn just like a root node.
*/
void setIsolated(bool isolated) { _isolated = isolated; }
bool isIsolated() const { return _isolated; }
CC_CONSTRUCTOR_ACCESS:
DrawNode(float lineWidth = DEFAULT_LINE_WIDTH);
virtual ~DrawNode();
virtual bool init() override;
protected:
void ensureCapacity(int count);
void ensureCapacityGLPoint(int count);
void ensureCapacityGLLine(int count);
void updateShader();
void setVertexLayout(CustomCommand& cmd);
void updateBlendState(CustomCommand& cmd);
void updateUniforms(const Mat4 &transform, CustomCommand& cmd);
int _bufferCapacity = 0;
int _bufferCount = 0;
V2F_C4B_T2F *_buffer = nullptr;
int _bufferCapacityGLPoint = 0;
int _bufferCountGLPoint = 0;
V2F_C4B_T2F *_bufferGLPoint = nullptr;
Color4F _pointColor;
int _pointSize = 0;
int _bufferCapacityGLLine = 0;
int _bufferCountGLLine = 0;
V2F_C4B_T2F *_bufferGLLine = nullptr;
BlendFunc _blendFunc;
backend::ProgramState* _programState = nullptr;
backend::ProgramState* _programStatePoint = nullptr;
backend::ProgramState* _programStateLine = nullptr;
CustomCommand _customCommand;
CustomCommand _customCommandGLPoint;
CustomCommand _customCommandGLLine;
bool _dirty = false;
bool _dirtyGLPoint = false;
bool _dirtyGLLine = false;
bool _isolated = false;
float _lineWidth = 0.0f;
float _defaultLineWidth = 0.0f;
private:
CC_DISALLOW_COPY_AND_ASSIGN(DrawNode);
};
/** @} */
NS_CC_END
#endif // __CCDRAWNODES_CCDRAW_NODE_H__