2012-07-04 11 views
13

que tienen código:CGPoint a NSValue y revertir

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 es CGPoint, así que necesito alguna manera de cambiar de CGPoint a NSValue para añadirlo a NSMutableArray y después de que convertir de nuevo a CGPoint. ¿Cómo podría hacerse?

Respuesta

20

La clase NSValue tiene los métodos +[valueWithPoint:] y -[CGPointValue]? ¿Es esto lo que estás buscando?

//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

me trataron 'NSValue * cgpointObj = [NSValue valueWithPoint: loc]; '. Pero, ¿cómo convertir de nuevo a CGPoint? – hockeyman

+1

Algo como 'CGPoint loc = cgpointObj.pointValue' debería funcionar. Agregué el código actualizado. – Stream

+0

Creo que no agrega ningún valor. Si escribo código: | 'NSUInteger sum = vertices.count; NSLog (@ "count:% lu", sum); ' Siempre escribe que suma = 0. ¿Por qué? No se han agregado valores? Entonces, ¿por qué no se han agregado? – hockeyman

Cuestiones relacionadas