I'm trying to make a sprite that uses the graphics that are made with core graphics. I cant seem to find anything to explain how to make shapes using core graphics to create a sprite to use in cocos2d-iPhone.
Thanks for any insights and help!
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityi don't know cocos2d, so i just can just give you some tricks about CoreGraphics on iOS.
First, the simple case, if you can create sprite from a UIImage or a CGImageRef.
/* function that draw your shape in a CGContextRef */
void DrawShape(CGContextRef ctx)
{
// draw a simple triangle
CGContextMoveToPoint(ctx, 50, 100);
CGContextAddLineToPoint(ctx, 100, 0);
CGContextAddLineToPoint(ctx, 0, 0);
CGContextClosePath(ctx);
}
void CreateImage(void)
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
DrawShape(UIGraphicsGetCurrentContext());
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// now you've got an (autorelease) UIImage, that you can use to
// create your sprite.
// use image.CGImage if you need a CGImageRef
}
If you need to provide a buffer of data, and create your own CGContextRef :
CGContextRef CreateBitmapContextWithData(int width, int height, void *buffer)
{
CGContextRef ctx;
CGColorSpaceRef colorspace;
size_t bytesPerRow;
size_t bitsPerComponent;
size_t numberOfComponents;
CGImageAlphaInfo alpha;
bitsPerComponent = 8;
alpha = kCGImageAlphaPremultipliedLast;
numberOfComponents = 1;
colorspace = CGColorSpaceCreateDeviceRGB();
numberOfComponents += CGColorSpaceGetNumberOfComponents(colorspace);
bytesPerRow = (width * numberOfComponents);
ctx = CGBitmapContextCreate(buffer, width, height, bitsPerComponent, bytesPerRow, colorspace, alpha);
CGColorSpaceRelease(colorspace);
return ctx;
}
void CreateImageOrGLTexture(void)
{
// use pow2 width and height to create your own glTexture ...
void *buffer = calloc(1, 128 * 128 * 4);
CGContextRef ctx = CreateBitmapContextWithData(128, 128, buffer);
// draw you shape
DrawShape(ctx);
// you can create a CGImageRef with this context
// CGImageRef image = CGBitmapContextCreateImage(ctx);
// you can create a gl texture with the current buffer
// glBindTexture(...)
// glTexParameteri(...)
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
CGContextRelease(ctx);
free(buffer);
}
See Apple documentation for all CGContext functions : https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGContext/Reference/reference.html
Hope this help.