开发者

Cocoa working with a texture atlas

I'm loading a texture atlas into my app using the drawRect of a NSView subview:

NSImage *imageFromBundle = [NSImage imageNamed:@"sheet1.png"];
[self setNeedsDisplay:YES];
NSSize isize = [imageFromBundle size];
[imageFromBundle drawInRect:[self bounds] fromRect:NSMakeRect(0.0, 0.0,  isize.width, isize.height) operation: NSCompositeCopy fraction:1.0];

this works fine but displays the whole texture atlas. How do I zone in on a specific part of the image? The image is 1800x1200 pixels, each image is 180x250pixels, so image one would be x=0, y=0, w=180, h=250, two would be x=180, y=0, w=180, h=250, and so on.

I tried changing the x, y, w, h in the above but my output is just a black image. Any help would be appreciated.

Edit: Solved it, though I'm not sure if this is correct:

I changed the file to 1100x1100 (this has no effect on the solution but will reflect in the answer so I wanted to note it. And then I was just targeting the wrong area (x,y) of the image and passing the wrong size to target. New code looks like so:

[imageFromBundle drawInRect:[self bounds] fromRect:NSMakeRect(0.0, 950.0, 175.0, 250开发者_如何转开发.0) operation: NSCompositeCopy fraction:1.0];

If there is a different way of accomplishing this would love to see it. Thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜