cocos2d-x本身提供了画空心圆形的方法,但没有实心圆的方法。在网上找了一下,发现都是从CCDrawingPrimitives.cpp中的ccDrawCircle方法修改而来。虽然实现了实心圆但却不能控制圆形的角度,即只能画圆形不能画扇形。
所以我自己进行了一些修改,总体没有变化,但是添加了实现画实心扇形的功能。
1.首先是函数声明,在CCDrawingPrimitives.h添加方法:
/** draws a solid circle given the center, radius and number of segments. */
void CC_DLL ccDrawSolidCircle(const CCPoint& center, float radius, //中心点及半径
float startDegree, //起始角度,x轴正方向为0度
float degree, //持续角度,逆时针为增长正方向
int segments, //组成圆形的扇形个数
float scaleX, float scaleY);
起始角度startDegree和持续角度degree均可正可负。
2.接下来时函数实现,在CCDrawingPrimitives.cpp添加:
void CC_DLL ccDrawSolidCircle(const CCPoint& center, float radius, float startDegree, float degree, int segments, float scaleX, float scaleY)
{
if (degree < 0.0) {
startDegree += degree;
degree *= -1.0;
}
float startRadians = CC_DEGREES_TO_RADIANS(startDegree);
float endRadians = CC_DEGREES_TO_RADIANS(startDegree+degree);
lazy_init();
const float coef = 2.0f * (float)M_PI/segments;
GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
if( ! vertices )
return;
vertices[0] = center.x;
vertices[1] = center.y;
int used_segment=1; //使用的segment
for(unsigned int i = 0;i <= segments; i++) {
float rads = startRadians + i*coef;
if (rads > endRadians) {
break;
}
GLfloat j = radius * cosf(rads/* + angle*/) * scaleX + center.x;
GLfloat k = radius * sinf(rads/* + angle*/) * scaleY + center.y;
vertices[used_segment*2] = j;
vertices[used_segment*2+1] = k;
used_segment++;
}
s_pShader->use();
s_pShader->setUniformForModelViewProjectionMatrix();
s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1);
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_TRIANGLE_FAN/*GL_LINE_STRIP*/, 0, (GLsizei) used_segment/*+additionalSegment*/);
free( vertices );
CC_INCREMENT_GL_DRAWS(1);
}
使用效果大家自己试下就知道了。