How to draw a solid circle with cocos2d for iPhone

后端 未结 6 1801
遇见更好的自我
遇见更好的自我 2021-02-02 15:53

Is it possible to draw a filled circle with cocos2d ? An outlined circle can be done using the drawCircle() function, but is there a way to fill it in a certain color? Perhaps b

6条回答
  •  长情又很酷
    2021-02-02 16:20

    Here's a slight modification of ccDrawCircle() that lets you draw any slice of a circle. Stick this in CCDrawingPrimitives.m and also add the method header information to CCDrawingPrimitives.h:

    Parameters: a: starting angle in radians, d: delta or change in angle in radians (use 2*M_PI for a complete circle)

    Changes are commented

    void ccDrawFilledCircle( CGPoint center, float r, float a, float d, NSUInteger totalSegs)
    {
        int additionalSegment = 2;
    
        const float coef = 2.0f * (float)M_PI/totalSegs;
    
        NSUInteger segs = d / coef;
        segs++; //Rather draw over than not draw enough
    
        if (d == 0) return;
    
        GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2), 1);
        if( ! vertices )
            return;
    
        for(NSUInteger i=0;i<=segs;i++)
        {
            float rads = i*coef;
            GLfloat j = r * cosf(rads + a) + center.x;
            GLfloat k = r * sinf(rads + a) + center.y;
    
            //Leave first 2 spots for origin
            vertices[2+ i*2] = j * CC_CONTENT_SCALE_FACTOR();
            vertices[2+ i*2+1] =k * CC_CONTENT_SCALE_FACTOR();
        }
        //Put origin vertices into first 2 spots
        vertices[0] = center.x * CC_CONTENT_SCALE_FACTOR();
        vertices[1] = center.y * CC_CONTENT_SCALE_FACTOR();
    
        // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
        // Needed states: GL_VERTEX_ARRAY, 
        // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY   
        glDisable(GL_TEXTURE_2D);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
    
        glVertexPointer(2, GL_FLOAT, 0, vertices);
        //Change to fan
        glDrawArrays(GL_TRIANGLE_FAN, 0, segs+additionalSegment);
    
        // restore default state
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnable(GL_TEXTURE_2D);    
    
        free( vertices );
    }
    

提交回复
热议问题