Reputation: 6276
Given an NSMutableArray
of dynamic CGPoint
s, what is the fastest and most efficient way to draw lines from array[0]
to array[1]
, array[1]
to array[2]
, etc.? Should I rewrite my function in C or C++ for better performance? Currently my framerate suffers dramatically when there are more than ~20 points in the array. I am using cocos2d v2.0.0-rc2 and I currently have:
-(void)draw
{
for (int i = 0; i < [points count]; i+=2)
{
CGPoint startFromArray = [[points objectAtIndex:i] CGPointValue];
CGPoint endFromArray = [[points objectAtIndex:i+1] CGPointValue];
ccDrawLine(startFromArray, endFromArray);
}
[super draw];
}
Upvotes: 1
Views: 1351
Reputation: 43842
There's no need to use iteration here. Cocos2d has a built-in function called ccDrawPoly()
. You can use it like this:
CGPoint *verts = malloc(sizeof(CGPoint) * [points count]);
for (int i = 0; i < [points count]; i++) {
verts[i] = [[points objectAtIndex:i] CGPointValue];
}
ccDrawPoly(verts, [points count], NO);
free(verts);
Obviously, you'll get even better performance if you store your CGPoints in a C array instead of boxing and unboxing them from NSValues, but if you really need the mutability, that can't be helped.
As for the third argument of ccDrawPoly()
, setting it to YES
will connect the start and end points of the array, making a closed polygon, while using NO
will just make a bunch of open lines.
Upvotes: 2