Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- + (id)spriteWithCGImage:(CGImageRef)image key:(NSString *)key
- /* 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
- }
- 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);
- }
Add Comment
Please, Sign In to add comment