2551/11/27

iPhone OpenGL Example: GLSprite

iPhone OpenGL Example: GLSprite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spriteData = (GLubyte *) malloc(width * height * 4);
// Uses the bitmatp creation function provided by the Core Graphics framework.
spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4,
CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipLast);
// After you create the context, you can draw the sprite image to the context.
CGContextDrawImage(spriteContext,
CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height),
spriteImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(spriteContext);
// Use OpenGL ES to generate a name for the texture.
glGenTextures(1, &spriteTexture);
// Bind the texture name.
glBindTexture(GL_TEXTURE_2D, spriteTexture);
// Specify a 2D texture image, provideing the a pointer to the image data in memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
// Release the image data
free(spriteData);
Powered By Blogger