Play cocos2d-x 34 drawing: CCDrawingPrimitives and CCDrawNode

Source: Internet
Author: User

Original works, reproduced please mark: http://blog.csdn.net/jackystudio/article/details/18978237


I have been busy recently and have been worried about this year. I haven't come to update the blog for a long time, today I will write a write cocos2d-x Drawing Graphics.


1. Overview

In fact, cocos2d-x encapsulates a lot of opengl plotting functions, we can easily call in the game. But in fact, we seldom do this, because the efficiency of coding plotting is too low, especially for some higher-order besell curves, which are not officially recommended, the use of images can improve the drawing efficiency more effectively and make the image more beautiful. It also reduces the issue of Sprite occlusion and edge aliasing. There are two main ways of drawing in the cocos2d-x: CCDrawingPrimitives and CCDrawNode. They are all in the cocos2dx \ draw_nodes directory.


2. CCDrawingPrimitives (native plotting)


2. Features

CCDrawingPrimitives must be called in the draw function of the node. The draw function is called once every frame, mainly for drawing. But this does not mean that it is static and cannot be changed. We can modify the descriptive content to dynamically draw. For example, you can draw images on the screen by using a touch.

In addition, the default Z-Order for drawing with CCDrawingPrimitives is 0, so it may be overwritten by other nodes because it cannot set Z-Order, therefore, you can only set the Z-Order of other nodes to a negative value so that the drawing can be seen at the frontend.


2.2.API

[Cpp]View plaincopy
  1. // Draw a point with the given x and y values
  2. Void CC_DLL ccDrawPoint (const CCPoint & point );
  3. // Draw a group of points
  4. Void CC_DLL ccDrawPoints (const CCPoint * points, unsigned int numberOfPoints );
  5. // Draw a straight line with a given start point and target point
  6. Void CC_DLL ccDrawLine (const CCPoint & origin, const CCPoint & destination );
  7. // Draw a rectangle with a given start point and target point
  8. Void CC_DLL ccDrawRect (CCPoint origin, CCPoint destination );
  9. // Draw a rectangle with a given starting point and destination point and color
  10. Void CC_DLL ccDrawSolidRect (CCPoint origin, CCPoint destination, ccColor4F color );
  11. // Draw a polygon with a given set of points, which can be closed or open
  12. Void CC_DLL ccDrawPoly (const CCPoint * vertices, unsigned int numOfVertices, bool closePolygon );
  13. // Draw a polygon with a given set of points and colors
  14. Void CC_DLL ccDrawSolidPoly (const CCPoint * poli, unsigned int numberOfPoints, ccColor4F color );
  15. // Draw a circle with a given center, radius, and number of segments
  16. Void CC_DLL ccDrawCircle (const CCPoint & center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY );
  17. Void CC_DLL ccDrawCircle (const CCPoint & center, float radius, float angle, unsigned int segments, bool drawLineToCenter );
  18. // Draw a Bessert curve with a control point (secondary)
  19. Void CC_DLL (const CCPoint & origin, const CCPoint & control, const CCPoint & destination, unsigned int segments );
  20. // Draw a besell curve with two control points (Higher Order)
  21. Void CC_DLL ccdrawcubicbezr (const CCPoint & origin, const CCPoint & control1, const CCPoint & control2, const CCPoint & destination, unsigned int segments );
  22. // Plot the base spline with default Curvature
  23. Void CC_DLL ccDrawCatmullRom (CCPointArray * arrayOfControlPoints, unsigned int segments );
  24. // Draw a base spline with the specified Curvature
  25. Void CC_DLL ccDrawCardinalSpline (CCPointArray * config, float tension, unsigned int segments );
  26. // Set the painting color, 4 bytes
  27. Void CC_DLL ccDrawColor4B (GLubyte r, GLubyte g, GLubyte B, GLubyte );
  28. // Set the painting color, 4 float Type
  29. Void CC_DLL ccDrawColor4F (GLfloat r, GLfloat g, GLfloat B, GLfloat );
  30. // Set the vertex size. The default value is 1.
  31. Void CC_DLL ccPointSize (GLfloat pointSize );

2. 3. Example

The Code uses the code in TestCpp.



3. CCDrawNode (drawing node)


3. 1. Features

CCDrawNode inherits from CCNode and is a node class for Drawing Images. It provides several common dot/line functions to facilitate plotting. In fact, it has been written in the previous article on game masks. Compared with CCDrawingPrimitives, CCDrawNode is recommended,

(1) It encapsulates the plot as a common node for processing, in line with the idea of cocos2d-x tree programming, but also to avoid some of the problem of covering.

(2) do not need to be called in the draw function.

(3) Use batch plot to improve the drawing efficiency.


3.2.API

[Cpp]View plaincopy
  1. // Draw a point of the specified position, size, and color
  2. Void drawDot (const CCPoint & pos, float radius, const ccColor4F & color );
  3. // Draw a point of the specified start point, size, and color.
  4. Void drawSegment (const CCPoint & from, const CCPoint & to, float radius, const ccColor4F & color );
  5. // Draw a polygon with the specified vertex, fill color, border color, and Border Width
  6. Void drawPolygon (CCPoint * verts, unsigned int count, const ccColor4F & fillColor, float borderWidth, const ccColor4F & borderColor );

3. Example

The Code is also the code in TestCpp.




4. Download source code

You can see the TestCpp example, you can also download from here: http://download.csdn.net/detail/jackyvincefu/6903357

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.