hoc*_*man 12 macos xcode cocoa objective-c
我有代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
loc
是CGPoint,所以我需要以某种方式从CGPoint更改为NSValue以将其添加到NSMutableArray,然后将其转换回CGPoint.怎么可能呢?
Vad*_*dim 20
班级NSValue
有方法+[valueWithPoint:]
和-[CGPointValue]
?这是你想要的?
//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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10776 次 |
最近记录: |