我对Objective-C很新,甚至一般都是C,所以请耐心等待。我的主要目标是使用glDrawArrays(GL_LINE_STRIP,0,points)显示我的NSMutableArray CGPoints(通过NSValue);
我注意到cocos2d需要一个数组(?)指针*poli
,如下所示:
void ccDrawPoly( CGPoint *poli, int points, BOOL closePolygon ) { ... }
所以我尝试将我的NSMutableArray转换为C数组,我可以正常访问/调试CGPoints:
NSUInteger count = [points count];
id *buffer = malloc(sizeof(NSValue) * count);
[points getObjects: buffer];
for(uint i = 0; i < count; i++) {
NSValue *val = buffer[i];
CGPoint p = [val CGPointValue];
NSLog(@"points x %i: %f", i, p.x);
/* shows up in the console as:
-----------points at 0: 42.000000
-----------points at 1: 44.000000
... etc
*/
}
free(buffer);
但我猜我陷入困境的原因是让他们进入ccDrawPoly
或glDrawArrays(GL_LINE_STRIP, 0, points)
接受的数据类型。这显然是一个结构或东西,但我不知道如何将它们变成结构。
任何帮助将不胜感激!谢谢!
答案 0 :(得分:0)
这是我正在使用的新代码,以防它帮助其他人:
@interface Blah : CCLayer
{
CGPoint *parr;
NSUInteger parr_count;
NSUInteger parr_max;
}
@end
@implementation Blah
-(id) init
{
if((self = [super init])) {
parr_count = 0;
parr_max = 64;
parr = malloc(parr_max * 2 * sizeof(CGPoint));
}
return self;
}
...
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint prev_loc = [touch previousLocationInView: [touch view]];
CGPoint prev_glloc = [[CCDirector sharedDirector] convertToGL:prev_loc];
CGPoint loc = [touch locationInView: [touch view]];
CGPoint glloc = [[CCDirector sharedDirector] convertToGL:loc];
if(parr_count >= 2048) { // hard limit of 2048
return;
} else if(parr_count == parr_max) {
parr_max = 2 * parr_max;
parr = realloc(parr, parr_max * 2 * sizeof(GLfloat));
}
parr[parr_count] = prev_glloc;
parr_count += 1;
parr[parr_count] = glloc;
parr_count += 1;
}
...
-(void) draw
{
if(parr_count < 2048)
ccDrawPoly(parr, parr_count, NO);
}
- (void) dealloc
{
free(parr);
[super dealloc];
}
@end
似乎工作正常!如果有人有任何优化或评论,我将不胜感激,谢谢。