修改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);
...



你可能感兴趣的:(Cocos2d-x)