iOS texture management fixes

This commit is contained in:
Xawotihs
2010-12-06 22:25:28 +00:00
parent 13f467a9ef
commit e8f5d2d349

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)))
{
TextureInfo textureInfo;
JTexture *tex = NULL;
textureInfo.mBits = NULL;
NSString *path = [NSString stringWithUTF8String: JGE_GET_RES(filename).c_str()];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];
CGImageAlphaInfo info;
BOOL hasAlpha;
TextureInfo textureInfo;
JTexture *tex = NULL;
textureInfo.mBits = NULL;
int rawsize = 0;
BYTE* rawdata = NULL;
JFileSystem* fileSystem = JFileSystem::GetInstance();
NSData *texData = NULL;
UIImage *image = NULL;
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.mHeight = CGImageGetHeight(image.CGImage);
textureInfo.mTexWidth = getNextPower2(textureInfo.mWidth);
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();
info = hasAlpha ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast;
CGContextRef context =
CGBitmapContextCreate(textureInfo.mBits,
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;
tex = new JTexture();
if (tex)
{
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();
return tex;
do {
if (!fileSystem->OpenFile(filename))
break;
rawsize = fileSystem->GetFileSize();
rawdata = new BYTE[rawsize];
if (!rawdata)
{
fileSystem->CloseFile();
break;
}
fileSystem->ReadFile(rawdata, rawsize);
fileSystem->CloseFile();
texData = [[NSData alloc] initWithBytes:rawdata length:rawsize];
image = [[UIImage alloc] initWithData:texData];
CGImageAlphaInfo info;
BOOL hasAlpha;
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.mHeight = CGImageGetHeight(image.CGImage);
textureInfo.mTexWidth = getNextPower2(textureInfo.mWidth);
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();
info = /*hasAlpha ?*/ kCGImageAlphaPremultipliedLast /*: kCGImageAlphaNoneSkipLast*/;
CGContextRef context =
CGBitmapContextCreate(textureInfo.mBits,
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 );
CGContextDrawImage( context, CGRectMake( 0, 0, textureInfo.mWidth, textureInfo.mHeight ), image.CGImage );
tex = new JTexture();
if (tex)
{
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;
tex->mBuffer = textureInfo.mBits;
} else {
NSLog(@"JTexture for %s not created\n", filename);
}
CGContextRelease(context);
} while(0);
if(rawdata)
delete[] rawdata;
[image release];
[texData release];
return tex;
}
#elif (defined QT_CONFIG)
JTexture* JRenderer::LoadTexture(const char* filename, int mode, int TextureFormat __attribute__((unused)))