修改cocos2d-x-2.2.5的框架,增加画实心圆的函数ccDrawSolidCircle

cocos2d-x-2.2.5的框架自带的画圆函数ccDrawCircle,默认是空心圆,不能满足项目需求。所以添加了一个画实心圆的函数ccDrawSolidCircle.

只需要修改CCDrawingPrimitives.h/cpp 两个文件:

1.在cocos2dx/draw_nodes/CCDrawingPrimitives.h里面增加2个函数声明:

/** 添加画实心圆的函数 wenke 20140901 **/
void CC_DLL ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY);
void CC_DLL ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter);


2.在cocos2dx/draw_nodes/CCDrawingPrimitives.cpp里面增加2个函数定义:

void CC_DLL ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
{
    ccDrawSolidCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f);
}


void ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY)
{
    lazy_init();
    
    int additionalSegment = 1;
    if (drawLineToCenter)
        additionalSegment++;
    
    const float coef = 2.0f * (float)M_PI/segments;
    
    GLfloat *vertices = (GLfloat*)calloc( sizeof(GLfloat)*2*(segments+2), 1);
    if( ! vertices )
        return;
    
    for(unsigned int i = 0;i <= segments; i++) {
        float rads = i*coef;
        GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
        GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
        
        vertices[i*2] = j;
        vertices[i*2+1] = k;
    }
    vertices[(segments+1)*2] = center.x;
    vertices[(segments+1)*2+1] = center.y;
    
    s_pShader->use();
    s_pShader->setUniformsForBuiltins();
    s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1);
    
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
    
#ifdef EMSCRIPTEN
    setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2));
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0);
#else
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#endif // EMSCRIPTEN
    glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) segments+additionalSegment);
    
    free( vertices );
    
    CC_INCREMENT_GL_DRAWS(1);
}

主要区别就是把 GL_LINE_STRIP  变成了 GL_TRIANGLE_FAN


3.项目中实际使用的时候很简单:

...
//画实心圆,需要修改cocos2dx框架添加此函数声明和定义
ccDrawSolidCircle(_pivot, 8, CC_DEGREES_TO_RADIANS(360), 15, false);
//画空心圆,cocos2dx框架自带
//ccDrawCircle(_pivot, 8, CC_DEGREES_TO_RADIANS(360), 15, false);
...



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值