Cocos2d-x 图形绘制

来源:互联网 发布:淘宝双十一英文介绍 编辑:程序博客网 时间:2024/05/04 13:46

转自:http://blog.csdn.net/zhy_cheng/article/details/8480048

 

图形绘制的话,在cocos2d-x自带的TestCpp里有,包括绘制点,直线,多边形(填充的和没有填充的),圆,贝赛尔曲线。

首先在HelloWorld类中重写父类的draw方法

[cpp] view plaincopyprint?
  1. virtual void draw();


在源文件中将init中的类容删去,应为init方法比draw先执行,会覆盖我们画出的效果。删除之后,init方法如下

[cpp] view plaincopyprint?
  1. bool HelloWorld::init()
  2. {
  3. bool bRet = false;
  4. do
  5. {
  6. CC_BREAK_IF(! CCLayer::init());
  7. bRet = true;
  8. } while (0);
  9. return bRet;
  10. }


然后在draw方法中写如下代码:

[cpp] view plaincopyprint?
  1. void HelloWorld::draw()
  2. {
  3. CHECK_GL_ERROR_DEBUG();
  4. /*
  5. 画一条直线,默认的宽度是1,颜色是白色,不透明,glEnable(GL_LINE_SMOOTH);
  6. 默认的情况下是后面不再设置颜色后线宽
  7. */
  8. glLineWidth( 1.0f );
  9. ccDrawColor4B(255,255,255,255);
  10. ccDrawLine(ccp(0,0),ccp(480,320));
  11. CHECK_GL_ERROR_DEBUG();
  12. glLineWidth( 5.0f );
  13. ccDrawColor4B(255,0,0,255);
  14. ccDrawLine(ccp(0,320), ccp(480,0));
  15. CHECK_GL_ERROR_DEBUG();
  16. //ccPointSize(128);这个没用啊
  17. ccDrawColor4B(0,255,255,128);
  18. ccDrawPoint( ccp(240,200) );
  19. CHECK_GL_ERROR_DEBUG();
  20. // 4个点一起画
  21. CCPoint points[] = { ccp(60,60), ccp(70,70), ccp(60,70), ccp(70,60) };
  22. ccPointSize(4);
  23. ccDrawColor4B(0,255,255,255);
  24. ccDrawPoints( points, 4);
  25. // draw a green circle with 10 segments
  26. glLineWidth(1);
  27. ccDrawColor4B(0, 255, 0, 255);
  28. ccDrawCircle( ccp(240,160),//圆心
  29. 100,//半径
  30. 1, //如果后面设置了从圆心到圆的连线为true的话,
  31. //这个值是连线的角度,0为水平向左,逆时针
  32. 360,//将这个圆分为多少块
  33. true//是否有从圆心到圆的连线
  34. );
  35. //画一个多边形
  36. ccDrawColor4B(255, 255, 0, 255);
  37. glLineWidth(1);
  38. CCPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
  39. ccDrawPoly( vertices, 5, true//是否封闭
  40. );
  41. // 填充的多边形
  42. glLineWidth(1);
  43. CCPoint filledVertices[] = { ccp(0,120), ccp(50,120), ccp(50,170), ccp(25,200), ccp(0,170) };
  44. ccDrawSolidPoly(filledVertices, 5, ccc4f(0.5f, 0.5f, 1, 1 )//填充颜色
  45. );
  46. //贝塞尔曲线
  47. ccDrawColor4B(255, 255, 0, 255);
  48. ccDrawCubicBezier(ccp(0,0),//开始点
  49. ccp(50,50),//控制点
  50. ccp(250,100),//控制点
  51. ccp(300,300),//目标点
  52. 100//分成多少段得到的曲线
  53. );
  54. // 还原默认值
  55. glLineWidth(1);
  56. ccDrawColor4B(255,255,255,255);
  57. ccPointSize(1);
  58. }


在代码中的注释解释的很清楚,下面来一张效果图:

源代码的话,那就没有必要上传了,代码我都贴出来了。

 

原创粉丝点击