【Cocos2d-X开发学习笔记】第10期:渲染框架之几何图形的绘制

来源:互联网 发布:数控车床滚花编程 编辑:程序博客网 时间:2024/05/17 02:11

http://blog.csdn.net/yangyu20121224/article/details/9748001


一、图形的绘制

 

      有时为了调试游戏,我们常常会需要绘制一些简单的几何图形,比如点、直线或者圆圈。这些几何图形颜色颜色

简单,样式单一,很少出现在游戏画面中,Cocos2D-X引擎提供了一个绘制几何图形的类CCDrawingPrimitives。我

们可以使用它来绘制几何图形,用于调试游戏画面。

 

注意:因为类CCDrawingPrimitives纯粹是为了调试游戏而存在的,所以我们在技术文档中找不到此类说明。

 

1、常用绘制图形函数如下。

 

<1> ccDrawColor4B(GLubyte r,GLubyte g,GLubyte b,GLubyte a)

作用:设置绘制颜色。

参数1:红色分量。

参数2:绿色分量。

参数3:蓝色分量。

参数4:透明度。

 

<2> glLineWidth(GLfloat width)

作用:设置线条宽度。

参数:宽度值。

 

<3> ccDrawLine(const CCPoint& origin,const CCPoint& destination)

作用:绘制一条直线。

参数1:起始坐标。

参数2:终点坐标。

 

<4> ccPointSize(GLfloat pointSize)

作用:设置每个点的大小。

参数:点的大小。

 

<5> ccDrawPoint(const CCPoint& point)

作用:绘制一个点。

参数:点的坐标。

 

<6> ccDrawCircle(const CCPoint& center,float radius,float angle,unsigned int segments,bool drawLineToCenter)

作用:绘制圆形。

参数1:中心点坐标。

参数2:半径长度。

参数3:圆形的角度。

参数4:角度。

参数5:定点数。

 

<7> ccDrawPoly(const CCPoint * poli,unsigned int numberOfPoints,bool closePolygon)

作用:绘制空心多边形。

参数1:顶点数组。

参数2:点数量。

参数3:是否自动封闭多边形。

 

<8> ccDrawSolidRect(CCPoint origin,CCPoint destination,ccColor4F color)

作用:绘制填充的矩形。

参数1:顶点数组。

参数2:点数量。

参数3:矩形的颜色。

 

<9> ccDrawRect(CCPoint origin,CCPoint destination)

作用:绘制空心的矩形。

参数1:顶点数组。

参数2:点数量。

 

<10> ccDrawQuadBezier(const CCPoint& origin,const CCPoint& control,const CCPoint& destination,unsigned int segments)

作用:绘制贝塞尔曲线。

参数1:起始点。

参数2:控制点。

参数3:结束点。

参数4:顶点数。

 

<11> ccDrawCubicBezier(conts CCPoint& origin,const CCPoint& control1,const CCPoint& control2,const CCPoint& destination,unsigned int segments)

作用:绘制立体贝塞尔曲线。

参数1:起始点。

参数2:控制点1。

参数3:控制点2.

参数4:结束点。

参数5:顶点数。

 

2、示例代码如下。

 

<1> 首先新建Cocos2D-X项目,在HelloWorldScene.h头文件中重写draw函数:

[cpp] view plaincopy
  1. //重写draw函数  
  2.     virtual void draw();  


<2> 然后在HelloWorldScene.cpp文件中实现了draw函数:

[cpp] view plaincopy
  1. void HelloWorld::draw(){  
  2.     CCSize s = CCDirector::sharedDirector()->getWinSize();  
  3.     //设置颜色  
  4.     ccDrawColor4B(255,0,0,255);  
  5.     //设置线的宽度  
  6.     glLineWidth(2);  
  7.     //绘制一条直线  
  8.     ccDrawLine( ccp(0, 0), ccp(s.width*0.5, s.height*0.5) );  
  9.       
  10.     ccDrawColor4B(255,255,0,0);  
  11.     //设置像素尺寸  
  12.     ccPointSize(30);  
  13.     //绘制一个点  
  14.     ccDrawPoint( ccp(s.width*0.5, s.height *0.5) );  
  15.       
  16.     ccDrawColor4B(0,0,255,0);  
  17.     //绘制圆形  
  18.     ccDrawCircle( ccp(s.width/2, s.height/2), 50, CC_DEGREES_TO_RADIANS(90), 50, false);  
  19.     ccDrawCircle( ccp(s.width/2, s.height/2), 50, CC_DEGREES_TO_RADIANS(45), 6, false);  
  20.       
  21.     ccDrawColor4B(0, 255, 255, 255);  
  22.     glLineWidth(5);  
  23.     //绘制多边形  
  24.     CCPoint vertices[] = { ccp(70,150), ccp(150,150), ccp(150,200),ccp(190,300)  };  
  25.     ccDrawPoly( vertices, 4, true);  
  26.       
  27.     ccDrawColor4B(255, 0, 255, 255);  
  28.     //绘制填充的多边形  
  29.     CCPoint filledVertices[] = { ccp(0,120), ccp(50,120), ccp(50,170), ccp(25,200), ccp(0,170) };  
  30.     ccDrawSolidPoly(filledVertices, 5, ccc4f(0.5f, 0.5f, 1, 1 ) );  
  31. //    ccDrawPoly(<#const cocos2d::CCPoint *vertices#>, <#unsigned int numOfVertices#>, <#bool closePolygon#>)  
  32.       
  33.     //绘制贝塞尔曲线  
  34.     ccDrawQuadBezier(ccp(0,s.height), ccp(s.width/2,s.height/2), ccp(s.width,s.height), 50);  
  35.       
  36.     //绘制立体的贝塞尔曲线  
  37.     // draw cubic bezier path  
  38.     ccDrawCubicBezier(ccp(s.width/2, s.height/2), ccp(s.width/2+30,s.height/2+50), ccp(s.width/2+60,s.height/2-50),ccp(s.width, s.height/2),100);  
  39.       
  40.     //绘制填充举行  
  41.     ccDrawSolidRect(ccp(240,50), ccp(300,10), ccc4f(255, 177, 177, 255));  
  42.     //绘制空心矩形  
  43. //    ccDrawRect(<#cocos2d::CCPoint origin#>, <#cocos2d::CCPoint destination#>)  
  44.       
  45. }  
0 0
原创粉丝点击