Hi all,
For my own project, I found it useful to create a SPCroppedImage which allows me to arbitrarily crop an image on my iPhone/iPad stage. You specify a rectangle of coordinates relative to the bottom left corner of your iPhone/iPad, and it will crop your image to that rect. You can then move the image around and it will respect the mask.
Here is the code:
@interface SPCroppedImage : SPImage { @protected int mCropX; // x,y are offsets from the lower left physical corner of the iPhone int mCropY; int mCropW; int mCropH; } @property (nonatomic, assign) CGRect cropRect; @end
#import "SPCroppedImage.h" @implementation SPCroppedImage - (void)render:(SPRenderSupport *)support { static float texCoords[8]; static uint colors[4]; float alpha = self.alpha; [support bindTexture:mTexture]; [mTexture adjustTextureCoordinates:mTexCoords saveAtTarget:texCoords numVertices:4]; for (int i=0; i<4; ++i) colors[i] = [support convertColor:mVertexColors[i] alpha:alpha]; glEnable(GL_SCISSOR_TEST); glScissor(mCropX, mCropY, mCropW, mCropH); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, texCoords); glVertexPointer(2, GL_FLOAT, 0, mVertexCoords); glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisable(GL_SCISSOR_TEST); // Rendering was tested with vertex buffers, too -- but for simple quads and images like these, // the overhead seems to outweigh the benefit. The "glDrawArrays"-approach is faster here. } - (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; } @end
Of course, you can do this to any other class that has a method called:
- (void)render:(SPRenderSupport *)support
See SPRendering.m.
If people find cool uses for this, perhaps Daniel can add it to future versions of Sparrow.
Thanks,
Ron