Guest User

Untitled

a guest
Jan 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. + (id)spriteWithCGImage:(CGImageRef)image key:(NSString *)key
  2.  
  3. /* function that draw your shape in a CGContextRef */
  4. void DrawShape(CGContextRef ctx)
  5. {
  6. // draw a simple triangle
  7.  
  8. CGContextMoveToPoint(ctx, 50, 100);
  9. CGContextAddLineToPoint(ctx, 100, 0);
  10. CGContextAddLineToPoint(ctx, 0, 0);
  11. CGContextClosePath(ctx);
  12. }
  13.  
  14.  
  15. void CreateImage(void)
  16. {
  17. UIGraphicsBeginImageContext(CGSizeMake(100, 100));
  18.  
  19. DrawShape(UIGraphicsGetCurrentContext());
  20.  
  21. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  22.  
  23. UIGraphicsEndImageContext();
  24.  
  25. // now you've got an (autorelease) UIImage, that you can use to
  26. // create your sprite.
  27. // use image.CGImage if you need a CGImageRef
  28. }
  29.  
  30. CGContextRef CreateBitmapContextWithData(int width, int height, void *buffer)
  31. {
  32. CGContextRef ctx;
  33. CGColorSpaceRef colorspace;
  34. size_t bytesPerRow;
  35. size_t bitsPerComponent;
  36. size_t numberOfComponents;
  37. CGImageAlphaInfo alpha;
  38.  
  39. bitsPerComponent = 8;
  40. alpha = kCGImageAlphaPremultipliedLast;
  41.  
  42. numberOfComponents = 1;
  43. colorspace = CGColorSpaceCreateDeviceRGB();
  44.  
  45. numberOfComponents += CGColorSpaceGetNumberOfComponents(colorspace);
  46. bytesPerRow = (width * numberOfComponents);
  47.  
  48. ctx = CGBitmapContextCreate(buffer, width, height, bitsPerComponent, bytesPerRow, colorspace, alpha);
  49.  
  50. CGColorSpaceRelease(colorspace);
  51.  
  52. return ctx;
  53. }
  54.  
  55. void CreateImageOrGLTexture(void)
  56. {
  57. // use pow2 width and height to create your own glTexture ...
  58. void *buffer = calloc(1, 128 * 128 * 4);
  59. CGContextRef ctx = CreateBitmapContextWithData(128, 128, buffer);
  60.  
  61. // draw you shape
  62. DrawShape(ctx);
  63.  
  64. // you can create a CGImageRef with this context
  65. // CGImageRef image = CGBitmapContextCreateImage(ctx);
  66.  
  67. // you can create a gl texture with the current buffer
  68. // glBindTexture(...)
  69. // glTexParameteri(...)
  70. // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  71.  
  72. CGContextRelease(ctx);
  73. free(buffer);
  74. }
Add Comment
Please, Sign In to add comment