From e247b5d96008fa5bf00a631b85bcef648dc21ad5 Mon Sep 17 00:00:00 2001 From: Darragh Coy Date: Tue, 2 Jul 2013 13:39:16 -0700 Subject: [PATCH] Add ccDrawSolidCircle Similar to ccDrawCircle() primitives drawing function except the circle is filled. --- cocos2dx/draw_nodes/CCDrawingPrimitives.cpp | 46 +++++++++++++++++++ cocos2dx/draw_nodes/CCDrawingPrimitives.h | 4 ++ .../DrawPrimitivesTest/DrawPrimitivesTest.cpp | 7 +++ 3 files changed, 57 insertions(+) diff --git a/cocos2dx/draw_nodes/CCDrawingPrimitives.cpp b/cocos2dx/draw_nodes/CCDrawingPrimitives.cpp index 30632f8958..8e55c894ad 100644 --- a/cocos2dx/draw_nodes/CCDrawingPrimitives.cpp +++ b/cocos2dx/draw_nodes/CCDrawingPrimitives.cpp @@ -383,6 +383,52 @@ void CC_DLL ccDrawCircle( const Point& center, float radius, float angle, unsign ccDrawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f); } +void ccDrawSolidCircle( const Point& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY) +{ + lazy_init(); + + 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( kVertexAttribFlag_Position ); + +#ifdef EMSCRIPTEN + setGLBufferData(vertices, sizeof(GLfloat)*2*(segments+2)); + glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0); +#else + glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices); +#endif // EMSCRIPTEN + + glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) segments+1); + + free( vertices ); + + CC_INCREMENT_GL_DRAWS(1); +} + +void CC_DLL ccDrawSolidCircle( const Point& center, float radius, float angle, unsigned int segments) +{ + ccDrawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f); +} + void ccDrawQuadBezier(const Point& origin, const Point& control, const Point& destination, unsigned int segments) { lazy_init(); diff --git a/cocos2dx/draw_nodes/CCDrawingPrimitives.h b/cocos2dx/draw_nodes/CCDrawingPrimitives.h index 8949aed7ab..d2cd24af75 100644 --- a/cocos2dx/draw_nodes/CCDrawingPrimitives.h +++ b/cocos2dx/draw_nodes/CCDrawingPrimitives.h @@ -113,6 +113,10 @@ void CC_DLL ccDrawSolidPoly( const Point *poli, unsigned int numberOfPoints, ccC void CC_DLL ccDrawCircle( const Point& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY); void CC_DLL ccDrawCircle( const Point& center, float radius, float angle, unsigned int segments, bool drawLineToCenter); +/** draws a solid circle given the center, radius and number of segments. */ +void CC_DLL ccDrawSolidCircle( const Point& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY); +void CC_DLL ccDrawSolidCircle( const Point& center, float radius, float angle, unsigned int segments); + /** draws a quad bezier path @warning This function could be pretty slow. Use it only for debugging purposes. @since v0.8 diff --git a/samples/Cpp/TestCpp/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp b/samples/Cpp/TestCpp/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp index a3a72e8622..bd35b80d5c 100644 --- a/samples/Cpp/TestCpp/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp +++ b/samples/Cpp/TestCpp/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp @@ -172,6 +172,13 @@ void DrawPrimitivesTest::draw() CHECK_GL_ERROR_DEBUG(); + // draw a pink solid circle with 50 segments + glLineWidth(2); + ccDrawColor4B(255, 0, 255, 255); + ccDrawSolidCircle( VisibleRect::center() + ccp(140,0), 40, CC_DEGREES_TO_RADIANS(90), 50, 1.0f, 1.0f); + + CHECK_GL_ERROR_DEBUG(); + // open yellow poly ccDrawColor4B(255, 255, 0, 255); glLineWidth(10);