cocos2d-x 画实心圆以及扇形

这篇博客介绍了如何在cocos2d-x中通过修改CCDrawingPrimitives.cpp文件,添加新的方法来实现画实心圆和控制角度的扇形功能。作者指出原始库只提供空心圆的绘制,并分享了自己的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}

使用效果大家自己试下就知道了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值