Skip to content

Instantly share code, notes, and snippets.

@jarodl
Created September 21, 2011 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarodl/1233071 to your computer and use it in GitHub Desktop.
Save jarodl/1233071 to your computer and use it in GitHub Desktop.
CCDrawingPrimitives+ccDrawFillCircle
//
// CCDrawingPrimitives+ccDrawFillCircle.h
// MindSnacks
//
// Created by Jarod L on 9/20/11.
// Copyright (c) 2011 MindSnacks. All rights reserved.
//
#import "CCDrawingPrimitives.h"
void ccDrawFillCircle( CGPoint center, float radius, float angle, NSUInteger segments);
//
// CCDrawingPrimitives+ccDrawFillCircle.m
// MindSnacks
//
// Created by Jarod L on 9/20/11.
// Copyright (c) 2011 MindSnacks. All rights reserved.
//
#import "CCDrawingPrimitives+ccDrawFillCircle.h"
#import "ccTypes.h"
#import "ccMacros.h"
//#import "Platforms/CCGL.h"
void ccDrawFillCircle( CGPoint center, float radius, float angle, NSUInteger segments)
{
const float coef = 2.0f * (float)M_PI/segments;
GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segments+2), 1);
if( ! vertices )
return;
for(NSUInteger i=0;i<=segments;i++)
{
float rads = i*coef;
GLfloat j = radius * cosf(rads + angle) + center.x;
GLfloat k = radius * sinf(rads + angle) + center.y;
vertices[i*2] = j * CC_CONTENT_SCALE_FACTOR();
vertices[i*2+1] =k * CC_CONTENT_SCALE_FACTOR();
}
vertices[(segments+1)*2] = center.x * CC_CONTENT_SCALE_FACTOR();
vertices[(segments+1)*2+1] = center.y * CC_CONTENT_SCALE_FACTOR();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glBlendFunc(GL_ONE, GL_ZERO);
glDrawArrays(GL_TRIANGLE_FAN, 0, segments);
// restore default GL state
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnable(GL_TEXTURE_2D);
}
@marksands
Copy link

Awesome sauce! Might wanna add this line?

void ccDrawFillCircle( ..
    // ..
    free(vertices);
}

@jarodl
Copy link
Author

jarodl commented Oct 17, 2011

Nice! Good catch. I'm not sure why the gist keeps showing up twice. Even after I delete the duplicate .m & .h file it still shows up.

@marksands
Copy link

Yeah, I saw that. They're named different though,CCDrawingPrimitives+ccDrawFillCircle and CCDrawingPrimitivesccDrawFillCircle might have something to do with it?

@jarodl
Copy link
Author

jarodl commented Oct 17, 2011

Weird. In the edit view they are all named the same thing (the filename without the +). When I changed them all to have + the extra two disappeared :S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment