Hi All,
I'm trying to integrate Quartz and Cocos2d. I have read on a number of forums that it is possible, and it may slow down performance. I have tried to create a simple App based on the previous posts, adding a UIImage to a CCTexture2D, and then initializing a CCSprite with it.
The App only displays a blank image square, does anyone know what I'm doing wrong??
Please see CircleSprite.m, CircleSprite.h and -(id) init from HelloWorldScene.m below:
CircleSprite.h ---------------------
#import "cocos2d.h"
@interface CircleSprite : CCSprite
{
}
- (id) initWithTexture: (CCTexture2D*) texture rect: (CGRect) rect;
@end
CircleSprite.m ----------------
#import <UIKit/UIKit.h>
#import "cocos2d.h"
CCTexture2D* spriteTexture;
@interface CircleSprite : CCSprite {
}
@end
@implementation CircleSprite
- (id) initWithTexture: (CCTexture2D*) texture rect: (CGRect) rect {
NSUInteger width = 128;
NSUInteger height = 128;
int bytesPerRow = (width * 4);
int byteCount = (bytesPerRow * height);
void *data = malloc(byteCount);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate (data, width, height,
8, bytesPerRow, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
CGContextSetGrayFillColor(context, 1.0f, 1.0f);
CGContextTranslateCTM(context, 0.0f, height);
CGContextScaleCTM(context, 1.0f, -1.0f);
UIGraphicsPushContext(context);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextFillEllipseInRect(context, rect);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextDrawImage(context, rect, image);
UIGraphicsPopContext();
UIImage* DrawingImage = [UIImage imageWithCGImage:image];
spriteTexture = [[CCTexture2D alloc ]initWithImage: DrawingImage];
self = [self initWithTexture:spriteTexture rect:rect];
CGContextRelease(context);
CGImageRelease(image);
free(data);
return self;
}
@end
HelloWordScene.m -----------------
-(id) init
{
CCTexture2D* tempTexture = [[CCTexture2D alloc] init];
CGRect tempRect = CGRectMake(0., 0., 128, 128);
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {
// create and initialize a Label
// CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
CircleSprite* sprite = [[CCSprite alloc] initWithTexture: tempTexture rect: tempRect]; // ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
// label.position = ccp( size.width /2 , size.height/2 );
sprite.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
// [self addChild: label];
[self addChild: sprite];
}
return self;
}
Thanks.