Improved texture loading on iOS

This commit is contained in:
Xawotihs
2010-11-25 23:18:57 +00:00
parent 29805852c8
commit 4951442499
+40 -16
View File
@@ -2046,37 +2046,58 @@ void JRenderer::LoadGIF(TextureInfo &textureInfo, const char *filename, int mode
JTexture* JRenderer::LoadTexture(const char* filename, int mode, int TextureFormat __attribute__((unused))) JTexture* JRenderer::LoadTexture(const char* filename, int mode, int TextureFormat __attribute__((unused)))
{ {
TextureInfo textureInfo; TextureInfo textureInfo;
JTexture *tex = NULL;
textureInfo.mBits = NULL; textureInfo.mBits = NULL;
NSString *path = [NSString stringWithUTF8String: JGE_GET_RES(filename).c_str()]; NSString *path = [NSString stringWithUTF8String: JGE_GET_RES(filename).c_str()];
// NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:newString2];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path]; NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData]; UIImage *image = [[UIImage alloc] initWithData:texData];
if (image == nil) CGImageAlphaInfo info;
NSLog(@"Do real error checking here"); BOOL hasAlpha;
do {
info = CGImageGetAlphaInfo(image.CGImage);
hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info == kCGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO);
if (image == nil) {
NSLog(@"Loading Texture : %s failed", filename);
break;
}
textureInfo.mWidth = CGImageGetWidth(image.CGImage); textureInfo.mWidth = CGImageGetWidth(image.CGImage);
textureInfo.mHeight = CGImageGetHeight(image.CGImage); textureInfo.mHeight = CGImageGetHeight(image.CGImage);
textureInfo.mTexWidth = textureInfo.mWidth; textureInfo.mTexWidth = getNextPower2(textureInfo.mWidth);
textureInfo.mTexHeight = textureInfo.mHeight; textureInfo.mTexHeight = getNextPower2(textureInfo.mHeight);
NSLog(@"Loading Texture : %s : %s : %ux%u", filename, (hasAlpha?"Alpha ":"No Alpha "), textureInfo.mWidth, textureInfo.mHeight);
textureInfo.mBits = new u8 [ textureInfo.mTexHeight * textureInfo.mTexWidth * 4 ];
if (textureInfo.mBits == NULL) {
NSLog(@"Texture %s failed to load\n", filename);
break;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
textureInfo.mBits = new u8 [ textureInfo.mHeight * textureInfo.mWidth * 4 ]; info = hasAlpha ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast;
CGContextRef context = CGBitmapContextCreate( textureInfo.mBits, textureInfo.mWidth, textureInfo.mHeight, 8, 4 * textureInfo.mWidth, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, textureInfo.mWidth, textureInfo.mHeight ) );
CGContextTranslateCTM( context, 0, textureInfo.mHeight - textureInfo.mHeight );
CGContextDrawImage( context, CGRectMake( 0, 0, textureInfo.mWidth, textureInfo.mHeight ), image.CGImage );
if (textureInfo.mBits == NULL) { CGContextRef context =
printf("Texture %s failed to load\n", filename); CGBitmapContextCreate(textureInfo.mBits,
return NULL; textureInfo.mTexWidth,
textureInfo.mTexHeight, 8,
4 * textureInfo.mTexWidth,
colorSpace,
info /*| kCGBitmapByteOrder32Big*/);
CGColorSpaceRelease(colorSpace);
CGContextClearRect( context, CGRectMake( 0, 0, textureInfo.mTexWidth, textureInfo.mTexHeight ) );
CGContextTranslateCTM( context, 0, textureInfo.mTexHeight - textureInfo.mHeight );
if(!hasAlpha) {
CGContextSetAlpha(context, 1.0f);
} }
CGContextDrawImage( context, CGRectMake( 0, 0, textureInfo.mWidth, textureInfo.mHeight ), image.CGImage );
bool ret = false; bool ret = false;
JTexture *tex = new JTexture(); tex = new JTexture();
if (tex) if (tex)
{ {
@@ -2105,6 +2126,8 @@ JTexture* JRenderer::LoadTexture(const char* filename, int mode, int TextureForm
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo.mTexWidth, textureInfo.mTexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureInfo.mBits); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo.mTexWidth, textureInfo.mTexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureInfo.mBits);
ret = true; ret = true;
} else {
NSLog(@"JTexture for %s not created\n", filename);
} }
CGContextRelease(context); CGContextRelease(context);
@@ -2121,6 +2144,7 @@ JTexture* JRenderer::LoadTexture(const char* filename, int mode, int TextureForm
delete tex; delete tex;
tex = NULL; tex = NULL;
} }
} while(0);
checkGlError(); checkGlError();
return tex; return tex;