Hi all,
Apple's Texture2D class is a very useful bit of kit for iOS developers.
Q. Is there a Mac OS X version of this class available?
(I've googled but can only find iOS implementations, mostly through Cocos2D projects.)
Cheers.
Hi all,
Apple's Texture2D class is a very useful bit of kit for iOS developers.
Q. Is there a Mac OS X version of this class available?
(I've googled but can only find iOS implementations, mostly through Cocos2D projects.)
Cheers.
You could just write a method for it, here's what I use:
typedef struct {
void *data;
GLfloat width;
GLfloat height;
} TextureData;
+ (TextureData)loadPngTexture:(NSString *)fileName {
CFURLRef textureURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),
(CFStringRef)fileName,
CFSTR("png"),
NULL);
NSAssert(textureURL, @"Texture name invalid");
CGImageSourceRef imageSource = CGImageSourceCreateWithURL(textureURL, NULL);
NSAssert(imageSource, @"Invalid Image Path.");
NSAssert((CGImageSourceGetCount(imageSource) > 0), @"No Image in Image Source.");
CFRelease(textureURL);
CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
NSAssert(image, @"Image not created.");
CFRelease(imageSource);
GLuint width = CGImageGetWidth(image);
GLuint height = CGImageGetHeight(image);
void *data = malloc(width * height * 4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSAssert(colorSpace, @"Colorspace not created.");
CGContextRef context = CGBitmapContextCreate(data,
width,
height,
8,
width * 4,
colorSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
NSAssert(context, @"Context not created.");
CGColorSpaceRelease(colorSpace);
// Flip so that it isn't upside-down
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGImageRelease(image);
CGContextRelease(context);
return (TextureData){ data, width, height };
}
and then use it like this:
TextureData td = [Renderer loadPngTexture:@"atlas"];
// Or use GL_TEXTURE_2D
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, td.width, td.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, td.data);