This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
extensions:sxcroppedimage [2011/03/26 01:16] – [Source Code] 66.201.49.218 | extensions:sxcroppedimage [2015/09/14 11:14] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~NOTOC~~ | ||
+ | ====== SXCroppedImage ====== | ||
+ | |||
+ | ---- dataentry extension ---- | ||
+ | type : extension #or ' | ||
+ | author_mail | ||
+ | description | ||
+ | lastupdate_dt : #the date you created the extension | ||
+ | compatible | ||
+ | depends | ||
+ | tags : image, crop, mask #enter a few tags, separated by commas | ||
+ | homepage_url | ||
+ | download_url | ||
+ | license | ||
+ | ---- | ||
+ | |||
+ | ===== Usage ===== | ||
+ | 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: | ||
+ | <code objc> | ||
+ | SPTexture *texture = [SPTexture textureWithContentsOfFile: | ||
+ | SXCroppedImage *img = [[[SXCroppedImage alloc] initWithTexture: | ||
+ | img.cropRect = CGRectMake(230, | ||
+ | [self addChild: | ||
+ | </ | ||
+ | |||
+ | The coordinates are relative to the top-left physical corner of your iPhone/ | ||
+ | |||
+ | ===== Source Code ===== | ||
+ | |||
+ | SXCroppedImage subclasses SPImage. You can take a similar approach if you want to mask other SPDisplayObjects, | ||
+ | |||
+ | === Header (SXCroppedImage.h) === | ||
+ | <code objc> | ||
+ | |||
+ | #import < | ||
+ | #import " | ||
+ | |||
+ | @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; | ||
+ | int mCropYPixelsFlipped; | ||
+ | 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: | ||
+ | - (id)initWithContentsOfFile: | ||
+ | + (SXCroppedImage *)imageWithTexture: | ||
+ | + (SXCroppedImage *)imageWithContentsOfFile: | ||
+ | |||
+ | @end | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | === Implementation (SXCroppedImage.m) === | ||
+ | <code objc> | ||
+ | |||
+ | #import " | ||
+ | #import " | ||
+ | #import " | ||
+ | #import < | ||
+ | #import < | ||
+ | #import < | ||
+ | |||
+ | @implementation SXCroppedImage | ||
+ | |||
+ | @synthesize cropX = mCropX; | ||
+ | @synthesize cropY = mCropY; | ||
+ | @synthesize cropWidth = mCropW; | ||
+ | @synthesize cropHeight = mCropH; | ||
+ | |||
+ | |||
+ | + (SXCroppedImage *)imageWithTexture: | ||
+ | return [[[SXCroppedImage alloc] initWithTexture: | ||
+ | } | ||
+ | |||
+ | + (SXCroppedImage *)imageWithContentsOfFile: | ||
+ | return [[[SXCroppedImage alloc] initWithContentsOfFile: | ||
+ | } | ||
+ | |||
+ | - (id)initWithTexture: | ||
+ | if (self = [super initWithTexture: | ||
+ | CGSize screenSize = [UIScreen mainScreen].bounds.size; | ||
+ | mScreenHeightPixels = screenSize.height * [SPStage contentScaleFactor]; | ||
+ | [self setCropRect: | ||
+ | } | ||
+ | return self; | ||
+ | } | ||
+ | |||
+ | - (id)initWithContentsOfFile: | ||
+ | return [self initWithTexture: | ||
+ | } | ||
+ | |||
+ | - (void)render: | ||
+ | glEnable(GL_SCISSOR_TEST); | ||
+ | glScissor(mCropXPixels, | ||
+ | [super render: | ||
+ | glDisable(GL_SCISSOR_TEST); | ||
+ | } | ||
+ | |||
+ | - (CGRect)cropRect { | ||
+ | return CGRectMake(mCropX, | ||
+ | } | ||
+ | |||
+ | - (void)setCropRect: | ||
+ | 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 | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * // | ||
+ | * // | ||
+ | * // | ||
+ | ===== Discussion ===== | ||
+ | |||
+ | //No comments so far. Feel free to edit this part of the page.// |