2012-07-04 11 views
13

Ho codice:CGPoint a NSValue e invertire

NSMutableArray *vertices = [[NSMutableArray alloc] init]; 

//Getting mouse coordinates 
loc = [self convertPoint: [event locationInWindow] fromView:self]; 
[vertices addObject:loc]; // Adding coordinates to NSMutableArray 

//Converting from NSMutableArray to GLfloat to work with OpenGL 
int count = [vertices count] * 2; // * 2 for the two coordinates of a loc object 
GLFloat []glVertices = (GLFloat *)malloc(count * sizeof(GLFloat)); 
int currIndex = 0; 
for (YourLocObject *loc in vertices) { 
    glVertices[currIndex++] = loc.x; 
    glVertices[currIndex++] = loc.y;   
} 

loc è CGPoint, quindi ho bisogno in qualche modo di cambiare da CGPoint a NSValue per aggiungerlo alla NSMutableArray e dopo che riconvertirlo in CGPoint. Come potrebbe essere fatto?

risposta

20

La classe NSValue ha metodi +[valueWithPoint:] e -[CGPointValue]? E 'questo quello che stai cercando?

//Getting mouse coordinates 
NSMutableArray *vertices = [[NSMutableArray alloc] init]; 
CGPoint location = [self convertPoint:event.locationInWindow fromView:self]; 
NSValue *locationValue = [NSValue valueWithPoint:location]; 
[vertices addObject:locationValue]; 

//Converting from NSMutableArray to GLFloat to work with OpenGL 
NSUInteger count = vertices.count * 2; // * 2 for the two coordinates 
GLFloat GLVertices[] = (GLFloat *)malloc(count * sizeof(GLFloat)); 
for (NSUInteger i = 0; i < count; i++) { 
    NSValue *locationValue = [vertices objectAtIndex:i]; 
    CGPoint location = locationValue.CGPointValue; 
    GLVertices[i] = location.x; 
    GLVertices[i] = location.y; 
} 
+0

ho cercato 'NSValue * cgpointObj = [NSValue valueWithPoint: loc];'. Ma come convertire nuovamente in CGPoint? – hockeyman

+1

Qualcosa come 'CGPoint loc = cgpointObj.pointValue' dovrebbe funzionare. Ho aggiunto il codice aggiornato. – Stream

+0

Penso che non aggiunga alcun valore. Se scrivo codice: | 'NSUInteger sum = vertici.count; NSLog (@ "count:% lu", sum); ' Scrive sempre tale somma = 0. Perché? Non sono stati aggiunti valori? Allora perché non sono stati aggiunti? – hockeyman