2009-07-04 14 views

Respuesta

1

Mira en:

  • CGContextAddArc
  • CGContextFillPath

Estos se permitirá llenar un círculo sin necesidad de OpenGL

1

También me pregunto esto, pero realmente no han logrado haciéndolo. Traté de usar CGContext cosas que Grouchal propina arriba, pero no puedo conseguir que dibuje nada en la pantalla. Esto es lo que he intentado:

-(void) draw 
{ 
    [self makestuff:UIGraphicsGetCurrentContext()]; 
} 

-(void)makestuff:(CGContextRef)context 
{ 
    // Drawing lines with a white stroke color 
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); 
    // Draw them with a 2.0 stroke width so they are a bit more visible. 
    CGContextSetLineWidth(context, 2.0); 

    // Draw a single line from left to right 
    CGContextMoveToPoint(context, 10.0, 30.0); 
    CGContextAddLineToPoint(context, 310.0, 30.0); 
    CGContextStrokePath(context); 

    // Draw a connected sequence of line segments 
    CGPoint addLines[] = 
    { 
     CGPointMake(10.0, 90.0), 
     CGPointMake(70.0, 60.0), 
     CGPointMake(130.0, 90.0), 
     CGPointMake(190.0, 60.0), 
     CGPointMake(250.0, 90.0), 
     CGPointMake(310.0, 60.0), 
    }; 
    // Bulk call to add lines to the current path. 
    // Equivalent to MoveToPoint(points[0]); for(i=1; i<count; ++i) AddLineToPoint(points[i]); 
    CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0])); 
    CGContextStrokePath(context); 

    // Draw a series of line segments. Each pair of points is a segment 
    CGPoint strokeSegments[] = 
    { 
     CGPointMake(10.0, 150.0), 
     CGPointMake(70.0, 120.0), 
     CGPointMake(130.0, 150.0), 
     CGPointMake(190.0, 120.0), 
     CGPointMake(250.0, 150.0), 
     CGPointMake(310.0, 120.0), 
    }; 
    // Bulk call to stroke a sequence of line segments. 
    // Equivalent to for(i=0; i<count; i+=2) { MoveToPoint(point[i]); AddLineToPoint(point[i+1]); StrokePath(); } 
    CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0])); 
} 

Estos métodos están definidos en una clase de nodo de cocos, y el método makestuff me prestó un ejemplo de código ...

NOTA: Estoy intentando dibujar cualquier forma o camino y llenarlo. Sé que el código anterior solo dibuja líneas, pero no quise continuar hasta que lo hice funcionar.

EDITAR: Esto es probablemente una solución horrible, pero creo que esto al menos funcionaría.

Cada CocosNode tiene una textura (Texture2D *). La clase Texture2D se puede inicializar desde un UIImage. UIImage se puede inicializar desde un CGImageRef. Es posible crear un contexto CGImageRef para la lib de cuarzo.

Por lo tanto, lo que haría es:

  1. Crear el contexto CGImageRef para el cuarzo
  2. dibujan en esta imagen con cuarzo
  3. inicializar un UIImage con este CGImageRef
  4. Hacer un Texture2D que es inicializado con esa imagen
  5. Establezca la textura de un CocosNode en esa instancia de Texture2D

La pregunta es si esto sería lo suficientemente rápido como para hacerlo. Preferiría si pudieras obtener un CGImageRef directamente del CocosNode y dibujar en él en lugar de seguir todos estos pasos, pero todavía no he encontrado la manera de hacerlo (y soy un novato en esto por lo que es difícil llegar a algún lugar en absoluto).

2

Aquí hay una ligera modificación de ccDrawCircle () que te permite dibujar cualquier porción de un círculo. Se adhieren esto en CCDrawingPrimitives.m y también añadir la información de cabecera método para CCDrawingPrimitives.h:

Parámetros: a: ángulo inicial en radianes, d: delta o cambio en el ángulo en radianes (uso 2*M_PI para un círculo completo)

Los cambios se comentan

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

utilicé esta manera a continuación.

glLineWidth(2); 
for(int i=0;i<50;i++){ 
    ccDrawCircle(ccp(s.width/2, s.height/2), i,0, 50, NO); 
} 

Hice múltiples círculos con para el bucle y parece un círculo relleno.

0

hay una nueva función en cocos2d CCDrawingPrimitives llamada ccDrawSolidCircle(CGPoint center, float r, NSUInteger segs). Para aquellos que buscan en este momento, utilizar este método en lugar, entonces usted no tiene que meterse con el código cocos2d, basta con importar CCDrawingPrimitives.h

Cuestiones relacionadas