Here is what I have so far.
in my ToolBox class..
+(UIImage*) takeAsUIImage:(CGPoint)startPoint
{
CCDirector* director = [CCDirector sharedDirector];
CGSize size = CGSizeMake(80,80);
CGPoint point = ccp(startPoint.y,startPoint.x);
//Create buffer for pixels
GLuint bufferLength = size.width * size.height * 4;
GLubyte* buffer = (GLubyte*)malloc(bufferLength);
//Read Pixels from OpenGL
glReadPixels(point.x, point.y, size.width, size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//Make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
//Configure image
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * size.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref1 = CGImageCreate(size.width, size.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGImageRef maskRef = [[UIImage imageNamed:@"loopmask2.png"] CGImage];
CGImageRef overlay = [[UIImage imageNamed:@"loop.png"] CGImage];
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef),
NULL,
true);
CGImageRef iref = CGImageCreateWithMask(iref1,mask);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);
CGContextRef context = CGBitmapContextCreate(pixels, size.width, size.height, 8, size.width * 4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
switch (director.deviceOrientation)
{
case CCDeviceOrientationPortrait:
break;
case CCDeviceOrientationPortraitUpsideDown:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
CGContextTranslateCTM(context, -size.width, -size.height);
break;
case CCDeviceOrientationLandscapeLeft:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context, -size.height, 0);
break;
case CCDeviceOrientationLandscapeRight:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
CGContextTranslateCTM(context, size.width * 0.5f, -size.height);
break;
}
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), iref);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), overlay);
UIImage *outputImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
//Dealloc
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGContextRelease(context);
free(buffer);
free(pixels);
return outputImage;
}
+(CCTexture2D*) takeAsTexture2D:(CGPoint)startPoint
{
return [[[CCTexture2D alloc] initWithImage:[ToolBox takeAsUIImage:startPoint]] autorelease];
}
and how I init it..
- (void)addMag {
CCTexture2D *tempImage =[ToolBox takeAsTexture2D:currentTouchPosition];
mag = [CCSprite spriteWithTexture:tempImage];
mag.position = ccp(currentTouchPosition.x,currentTouchPosition.y + 60);
[self addChild:mag z:50];
}
It shows the magnifier and overlay, however, the image rendered inside it is from the wrong position on the scene. It seems like it may be updating dynamically with the new images, but they show up inside the magnifier "stacked" on one another. I also get "bad pixel data" where the overlay image should be.
Can anyone see any obvious mistakes or changes I should make?
This has actually been fun to do, as I am learning a lot about CG stuff that I was afraid to look at before...
Thanks!!
Harv