2013-09-29 85 views
2

我是cocos2d-x中的新手,我試圖用裏面的顏色繪製圓圈。 我在網上搜索,我發現了一些代碼。我嘗試了下面的代碼,但它只繪製帶有顏色邊框的圓。我的cocos2d-x版本是2.1.5,我正在使用它的android。 我也嘗試改變使用此方法的邊框寬度:glLineWidth(2);但在我的cocos2d-x中找不到此方法。如何在圓圈中添加顏色以及如何更改圓圈邊框的寬度。如何用cocos2d-x中的顏色​​繪製圓圈填充?

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

回答

0

不知道從哪個cocos2d-x版本,但你應該有繪製固體circles.-

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

的具體方法,看一下夜間CCDrawingPrimitives.cpp類。

+0

謝謝你答,但這方法在3.0alpha0-pre –

+0

那麼,你可以嘗試並複製該方法。主要區別應該是用'GL_TRIANGLE_FAN'而不是'GL_LINE_STRIP'調用'glDrawArrays' – ssantos

2

CCDrawNode類中,您可以繪製圓形,線條和多邊形。

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

的位置畫一個圓填充,具有給定半徑和顏色

1

就在CCDrawingPrimitives添加一個新方法。這是相同的ccDrawCircle除了glDrawArrays使用的GL_TRIANGLE_FAN代替GL_LINE_STRIP

在標題中加上:

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

在.cpp文件,添加:

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