Dhiral Pandya
Dhiral Pandya

Reputation: 10619

How to draw circle fill with color in cocos2d-x?

I am new in cocos2d-x, I am trying to draw circle with color inside it. I search on net and i found some code. I tried below code but it only draw circle with color border. My cocos2d-x version is 2.1.5 and I am using it for android. I also tried to change width of border using this method : glLineWidth(2); but this method is not found in my cocos2d-x. How to add color in circle and how to change width of border of circle.

cocos2d::ccDrawColor4B(0, 255, 255, 255);
cocos2d::ccDrawColor4F(0, 255, 255, 255);
cocos2d::ccDrawCircle( ccp(100/2, 100/2), 50, CC_DEGREES_TO_RADIANS(90), 50, false);

Upvotes: 4

Views: 7695

Answers (3)

Austin
Austin

Reputation: 1020

Just add a new method in CCDrawingPrimitives. It is identical to ccDrawCircle except the glDrawArrays uses GL_TRIANGLE_FAN instead of GL_LINE_STRIP

In the header, add:

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

In the .cpp file, add:

 void ccDrawSolidCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter)
 {
lazy_init();

float scaleX = 1;
float scaleY = 1;

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

Upvotes: 1

Wez Sie Tato
Wez Sie Tato

Reputation: 1186

In CCDrawNode class you can draw circles, lines and polygons.

void drawDot(const CCPoint & pos,float  radius, const ccColor4F & color)

draw a fill circle at a position, with a given radius and color

Upvotes: 3

ssantos
ssantos

Reputation: 16526

Not sure from which cocos2d-x version is available, but you should have a specific method for drawing solid circles.-

void drawSolidCircle( const Point& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY);

Have a look to the nightly CCDrawingPrimitives.cpp class.

Upvotes: 1

Related Questions