CCDrawNode.h 13 KB

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