Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
extensions:sxcroppedimage [2011/03/26 01:18] – [Source Code] 66.201.49.218extensions:sxcroppedimage [2015/09/14 11:14] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +~~NOTOC~~
  
 +====== SXCroppedImage ======
 +
 +---- dataentry extension ----
 +type          : extension #or 'mod' if it requires changing the Sparrow source
 +author_mail   : ronyeh@gmail.com Ron Yeh
 +description   : Apply a simple rectangular mask for any SPImage. #enter a short description of the extension
 +lastupdate_dt :  #the date you created the extension
 +compatible    : v1.1 #the Sparrow version you tested the extension with
 +depends       :  #if the ext. depends on others, list them here
 +tags          : image, crop, mask #enter a few tags, separated by commas
 +homepage_url  :  #if the ext. has an URL (e.g. a Gist-page), add it here
 +download_url  :  #a direct link to the download (e.g. the Gist-archive)
 +license       : BSD 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:@"Image.png"];
 +SXCroppedImage *img = [[[SXCroppedImage alloc] initWithTexture:texture] autorelease];
 +img.cropRect = CGRectMake(230, 240, 90, 48);
 +[self addChild:img];
 +</code>
 +
 +The coordinates are relative to the top-left physical corner of your iPhone/iPad.
 +
 +===== Source Code =====
 +
 +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.
 +
 +=== Header (SXCroppedImage.h) ===
 +<code objc>
 +
 +#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
 +
 +</code>
 +
 +
 +=== Implementation (SXCroppedImage.m) === 
 +<code objc>
 +
 +#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
 +
 +
 +</code>
 +
 +===== Changelog =====
 +
 +  * //2011/03/24 17:00 PDT//: First public version
 +  * //2011/03/24 23:00 PDT//: Updated by Shilo
 +  * //2011/03/25 17:20 PDT//: Updated by Ron to support Retina Display
 +===== Discussion =====
 +
 +//No comments so far. Feel free to edit this part of the page.//
 
 
Powered by DokuWiki