4

我是 cocos2d-x 的新手,我正在尝试在其中绘制带有颜色的圆圈。我在网上搜索,我发现了一些代码。我尝试了下面的代码,但它只绘制带有颜色边框的圆圈。我的 cocos2d-x 版本是 2.1.5,我将它用于 android。我还尝试使用此方法更改边框宽度:glLineWidth(2); 但是我的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);
4

3 回答 3

3

CCDrawNode类中,您可以绘制圆、线和多边形。

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

在给定半径和颜色的位置绘制填充圆

于 2013-09-30T09:21:57.537 回答
1

只需在 CCDrawingPrimitives 中添加一个新方法即可。除了ccDrawCircle使用glDrawArraysGL_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);
 }
于 2013-10-07T17:39:11.167 回答
1

不确定哪个cocos2d-x版本可用,但你应该有一个绘制实心圆的特定方法。-

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

看看每晚的CCDrawingPrimitives.cpp课程。

于 2013-09-29T09:21:08.377 回答