CCDrawNode.h 13 KB

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