Use it like you would an SPImage, except you may specify a cropRect, which will allow you to crop/show a certain area of the image:
SPTexture *texture = [SPTexture textureWithContentsOfFile:@"Image.png"]; SXCroppedImage *img = [[[SXCroppedImage alloc] initWithTexture:texture] autorelease]; img.cropRect = CGRectMake(230, 240, 90, 48); [self addChild:img];
The coordinates are relative to the top-left physical corner of your iPhone/iPad.
SXCroppedImage subclasses SPImage. You can take a similar approach if you want to mask other SPDisplayObjects, such as SPQuad. You might need to add more imports, depending on how you set up your project and how you reference the Sparrow framework.
#import <Foundation/Foundation.h> #import "SPImage.h" @class SPTexture; @interface SXCroppedImage : SPImage { @protected int mCropX; // these coordinates are in points (320 x 480 for iPhone) int mCropY; int mCropW; int mCropH; int mCropXPixels; // However, GL_SCISSOR_TEST needs absolute pixels int mCropYPixelsFlipped; // We also flip the y axis so that our offsets are from the top-left corner of the iPhone int mCropWPixels; int mCropHPixels; float mScreenHeightPixels; } @property (nonatomic, assign) CGRect cropRect; @property (nonatomic, assign) int cropX; @property (nonatomic, assign) int cropY; @property (nonatomic, assign) int cropWidth; @property (nonatomic, assign) int cropHeight; - (id)initWithTexture:(SPTexture *)texture; - (id)initWithContentsOfFile:(NSString *)path; + (SXCroppedImage *)imageWithTexture:(SPTexture *)texture; + (SXCroppedImage *)imageWithContentsOfFile:(NSString *)path; @end
#import "SXCroppedImage.h" #import "SPRenderSupport.h" #import "SPTexture.h" #import <OpenGLES/EAGL.h> #import <OpenGLES/ES1/gl.h> #import <OpenGLES/ES1/glext.h> @implementation SXCroppedImage @synthesize cropX = mCropX; @synthesize cropY = mCropY; @synthesize cropWidth = mCropW; @synthesize cropHeight = mCropH; + (SXCroppedImage *)imageWithTexture:(SPTexture *)texture { return [[[SXCroppedImage alloc] initWithTexture:texture] autorelease]; } + (SXCroppedImage *)imageWithContentsOfFile:(NSString *)path { return [[[SXCroppedImage alloc] initWithContentsOfFile:path] autorelease]; } - (id)initWithTexture:(SPTexture *)texture { if (self = [super initWithTexture:texture]) { CGSize screenSize = [UIScreen mainScreen].bounds.size; mScreenHeightPixels = screenSize.height * [SPStage contentScaleFactor]; [self setCropRect:CGRectMake(0,0,screenSize.width,screenSize.height)]; } return self; } - (id)initWithContentsOfFile:(NSString *)path { return [self initWithTexture:[SPTexture textureWithContentsOfFile:path]]; } - (void)render:(SPRenderSupport *)support { glEnable(GL_SCISSOR_TEST); glScissor(mCropXPixels, mCropYPixelsFlipped, mCropWPixels, mCropHPixels); [super render:support]; glDisable(GL_SCISSOR_TEST); } - (CGRect)cropRect { return CGRectMake(mCropX, mCropY, mCropW, mCropH); } - (void)setCropRect:(CGRect)rect { mCropX = rect.origin.x; mCropY = rect.origin.y; mCropW = rect.size.width; mCropH = rect.size.height; mCropXPixels = mCropX * [SPStage contentScaleFactor]; mCropWPixels = mCropW * [SPStage contentScaleFactor]; mCropHPixels = mCropH * [SPStage contentScaleFactor]; mCropYPixelsFlipped = mScreenHeightPixels - (mCropY * [SPStage contentScaleFactor]) - mCropHPixels; } @end
No comments so far. Feel free to edit this part of the page.