Improved texture loading on iOS

This commit is contained in:
Xawotihs
2010-11-25 23:18:57 +00:00
parent 29805852c8
commit 4951442499
+83 -59
View File
@@ -2046,81 +2046,105 @@ 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;
textureInfo.mWidth = CGImageGetWidth(image.CGImage); do {
textureInfo.mHeight = CGImageGetHeight(image.CGImage); info = CGImageGetAlphaInfo(image.CGImage);
textureInfo.mTexWidth = textureInfo.mWidth; hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info == kCGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO);
textureInfo.mTexHeight = textureInfo.mHeight;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if (image == nil) {
textureInfo.mBits = new u8 [ textureInfo.mHeight * textureInfo.mWidth * 4 ]; NSLog(@"Loading Texture : %s failed", filename);
CGContextRef context = CGBitmapContextCreate( textureInfo.mBits, textureInfo.mWidth, textureInfo.mHeight, 8, 4 * textureInfo.mWidth, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big ); break;
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) { textureInfo.mWidth = CGImageGetWidth(image.CGImage);
printf("Texture %s failed to load\n", filename); textureInfo.mHeight = CGImageGetHeight(image.CGImage);
return NULL; textureInfo.mTexWidth = getNextPower2(textureInfo.mWidth);
} textureInfo.mTexHeight = getNextPower2(textureInfo.mHeight);
bool ret = false; NSLog(@"Loading Texture : %s : %s : %ux%u", filename, (hasAlpha?"Alpha ":"No Alpha "), textureInfo.mWidth, textureInfo.mHeight);
JTexture *tex = new JTexture(); textureInfo.mBits = new u8 [ textureInfo.mTexHeight * textureInfo.mTexWidth * 4 ];
if (textureInfo.mBits == NULL) {
NSLog(@"Texture %s failed to load\n", filename);
break;
}
if (tex) CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
{ info = hasAlpha ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast;
if (mImageFilter != NULL)
mImageFilter->ProcessImage((PIXEL_TYPE*)textureInfo.mBits, textureInfo.mWidth, textureInfo.mHeight);
tex->mFilter = TEX_FILTER_LINEAR; CGContextRef context =
tex->mWidth = textureInfo.mWidth; CGBitmapContextCreate(textureInfo.mBits,
tex->mHeight = textureInfo.mHeight; textureInfo.mTexWidth,
tex->mTexWidth = textureInfo.mTexWidth; textureInfo.mTexHeight, 8,
tex->mTexHeight = textureInfo.mTexHeight; 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 );
GLuint texid; bool ret = false;
checkGlError();
glGenTextures(1, &texid);
tex->mTexId = texid;
// glError = glGetError();
mCurrentTex = texid; tex = new JTexture();
glBindTexture(GL_TEXTURE_2D, mCurrentTex); // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo.mTexWidth, textureInfo.mTexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureInfo.mBits);
ret = true;
}
CGContextRelease(context);
delete [] textureInfo.mBits;
//delete textureInfo;
[image release];
[texData release];
if (!ret)
{
if (tex) if (tex)
delete tex; {
tex = NULL; if (mImageFilter != NULL)
} mImageFilter->ProcessImage((PIXEL_TYPE*)textureInfo.mBits, textureInfo.mWidth, textureInfo.mHeight);
tex->mFilter = TEX_FILTER_LINEAR;
tex->mWidth = textureInfo.mWidth;
tex->mHeight = textureInfo.mHeight;
tex->mTexWidth = textureInfo.mTexWidth;
tex->mTexHeight = textureInfo.mTexHeight;
GLuint texid;
checkGlError();
glGenTextures(1, &texid);
tex->mTexId = texid;
// glError = glGetError();
mCurrentTex = texid;
glBindTexture(GL_TEXTURE_2D, mCurrentTex); // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo.mTexWidth, textureInfo.mTexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureInfo.mBits);
ret = true;
} else {
NSLog(@"JTexture for %s not created\n", filename);
}
CGContextRelease(context);
delete [] textureInfo.mBits;
//delete textureInfo;
[image release];
[texData release];
if (!ret)
{
if (tex)
delete tex;
tex = NULL;
}
} while(0);
checkGlError(); checkGlError();
return tex; return tex;