Cocos2d-x3.3之DrawPrimitivesTest分析

前端之家收集整理的这篇文章主要介绍了Cocos2d-x3.3之DrawPrimitivesTest分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、代码列表


2、VisibleRect类

该类是test-cpp自带工具类

3、HelloWorldScene类

同前面代码

4、DrawPrimitivesDemo类

1).h文件

  1. #include "cocos2d.h"
  2. #include "ui/CocosGUI.h"
  3. #include "VisibleRect.h"
  4. #include "renderer/CCRenderer.h"
  5. #include "renderer/CCCustomCommand.h"
  6.  
  7. USING_NS_CC;
  8. using namespace ui;
  9.  
  10. class DrawPrimitivesDemo : public Scene
  11. {
  12. public:
  13. CREATE_FUNC(DrawPrimitivesDemo);
  14. virtual bool init();
  15. };
  16.  
  17. class BaseTest : public cocos2d::Layer
  18. {
  19. public:
  20. CREATE_FUNC(BaseTest);
  21. std::string title() const;
  22. virtual std::string subtitle() const;
  23. void restartCallback(Ref* sender);//重新执行当前test
  24. void nextCallback(Ref* sender);//下一个test
  25. void backCallback(Ref* sender);//上一个test
  26. virtual bool init();
  27. void menuCloseCallback(cocos2d::Ref* pSender);//关闭菜单回调函数
  28. };
  29.  
  30. class DrawPrimitivesTest : public BaseTest
  31. {
  32. public:
  33. CREATE_FUNC(DrawPrimitivesTest);
  34. virtual bool init();
  35. virtual std::string subtitle() const override;
  36. virtual void draw(Renderer* renderer,const Mat4 &transform,uint32_t flags) override;
  37. protected:
  38. void onDraw(const Mat4 &transform,uint32_t flags);
  39. CustomCommand _customCommand;
  40. };
  41.  
  42. class DrawNodeTest : public BaseTest
  43. {
  44. public:
  45. CREATE_FUNC(DrawNodeTest);
  46. virtual bool init();
  47. virtual std::string subtitle() const override;
  48. };

2).cpp文件

  1. #include "DrawPrimitivesDemo.h"
  2. #include "renderer/CCRenderer.h"
  3. #include "renderer/CCCustomCommand.h"
  4. #define CL(__className__) [](){ return __className__::create();}
  5. static int sceneIdx = -1;
  6.  
  7. typedef Layer* (*NEWDRAWPRIMITIVESFUNC)();
  8. #define DRAWPRIMITIVES_CREATE_FUNC(className) \
  9. static Layer* create##className() \
  10. { return new className(); }
  11.  
  12. static std::function<Layer*()> createFunctions[] =
  13. {
  14. CL(DrawPrimitivesTest),CL(DrawNodeTest),};
  15.  
  16. #define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
  17.  
  18. static Layer* nextAction()
  19. {
  20. sceneIdx++;
  21. sceneIdx = sceneIdx % MAX_LAYER;
  22. auto layer = (createFunctions[sceneIdx])();
  23. // layer->autorelease();
  24. return layer;
  25. }
  26.  
  27. static Layer* backAction()
  28. {
  29. sceneIdx--;
  30. int total = MAX_LAYER;
  31. if( sceneIdx < 0 )
  32. sceneIdx += total;
  33. auto layer = (createFunctions[sceneIdx])();
  34. // layer->autorelease();
  35. return layer;
  36. }
  37.  
  38. static Layer* restartAction()
  39. {
  40. auto layer = (createFunctions[sceneIdx])();
  41. // layer->autorelease();
  42. return layer;
  43. }
  44.  
  45. bool BaseTest::init()
  46. {
  47. bool bRet = false;
  48. do{
  49. CC_BREAK_IF(!Layer::init());
  50. Size visibleSize = Director::getInstance()->getVisibleSize();
  51. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  52. /////////////////////////////
  53. // 2. add a menu item with "X" image,which is clicked to quit the program
  54. // you may modify it.
  55. // add a "close" icon to exit the progress. it's an autorelease object
  56. auto closeItem = MenuItemImage::create(
  57. "CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(BaseTest::menuCloseCallback,this));
  58. closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + visibleSize.height - closeItem->getContentSize().height/2));
  59. // create menu,it's an autorelease object
  60. auto menu1 = Menu::create(closeItem,NULL);
  61. menu1->setPosition(Vec2::ZERO);
  62. this->addChild(menu1,1);
  63. std::string str = title();
  64. const char * pTitle = str.c_str();
  65. TTFConfig ttfConfig("tahoma.ttf",35);
  66. auto label = Label::createWithTTF(ttfConfig,pTitle);
  67. addChild(label,9999);
  68. label->setPosition( Vec2(VisibleRect::center().x,VisibleRect::top().y - 30) );
  69. std::string strSubtitle = subtitle();
  70. if( ! strSubtitle.empty() )
  71. {
  72. ttfConfig.fontFilePath = "tahoma.ttf";
  73. ttfConfig.fontSize = 30;
  74. auto l = Label::createWithTTF(ttfConfig,strSubtitle.c_str());
  75. addChild(l,9999);
  76. l->setPosition( Vec2(VisibleRect::center().x,VisibleRect::top().y - 100) );
  77. }
  78. auto item1 = MenuItemFont::create("backCallback",CC_CALLBACK_1(BaseTest::backCallback,this) );
  79. auto item2 = MenuItemFont::create("restartCallback",CC_CALLBACK_1(BaseTest::restartCallback,this) );
  80. auto item3 = MenuItemFont::create("nextCallback",CC_CALLBACK_1(BaseTest::nextCallback,this) );
  81. auto menu = Menu::create(item1,item2,item3,NULL);
  82. menu->setPosition(Vec2::ZERO);
  83. item1->setPosition(Vec2(VisibleRect::center().x - item2->getContentSize().width*2,VisibleRect::bottom().y+item2->getContentSize().height/2));
  84. item2->setPosition(Vec2(VisibleRect::center().x,VisibleRect::bottom().y+item2->getContentSize().height/2));
  85. item3->setPosition(Vec2(VisibleRect::center().x + item2->getContentSize().width*2,VisibleRect::bottom().y+item2->getContentSize().height/2));
  86. addChild(menu,9999);
  87.  
  88. bRet = true;
  89. }while(0);
  90. return bRet;
  91. }
  92.  
  93. void BaseTest::menuCloseCallback(Ref* pSender)
  94. {
  95. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  96. MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  97. return;
  98. #endif
  99. Director::getInstance()->end();
  100. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  101. exit(0);
  102. #endif
  103. }
  104.  
  105. void BaseTest::restartCallback(cocos2d::Ref *sender)
  106. {
  107. auto s = new (std::nothrow) DrawPrimitivesDemo();
  108. s->addChild(restartAction());
  109. Director::getInstance()->replaceScene(s);
  110. s->release();
  111. }
  112.  
  113. void BaseTest::nextCallback(cocos2d::Ref *sender)
  114. {
  115. auto s = new (std::nothrow) DrawPrimitivesDemo();
  116. s->addChild(nextAction());
  117. Director::getInstance()->replaceScene(s);
  118. s->release();
  119. }
  120.  
  121. void BaseTest::backCallback(cocos2d::Ref *sender)
  122. {
  123. auto s = new (std::nothrow) DrawPrimitivesDemo();
  124. s->addChild(backAction());
  125. Director::getInstance()->replaceScene(s);
  126. s->release();
  127. }
  128.  
  129. std::string BaseTest::title() const
  130. {
  131. return "DrawPrimitives Test";
  132. }
  133.  
  134. std::string BaseTest::subtitle() const
  135. {
  136. return "";
  137. }
  138.  
  139. bool DrawPrimitivesDemo::init()
  140. {
  141. bool bRet = false;
  142. do{
  143. CC_BREAK_IF(!Scene::init());
  144. auto layer = nextAction();
  145. addChild(layer);
  146. Director::getInstance()->replaceScene(this);
  147. bRet = true;
  148. }while(0);
  149. return bRet;
  150. }
  151.  
  152. bool DrawPrimitivesTest::init()
  153. {
  154. bool bRet = false;
  155. do{
  156. CC_BREAK_IF(!BaseTest::init());
  157. bRet = true;
  158. }while(0);
  159. return bRet;
  160. }
  161.  
  162. void DrawPrimitivesTest::draw(cocos2d::Renderer *renderer,const cocos2d::Mat4 &transform,uint32_t flags)
  163. {
  164. //重写draw函数,回调onDraw函数
  165. _customCommand.init(_globalZOrder);
  166. _customCommand.func = CC_CALLBACK_0(DrawPrimitivesTest::onDraw,this,transform,flags);
  167. renderer->addCommand(&_customCommand);
  168. }
  169.  
  170. void DrawPrimitivesTest::onDraw(const cocos2d::Mat4 &transform,uint32_t flags)
  171. {
  172. //初始化
  173. Director* director = Director::getInstance();
  174. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  175. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW,transform);
  176.  
  177. //画线
  178. CHECK_GL_ERROR_DEBUG();
  179. DrawPrimitives::drawLine(VisibleRect::leftBottom(),VisibleRect::rightTop());//参数是2个Vec2对象
  180.  
  181. //画线
  182. CHECK_GL_ERROR_DEBUG();
  183. glLineWidth(5.0f);//设置线条宽度
  184. DrawPrimitives::setDrawColor4B(255,255);//设置颜色
  185. DrawPrimitives::drawLine(VisibleRect::leftTop(),VisibleRect::rightBottom());
  186. //画点
  187. CHECK_GL_ERROR_DEBUG();
  188. DrawPrimitives::setPointSize(64);//设置点大小
  189. DrawPrimitives::setDrawColor4B(0,255,255);//设置颜色
  190. DrawPrimitives::drawPoint(VisibleRect::center());//参数Vec2对象,表示圆心的位置
  191. //画多个点
  192. CHECK_GL_ERROR_DEBUG();
  193. Vec2 points[] = {Vec2(60,160),Vec2(70,170),Vec2(60,160)};
  194. DrawPrimitives::setPointSize(4);
  195. DrawPrimitives::setDrawColor4B(0,255);
  196. DrawPrimitives::drawPoints(points,4);//参数依次为圆心Vec2和点的个数
  197. //画圆圈
  198. CHECK_GL_ERROR_DEBUG();
  199. glLineWidth(16);
  200. DrawPrimitives::setDrawColor4B(0,255);
  201. DrawPrimitives::drawCircle(VisibleRect::center(),100,10,false);//参数依次为:圆心、半径、角度、段数、是否画出一条半径
  202. //画圆圈
  203. CHECK_GL_ERROR_DEBUG();
  204. glLineWidth(2);
  205. DrawPrimitives::setDrawColor4B(0,50,CC_DEGREES_TO_RADIANS(90),true);
  206. //画实心圆
  207. CHECK_GL_ERROR_DEBUG();
  208. glLineWidth(2);//线条宽度
  209. DrawPrimitives::setDrawColor4B(255,255);//线条颜色
  210. //参数依次为:圆心、半径、角度、段数、X轴缩放系数、Y轴缩放系数
  211. DrawPrimitives::drawSolidCircle(VisibleRect::center() + Vec2(140,0),40,1.0f,1.0f);
  212. //画多边形图形,不闭合
  213. CHECK_GL_ERROR_DEBUG();
  214. glLineWidth(10);
  215. Vec2 vertices[] = {Vec2(0,Vec2(50,50),Vec2(100,100),100)};
  216. DrawPrimitives::drawPoly(vertices,5,false);//参数依次为多边形定点、定点数、是否闭合
  217.  
  218. //绘制填充多边形
  219. CHECK_GL_ERROR_DEBUG();
  220. glLineWidth(1);
  221. Vec2 filledVertices[] = {Vec2(0,120),Vec2(25,200),Vec2(0,170)};
  222. DrawPrimitives::drawSolidPoly(filledVertices,Color4F(0.5f,0.5f,1,1));//参数依次为定点、顶点个数、填充颜色
  223.  
  224. //绘制多边形,闭合
  225. DrawPrimitives::setDrawColor4B(255,255);
  226. glLineWidth(2);
  227. Vec2 vertices2[] = {Vec2(90,330),Vec2(90,530),Vec2(150,500)};
  228. DrawPrimitives::drawPoly(vertices2,3,true);
  229. //绘制贝塞尔曲线
  230. CHECK_GL_ERROR_DEBUG();
  231. //参数依次为起点、控制点、终点、段数
  232. DrawPrimitives::drawQuadBezier(VisibleRect::leftTop(),VisibleRect::center(),VisibleRect::rightTop(),50);
  233. //画贝塞尔曲线
  234. CHECK_GL_ERROR_DEBUG();
  235. //参数依次为起点、控制点1、控制点2、终点、段数
  236. DrawPrimitives::drawCubicBezier(VisibleRect::center(),Vec2(VisibleRect::center().x + 30,VisibleRect::center().y + 50),Vec2(VisibleRect::center().x + 60,VisibleRect::center().y - 50),VisibleRect::right(),100);
  237. //画填充闭合曲线
  238. CHECK_GL_ERROR_DEBUG();
  239. Vec2 vertices3[] = {Vec2(90,190),Vec2(140,160)};
  240. DrawPrimitives::drawSolidPoly(vertices3,4,Color4F(1,1));//参数依次为顶点、顶点个数、填充颜色
  241. glLineWidth(1);
  242. DrawPrimitives::setDrawColor4B(255,255);
  243. DrawPrimitives::setPointSize(1);
  244. CHECK_GL_ERROR_DEBUG();
  245. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  246. }
  247.  
  248. std::string DrawPrimitivesTest::subtitle() const
  249. {
  250. return "draw primitives";
  251. }
  252.  
  253. bool DrawNodeTest::init()
  254. {
  255. bool bRet = false;
  256. do{
  257. CC_BREAK_IF(!BaseTest::init());
  258. auto s = Director::getInstance()->getWinSize();
  259. //add DrawNode
  260. auto draw = DrawNode::create();
  261. addChild(draw,10);
  262. //draw point,参数依次为位置、Size、填充颜色
  263. draw->drawPoint(Vec2(s.width/2 - 120,s.height/2 - 120),Color4F(CCRANDOM_0_1(),CCRANDOM_0_1(),1));
  264. draw->drawPoint(Vec2(s.width/2 + 120,s.height/2 + 120),1));
  265. //draw points,同上
  266. Vec2 position[] = {Vec2(60,60),70),60)};
  267. draw->drawPoints(position,1));
  268. //draw a line,参数依次为起点、终点、颜色
  269. draw->drawLine(Vec2(0,Vec2(s.width,s.height),Color4F(1.0,0.0,0.5));
  270. //draw a rectangle,绘制剪裁点,参数为剪裁起点、终点、颜色
  271. draw->drawRect(Vec2(23,123),Vec2(7,1));
  272. //draw circle,绘制圆圈,参数依次为:圆心、半径、角度、段数、是否绘制一条半径、X轴缩放、Y轴缩放、颜色
  273. draw->drawCircle(VisibleRect::center() + Vec2(140,true,2.0f,0.5));
  274. draw->drawCircle(VisibleRect::center() - Vec2(140,30,false,1));
  275. //Draw some beziers,参数依次为:起点、控制点、终点、段数、颜色
  276. draw->drawQuadBezier(Vec2(s.width - 150,s.height - 150),Vec2(s.width - 70,s.height - 10),Vec2(s.width - 10,0.5));
  277. draw->drawQuadBezier(Vec2(0,Vec2(s.width/2,s.height/2),0.5));
  278. //参数依次为:起点、控制点1、控制点2、终点、段数、颜色
  279. draw->drawCubicBezier(VisibleRect::center(),Vec2(VisibleRect::center().x+30,VisibleRect::center().y+50),Vec2(VisibleRect::center().x+60,VisibleRect::center().y-50),0.5));
  280. draw->drawCubicBezier(Vec2(s.width - 250,40),Vec2(s.width - 30,250),s.height - 50),0.5));
  281. auto array = PointArray::create(20);
  282. array->addControlPoint(Vec2(0,0));
  283. array->addControlPoint(Vec2(80,80));
  284. array->addControlPoint(Vec2(s.width - 80,s.height - 80));
  285. array->addControlPoint(Vec2(80,80));
  286. array->addControlPoint(Vec2(s.width/2,s.height/2));
  287. //绘制基本线条,参数依次为:点数组、张力、段数、颜色
  288. draw->drawCardinalSpline(array,0.5,0.5));
  289.  
  290. auto array2 = PointArray::create(20);
  291. array2->addControlPoint(Vec2(s.width/2,30));
  292. array2->addControlPoint(Vec2(s.width - 80,s.height - 80));
  293. array2->addControlPoint(Vec2(s.width/2,30));
  294. draw->drawCatmullRom(array2,0.5));
  295.  
  296. //open random color poly,绘制多边形,不封闭
  297. Vec2 vertices[] = {Vec2(0,100)};
  298. draw->drawPoly(vertices,1));
  299. //closed random color poly,绘制多边形,封闭
  300. Vec2 vertices2[] = {Vec2(30,130),Vec2(30,230),200)};
  301. draw->drawPoly(vertices2,1));
  302.  
  303. //Draw 10 Circles,绘制圆圈
  304. for(int i = 0; i < 10; i++)
  305. {
  306. //参数依次为:圆心、半径、颜色
  307. draw->drawDot(Vec2(s.width/2,10*(10-i),1));
  308. }
  309. //Draw polygons绘制闭合多边形,参数依次是顶点、顶点个数、填充颜色、线条宽度、线条颜色
  310. Vec2 points[] = { Vec2(s.height/4,s.height/5),Vec2(s.width/3*2,s.height) };
  311. // draw->drawPolygon(points,sizeof(points) / sizeof(points[0]),0.5),Color4F(0,0.5));
  312.  
  313. //star poly (triggers buggs)绘制多边形图形
  314. {
  315. const float o = 80;
  316. const float w = 20;
  317. const float h = 50;
  318. Vec2 star[] = {Vec2(o+w,o-h),Vec2(o+w*2,o),Vec2(o+w*2+h,o+w),o+w*2)};
  319. //参数依次为:顶点、顶点个数、填充颜色、线条宽度、线条颜色
  320. draw->drawPolygon(star,sizeof(star) / sizeof(star[0]),1));
  321. }
  322. //绘制多边形
  323. //star poly(doesn't trigger bug...order is important un tesselation is supported.
  324. {
  325. const float o=180;
  326. const float w=20;
  327. const float h=50;
  328. Vec2 star[] = {
  329. Vec2(o,Vec2(o+w,// lower spike
  330. Vec2(o + w*2 + h,o+w ),Vec2(o + w*2,o+w*2),// right spike
  331. Vec2(o +w,o+w*2+h),Vec2(o,// top spike
  332. Vec2(o -h,}; // left spike
  333.  
  334. draw->drawPolygon(star,sizeof(star)/sizeof(star[0]),1));
  335. }
  336.  
  337. //draw a solid polygon,绘制填充多边形
  338. Vec2 vertices3[] = {Vec2(60,160)};
  339. draw->drawSolidPoly(vertices3,1));//参数依次为:顶点、顶点个数、填充颜色
  340. //draw a solid rectangle//绘制剪裁图形,参数依次为:起点、终点、颜色
  341. draw->drawSolidRect(Vec2(10,10),Vec2(20,20),1));
  342. //draw a solid circle绘制圆圈,参数依次为:终点、半径、角度、段数、X轴缩放、y轴缩放、颜色
  343. draw->drawSolidCircle(VisibleRect::center() + Vec2(140,1));
  344.  
  345. //draw segment绘制线段,参数依次为:起点、终点、半径、颜色
  346. draw->drawSegment(Vec2(20,s.height / 2),1));
  347. draw->drawSegment(Vec2(10,0.5));
  348. //draw triangle绘制三角形
  349. draw->drawTriangle(Vec2(10,30),140),0.5));
  350. bRet = true;
  351. }while(0);
  352. return bRet;
  353. }
  354.  
  355. std::string DrawNodeTest::subtitle() const
  356. {
  357. return "Draw Node test";
  358. }

5、效果


猜你在找的Cocos2d-x相关文章