我對Objective-C甚至是C語言都很新穎,因此請耐心等待。我的主要目標是使用glDrawArrays(GL_LINE_STRIP,0,points)顯示我的CGPoints NSMutableArray(通過NSValue);將一個NSValues/CGPoints數組轉換爲一個CGPoint結構體,以便在cocos2d中與glDrawArrays一起使用
我注意到,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)
會接受。這顯然是一個結構或東西,但我不知道如何讓他們進入一個結構。
任何幫助將不勝感激!謝謝!
糟糕,我想我知道了... 而不是使用NSMutableArray我現在使用CGPoint * parr;在我的init方法中,連同一個NSUInteger「parr_count」來跟蹤* parr的大小。然後,我像添加普通數組一樣添加CGPoint,如下所示:parr [parr_count] = glloc;我唯一的問題是...我怎麼知道要分配多大,以及我應該在什麼時候釋放這些? (在 - (void)dealloc方法?)謝謝! – taber 2010-03-02 23:19:27