iOS texture management fixes

This commit is contained in:
Xawotihs
2010-12-06 22:25:28 +00:00
parent 13f467a9ef
commit e8f5d2d349
+92 -102
View File
@@ -1992,109 +1992,99 @@ 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; JTexture *tex = NULL;
textureInfo.mBits = NULL; textureInfo.mBits = NULL;
int rawsize = 0;
NSString *path = [NSString stringWithUTF8String: JGE_GET_RES(filename).c_str()]; BYTE* rawdata = NULL;
NSData *texData = [[NSData alloc] initWithContentsOfFile:path]; JFileSystem* fileSystem = JFileSystem::GetInstance();
UIImage *image = [[UIImage alloc] initWithData:texData]; NSData *texData = NULL;
CGImageAlphaInfo info; UIImage *image = NULL;
BOOL hasAlpha;
do { do {
info = CGImageGetAlphaInfo(image.CGImage); if (!fileSystem->OpenFile(filename))
hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info == kCGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO); break;
if (image == nil) { rawsize = fileSystem->GetFileSize();
NSLog(@"Loading Texture : %s failed", filename); rawdata = new BYTE[rawsize];
break;
} if (!rawdata)
{
textureInfo.mWidth = CGImageGetWidth(image.CGImage); fileSystem->CloseFile();
textureInfo.mHeight = CGImageGetHeight(image.CGImage); break;
textureInfo.mTexWidth = getNextPower2(textureInfo.mWidth); }
textureInfo.mTexHeight = getNextPower2(textureInfo.mHeight);
fileSystem->ReadFile(rawdata, rawsize);
NSLog(@"Loading Texture : %s : %s : %ux%u", filename, (hasAlpha?"Alpha ":"No Alpha "), textureInfo.mWidth, textureInfo.mHeight); fileSystem->CloseFile();
textureInfo.mBits = new u8 [ textureInfo.mTexHeight * textureInfo.mTexWidth * 4 ]; texData = [[NSData alloc] initWithBytes:rawdata length:rawsize];
if (textureInfo.mBits == NULL) { image = [[UIImage alloc] initWithData:texData];
NSLog(@"Texture %s failed to load\n", filename); CGImageAlphaInfo info;
break; BOOL hasAlpha;
}
info = CGImageGetAlphaInfo(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info == kCGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO);
info = hasAlpha ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast;
if (image == nil) {
CGContextRef context = NSLog(@"Loading Texture : %s failed", filename);
CGBitmapContextCreate(textureInfo.mBits, break;
textureInfo.mTexWidth, }
textureInfo.mTexHeight, 8,
4 * textureInfo.mTexWidth, textureInfo.mWidth = CGImageGetWidth(image.CGImage);
colorSpace, textureInfo.mHeight = CGImageGetHeight(image.CGImage);
info /*| kCGBitmapByteOrder32Big*/); textureInfo.mTexWidth = getNextPower2(textureInfo.mWidth);
CGColorSpaceRelease(colorSpace); textureInfo.mTexHeight = getNextPower2(textureInfo.mHeight);
CGContextClearRect( context, CGRectMake( 0, 0, textureInfo.mTexWidth, textureInfo.mTexHeight ) );
CGContextTranslateCTM( context, 0, textureInfo.mTexHeight - textureInfo.mHeight ); NSLog(@"Loading Texture : %s : %s : %ux%u", filename, (hasAlpha?"Alpha ":"No Alpha "), textureInfo.mWidth, textureInfo.mHeight);
if(!hasAlpha) {
CGContextSetAlpha(context, 1.0f); textureInfo.mBits = new u8 [ textureInfo.mTexHeight * textureInfo.mTexWidth * 4 ];
} if (textureInfo.mBits == NULL) {
CGContextDrawImage( context, CGRectMake( 0, 0, textureInfo.mWidth, textureInfo.mHeight ), image.CGImage ); NSLog(@"Texture %s failed to load\n", filename);
break;
bool ret = false; }
tex = new JTexture(); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
info = /*hasAlpha ?*/ kCGImageAlphaPremultipliedLast /*: kCGImageAlphaNoneSkipLast*/;
if (tex)
{ CGContextRef context =
if (mImageFilter != NULL) CGBitmapContextCreate(textureInfo.mBits,
mImageFilter->ProcessImage((PIXEL_TYPE*)textureInfo.mBits, textureInfo.mWidth, textureInfo.mHeight); textureInfo.mTexWidth,
textureInfo.mTexHeight, 8,
tex->mFilter = TEX_FILTER_LINEAR; 4 * textureInfo.mTexWidth,
tex->mWidth = textureInfo.mWidth; colorSpace,
tex->mHeight = textureInfo.mHeight; info /*| kCGBitmapByteOrder32Big*/);
tex->mTexWidth = textureInfo.mTexWidth; CGColorSpaceRelease(colorSpace);
tex->mTexHeight = textureInfo.mTexHeight; CGContextClearRect( context, CGRectMake( 0, 0, textureInfo.mTexWidth, textureInfo.mTexHeight ) );
CGContextTranslateCTM( context, 0, textureInfo.mTexHeight - textureInfo.mHeight );
GLuint texid; CGContextDrawImage( context, CGRectMake( 0, 0, textureInfo.mWidth, textureInfo.mHeight ), image.CGImage );
checkGlError();
glGenTextures(1, &texid); tex = new JTexture();
tex->mTexId = texid;
// glError = glGetError(); if (tex)
{
mCurrentTex = texid; if (mImageFilter != NULL)
glBindTexture(GL_TEXTURE_2D, mCurrentTex); // Bind To The Texture ID mImageFilter->ProcessImage((PIXEL_TYPE*)textureInfo.mBits, textureInfo.mWidth, textureInfo.mHeight);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); tex->mFilter = TEX_FILTER_LINEAR;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); tex->mWidth = textureInfo.mWidth;
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); tex->mHeight = textureInfo.mHeight;
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); tex->mTexWidth = textureInfo.mTexWidth;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureInfo.mTexWidth, textureInfo.mTexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureInfo.mBits); tex->mTexHeight = textureInfo.mTexHeight;
tex->mBuffer = textureInfo.mBits;
ret = true; } else {
} else { NSLog(@"JTexture for %s not created\n", filename);
NSLog(@"JTexture for %s not created\n", filename); }
}
CGContextRelease(context);
CGContextRelease(context); } while(0);
delete [] textureInfo.mBits; if(rawdata)
//delete textureInfo; delete[] rawdata;
[image release]; [image release];
[texData release]; [texData release];
if (!ret) return tex;
{
if (tex)
delete tex;
tex = NULL;
}
} while(0);
checkGlError();
return tex;
} }
#elif (defined QT_CONFIG) #elif (defined QT_CONFIG)
JTexture* JRenderer::LoadTexture(const char* filename, int mode, int TextureFormat __attribute__((unused))) JTexture* JRenderer::LoadTexture(const char* filename, int mode, int TextureFormat __attribute__((unused)))