Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 0 down vote accepted

i 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);

    void CreateImage(void)
       UIGraphicsBeginImageContext(CGSizeMake(100, 100));


       UIImage *image = UIGraphicsGetImageFromCurrentImageContext();


       // 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);


    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

   // 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);


See Apple documentation for all CGContext functions :

Hope this help.

share|improve this answer
Hey Thanks for the detailed reply! however In the cocos2d environment I'm unable to get a valid GRContextRef. – PeteBob Oct 2 '12 at 16:44
Strange, i use similar function than CreateBitmapContextWithData a lot on iOS with or without OpenGL ES, in command line tools on Mac OS ... Do you have errors log on console ? – Emmanuel Oct 2 '12 at 16:54
Spoke to soon! It actually works great! Thanks for the help :) I did have to create my own bitmap context though. – PeteBob Oct 2 '12 at 20:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.