Cocos2d-x3.3-based DrawPrimitivesTest analysis, drawprimitivestest
1. Code List
2. VisibleRect class
This class is the tool class that comes with test-cpp.
3. HelloWorldScene class
Same as the previous Code
4. DrawPrimitivesDemo Class 1). h file
# Include "cocos2d. h "# include" ui/CocosGUI. h "# include" VisibleRect. h "# include" renderer/CCRenderer. h "# include" renderer/CCCustomCommand. h "USING_NS_CC; using namespace ui; class DrawPrimitivesDemo: public Scene {public: CREATE_FUNC (DrawPrimitivesDemo); virtual bool init () ;}; class BaseTest: public cocos2d :: Layer {public: CREATE_FUNC (BaseTest); std: string title () const; virtual std: string subtitle () const; void restartCallback (Ref * sender ); // re-execute the current test void nextCallback (Ref * sender); // The next test void backCallback (Ref * sender); // the previous test virtual bool init (); void menuCloseCallback (cocos2d: Ref * pSender); // close the menu callback function}; class DrawPrimitivesTest: public BaseTest {public: CREATE_FUNC (DrawPrimitivesTest); virtual bool init (); virtual std: string subtitle () const override; virtual void draw (Renderer * renderer, const Mat4 & transform, uint32_t flags) override; protected: void onDraw (const Mat4 & transform, uint32_t flags); CustomCommand _ customCommand;}; class DrawNodeTest: public BaseTest {public: CREATE_FUNC (DrawNodeTest); virtual bool init (); virtual std: string subtitle () const override ;};
2). cpp File
# Include "DrawPrimitivesDemo. h "# include" renderer/CCRenderer. h "# include" renderer/CCCustomCommand. h "# define CL (_ className _) [] () {return _ className __:: create ();} static int sceneIdx =-1; typedef Layer * (* NEWDRAWPRIMITIVESFUNC) (); # define DRAWPRIMITIVES_CREATE_FUNC (className) \ static Layer * create ## className () \ {return new className ();} static std :: function <Layer * ()> createFunctions [] = {CL (DrawPr ImitivesTest), CL (DrawNodeTest), };# define MAX_LAYER (sizeof (createFunctions)/sizeof (createFunctions [0]) static Layer * nextAction () {sceneIdx ++; sceneIdx = sceneIdx % MAX_LAYER; auto layer = (createFunctions [sceneIdx]) (); // layer-> autorelease (); return layer;} static Layer * backAction () {sceneIdx --; int total = MAX_LAYER; if (sceneIdx <0) sceneIdx + = total; auto layer = (createFunctions [sceneIdx ]) (); // Layer-> autorelease (); return layer;} static Layer * restartAction () {auto layer = (createFunctions [sceneIdx]) (); // layer-> autorelease (); return layer;} bool BaseTest: init () {bool bRet = false; do {CC_BREAK_IF (! Layer: init (); Size visibleSize = Director: getInstance ()-> getVisibleSize (); Vec2 origin = Director: getInstance ()-> getVisibleOrigin (); /// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage: create ("CloseNormal.png", "CloseS Elected.png ", CC_CALLBACK_1 (BaseTest: menuCloseCallback, this); closeItem-> setPosition (Vec2 (origin. x + visibleSize. width-closeItem-> getContentSize (). width/2, origin. y + visibleSize. height-closeItem-> getContentSize (). height/2); // create menu, it's an autorelease object auto menu1 = Menu: create (closeItem, NULL); menu1-> setPosition (Vec2 :: ZERO); this-> addChild (menu1, 1); std: string str = tit Le (); const char * pTitle = str. c_str (); TTFConfig ttfConfig ("tahoma. ttf ", 35); auto label = Label: createWithTTF (ttfConfig, pTitle); addChild (label, 9999); label-> setPosition (Vec2 (VisibleRect: center (). x, VisibleRect: top (). y-30); std: string strSubtitle = subtitle (); if (! StrSubtitle. empty () {ttfConfig. fontFilePath = "tahoma. ttf "; ttfConfig. fontSize = 30; auto l = Label: createWithTTF (ttfConfig, strSubtitle. c_str (); addChild (l, 9999); l-> setPosition (Vec2 (VisibleRect: center (). x, VisibleRect: top (). y-100);} auto item1 = MenuItemFont: create ("backCallback", CC_CALLBACK_1 (BaseTest: backCallback, this); auto item2 = MenuItemFont :: create ("restartCallback", CC _ CALLBACK_1 (BaseTest: restartCallback, this); auto item3 = MenuItemFont: create ("nextCallback", CC_CALLBACK_1 (BaseTest: nextCallback, this); auto menu = Menu:: create (item1, item2, item3, NULL); menu-> setPosition (Vec2: ZERO); item1-> setPosition (Vec2 (VisibleRect: center (). x-item2-> getContentSize (). width * 2, VisibleRect: bottom (). y + item2-> getContentSize (). height/2); item2-> setPosition (Vec2 (Visi BleRect: center (). x, VisibleRect: bottom (). y + item2-> getContentSize (). height/2); item3-> setPosition (Vec2 (VisibleRect: center (). x + item2-> getContentSize (). width * 2, VisibleRect: bottom (). y + item2-> getContentSize (). height/2); addChild (menu, 9999); bRet = true;} while (0); return bRet;} void BaseTest: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = CC_PLATFORM_WP8) | (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) MessageBox ("You pressed the close button. windows Store Apps do not implement a close button. "," Alert "); return; # endif Director: getInstance ()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0 ); # endif} void BaseTest: restartCallback (cocos2d: Ref * sender) {auto s = new (std: nothrow) DrawPrimitivesDemo (); s-> addChild (restartAction (); Director: getInstance ()-> replaceScene (S); s-> release ();} void BaseTest: nextCallback (cocos2d: Ref * sender) {auto s = new (std: nothrow) DrawPrimitivesDemo (); s-> addChild (nextAction (); ctor: getInstance ()-> replaceScene (s); s-> release ();} void BaseTest: backCallback (cocos2d :: ref * sender) {auto s = new (std: nothrow) DrawPrimitivesDemo (); s-> addChild (backAction (); Director: getInstance () -> replaceScene (s); s-> release ();} std: string B AseTest: title () const {return "DrawPrimitives Test";} std: string BaseTest: subtitle () const {return "";} bool DrawPrimitivesDemo: init () {bool bRet = false; do {CC_BREAK_IF (! Scene: init (); auto layer = nextAction (); addChild (layer); Director: getInstance ()-> replaceScene (this); bRet = true ;} while (0); return bRet;} bool DrawPrimitivesTest: init () {bool bRet = false; do {CC_BREAK_IF (! BaseTest: init (); bRet = true;} while (0); return bRet;} void DrawPrimitivesTest: draw (cocos2d: Renderer * renderer, const cocos2d :: mat4 & transform, uint32_t flags) {// rewrite the draw function and call back the onDraw function _ customCommand. init (_ globalZOrder); _ customCommand. func = CC_CALLBACK_0 (outputs: onDraw, this, transform, flags); renderer-> addCommand (& _ customCommand);} void DrawPrimitivesTest: onDraw (const cocos2d: Mat4 & Transform, uint32_t flags) {// initialize Director * director = Director: getInstance (); director-> pushMatrix (MATRIX_STACK_TYPE: MATRIX_STACK_MODELVIEW); director-> loadMatrix (MATRIX_STACK_TYPE :: MATRIX_STACK_MODELVIEW, transform); // draw line CHECK_GL_ERROR_DEBUG (); DrawPrimitives: drawLine (VisibleRect: leftBottom (), VisibleRect: rightTop ()); // The parameter is two Vec2 objects // draw line CHECK_GL_ERROR_DEBUG (); glLineWidth (5.0f); // set line width Dr AwPrimitives: setDrawColor4B (255, 0, 0,255); // set the color DrawPrimitives: drawLine (VisibleRect: leftTop (), VisibleRect: rightBottom ()); // vertex drawing (); DrawPrimitives: setPointSize (64); // set the vertex size DrawPrimitives: setDrawColor4B (0,255,255,255); // set the color DrawPrimitives: drawPoint (VisibleRect :: center (); // The Vec2 parameter, indicating the center position // draw multiple vertices CHECK_GL_ERROR_DEBUG (); Vec2 points [] = {Vec2 (60,160 ), vec2 (1, 70,170), Ve C2 (60,170), Vec2 (70,160)}; DrawPrimitives: setPointSize (4); DrawPrimitives: setDrawColor4B (0,255,255,255); DrawPrimitives: drawPoints (points, 4 ); // The parameters are the number of centers Vec2 and points in sequence. // draw the circle CHECK_GL_ERROR_DEBUG (); glLineWidth (16); DrawPrimitives: setDrawColor4B (0,255, 0,255); DrawPrimitives :: drawCircle (VisibleRect: center (), 100, 0, 10, false); // The parameters are as follows: circle Center, radius, angle, number of segments, whether to draw a radius // draw a circle CHECK_GL_ERROR_DEBUG (); glLineWidth (2); DrawPrimitives: setDrawColor4B (0,255,255,255); DrawPrimitives: drawCircle (VisibleRect: center (), 50, CC_DEGREES_TO_RADIANS (90), 50, true ); // draw the solid circle CHECK_GL_ERROR_DEBUG (); glLineWidth (2); // line width DrawPrimitives: setDrawColor4B (255, 0,255,255); // line color // The parameters are: circle center, radius, angle, number of segments, X-axis scaling factor, Y-axis scaling factor DrawPrimitives: drawSolidCircle (VisibleRect: center () + Vec2 (), 40, CC_DEGREES_TO_RADIANS (90 ), 50, 1.0f, 1.0f); // draw a polygon image without closing CHECK_GL_ERROR_DEBUG (); glLineWidth (10); Vec2 vertices [] = {Vec2 (), Vec2 ), vec2 (100,100), Vec2 (50,100), Vec2 ()}; DrawPrimitives: drawPoly (vertices, 5, false ); // The parameters are fixed points, fixed points, and whether to close the polygon. // draw the filled polygon CHECK_GL_ERROR_DEBUG (); glLineWidth (1); Vec2 filledVertices [] = {Vec2 (0,120 ), vec2 (50,120), Vec2 (50,170), Vec2 (25,200), Vec2 (0,170)}; DrawPrimitives: drawSolidPoly (filledVertices, 5, Color4F (0.5f, 0.5f, 255); // The parameters are fixed points, number of vertices, fill color in sequence. // draw a polygon and close DrawPrimitives: setDrawColor4B (0,255,255 ); glLineWidth (2); Vec2 vertices2 [] = {Vec2 (90,330), Vec2 (90,530), Vec2 (150,500)}; DrawPrimitives: drawPoly (vertices2, 3, true ); // draw the betel curve CHECK_GL_ERROR_DEBUG (); // The parameters are set to start point, control point, end point, and number of segments. DrawPrimitives: drawquadbeier (VisibleRect: leftTop (), VisibleRect :: center (), VisibleRect: rightTop (), 50); // draw the besell Curve CHECK_GL_ERROR_DEBUG (); // The parameters are set to start point, control point 1, control point 2, end point, number of segments DrawPrimitives: drawcubicbeier (VisibleRect: center (), Vec2 (VisibleRect :: center (). x + 30, VisibleRect: center (). y + 50), Vec2 (VisibleRect: center (). x + 60, VisibleRect: center (). y-50), VisibleRect: right (), 100); // draw the closed curve CHECK_GL_ERROR_DEBUG (); Vec2 vertices3 [] = {Vec2 (90,160 ), vec2 (100,190), Vec2 (150,190), Vec2 (140,160)}; DrawPrimitives: drawSol IdPoly (vertices3, 4, Color4F (255,255,255,255,); // The parameters are vertex, number of vertex, fill color glLineWidth (1); DrawPrimitives: setDrawColor4B ); drawPrimitives: setPointSize (1); CHECK_GL_ERROR_DEBUG (); ctor-> popMatrix (MATRIX_STACK_TYPE: MATRIX_STACK_MODELVIEW);} std: string FIG: subtitle () const {return "draw primitives";} bool DrawNodeTest: init () {bool bRet = false; do {CC_BREAK_IF (! BaseTest: init (); auto s = Director: getInstance ()-> getWinSize (); // add DrawNode auto draw = DrawNode: create (); addChild (draw, 10); // draw point. The parameters are location, Size, and fill color draw-> drawPoint (Vec2 (s. width/2-120, s. height/2-120), 10, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 1); draw-> drawPoint (Vec2 (s. width/2 + 120, s. height/2 + 120), 10, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 1); // draw points, same as Vec2 position [] = {Vec2 (60,60), Vec2 (70,70), Vec2 (60,70), Vec2 (70,60)}; draw-> drawPoints (position, 4, color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 1); // draw a line, the parameters are set to start point, end point, color draw-> drawLine (Vec2 (), and Vec2 (s. width, s. height), Color4F (1.0, 0.0, 0.0, 0.5); // draw a rectangle, draw a cropping point, the parameters are the cropping start point, end point, and color draw-> drawRect (Vec2 (23,123), Vec2 (7,100), Color4F (1, 1, 0, 1); // draw circle, draw a circle with the following parameters: center, radius, angle, number of segments, whether to draw a radius, X-axis scaling, Y-axis scaling, color draw-> drawCircle (VisibleRect: center () + Vec2 (100), 1.0, CC_DEGREES_TO_RADIANS (90), 50, true, 1.0f, 2.0f, Color4F (0.0, 0.0, 0.5,); draw-> drawCircle (VisibleRect:: center ()-Vec2 (140,0), 50, CC_DEGREES_TO_RADIANS (90), 30, false, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 1 )); // Draw some beziers, with the following parameters: Start Point, control point, end point, number of segments, and color draw-> drawquadbezr (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-> drawquadbezr (Vec2 (0, s. height), Vec2 (s. width/2, s. height/2), Vec2 (s. width, s. height), 50, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 0.5); // The parameters are: start Point, control point 1, control point 2, end point, number of segments, color draw-> drawcubicbeier (VisibleRect: center (), Vec2 (VisibleRect: center (). x + 30, VisibleRect: center (). y + 50), Vec2 (VisibleRect: center (). x + 60, VisibleRect: center (). y-50), VisibleRect: right (), 100, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 0.5); draw-> drawcubicbeener (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); auto array = PointArray: create (20 ); array-> addControlPoint (Vec2 (0, 0); array-> addControlPoint (Vec2 (80, 80); array-> addControlPoint (Vec2 (s. width-80, 80); array-> addControlPoint (Vec2 (s. width-80, s. height-80); array-> addControlPoint (Vec2 (80, s. height-80); array-> addControlPoint (Vec2 (80, 80); array-> addControlPoint (Vec2 (s. width/2, s. height/2); // draw the basic line with the following parameters: vertex array, tension, number of segments, color draw-> drawCardinalSpline (array, 0.5, 50, color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 0.5); auto array2 = PointArray: create (20); array2-> addControlPoint (Vec2 (s. width/2, 30); array2-> addControlPoint (Vec2 (s. width-80, 30); array2-> addControlPoint (Vec2 (s. width-80, s. height-80); array2-> addControlPoint (Vec2 (s. width/2, s. height-80); array2-> addControlPoint (Vec2 (s. width/0.5); draw-> drawCatmullRom (array2, 50, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (),); // open random color poly, draw a polygon, without blocking Vec2 vertices [] = {Vec2 (100,100), Vec2 (50, 50), Vec2 (50,100), Vec2 )}; draw-> drawPoly (vertices, 5, false, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 1); // closed random color poly, draw a polygon, close Vec2 vertices2 [] = {Vec2 (30,130), Vec2 (30,230), Vec2 (50,200)}; draw-> drawPoly (vertices2, 3, true, Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 1); // Draw 10 Circles, Draw a circle for (int I = 0; I <10; I ++) {// The parameters are center, radius, and color draw-> drawDot (Vec2 (s. width/2, s. height/2), 10 * (10-i), Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 1);} // Draw polygons draws a closed polygon, the parameters are vertex, vertex number, fill color, line width, and line color. Vec2 points [] = {Vec2 (s. height/4, 0), Vec2 (s. width, s. height/5), Vec2 (s. width/3*2, s. height)}; // draw-> drawPolygon (points, sizeof (points)/sizeof (points [0]), Color4F (0.5, 0,), 4, color4F (0.5, 1,); // star poly (triggers buggs) Draw a polygon image {const float o = 80; const float w = 20; const float h = 50; vec2 star [] = {Vec2 (o + w, o-h), Vec2 (o + w * 2, o), Vec2 (o + w * 2 + h, o + w), Vec2 (o + w * 2, o + w * 2)}; // The parameters are: vertex, vertex number, fill color, line width, line color draw-> drawPolygon (star, sizeof (star)/sizeof (star [0]), Color4F (, 0, 0.5), 1, Color4F (,);} // draw a polygon // star poly (doesn't trigger bug... order is important un tesselation is supported. {const float o = 180; const float w = 20; const float h = 50; Vec2 star [] = {Vec2 (o, o), Vec2 (o + w, o-h), Vec2 (o + w * 2, o), // lower spike Vec2 (o + w * 2 + h, o + w ), vec2 (o + w * 2, o + w * 2), // right spike Vec2 (o + w, o + w * 2 + h), Vec2 (o, o + w * 2), // top spike Vec2 (o-h, o + w),}; // left spike draw-> drawPolygon (star, sizeof (star) /sizeof (star [0]), Color4F (0.5, 0,), 1, Color4F (,);} // draw a solid polygon, draw a filled polygon Vec2 vertices3 [] = {Vec2 (60,160), Vec2 (70,190), Vec2 (100,190), Vec2 (90,160)}; draw-> drawSolidPoly (vertices3, 4, color4F (,); // The parameters are: vertex, number of vertex, fill color // draw a solid rectangle // draw the cropping image. The parameters are as follows: start Point, end point, color draw-> drawSolidRect (Vec2 (10, 10), Vec2 (20, 20), Color4F (,); // draw a circle with draw a solid circle, parameters are as follows: endpoint, radius, angle, number of segments, X-axis scaling, Y-axis scaling, color draw-> drawSolidCircle (VisibleRect: center () + Vec2 (), 40, CC_DEGREES_TO_RADIANS (90), 50, 2.0f, 2.0f, Color4F (,); // draw a line segment with the following parameters: start Point, end point, radius, color draw-> drawSegment (Vec2 (20, s. height), Vec2 (20, s. height/2), 10, Color4F (0, 1); draw-> drawSegment (Vec2 (10, s. height/2), Vec2 (s. width/2, s. height/2), 40, Color4F (0.5, 1,); // draw a triangle draw-> drawTriangle (Vec2 (), Vec2 ), vec2 (100,140), Color4F (CCRANDOM_0_1 (), CCRANDOM_0_1 (), CCRANDOM_0_1 (), 0.5); bRet = true;} while (0); return bRet;} std :: string DrawNodeTest: subtitle () const {return "Draw Node test ";}5,