Check it out. I've learned alot and my game has benefited so much from the code on this site, so I figured I should give something back.
I'm an objective-c noob so don't slam my coding style, its roots date back to the days of C-based DOS games.
This class will dynamically switch between two texture classes, one that does pvr compression and one that does not. It will pick the right one based on your hardware capability, so your app will always support new devices without the pvrtc extension. Both texture sub classes are available from the iphone sdk examples (CrashLanding and PVRTextureLoader).
Code:
//
// TextureBase.h
// Aero
//
// Created by Mike Farrell on 2/6/10.
//
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
@interface TextureBase : NSObject
+load_texture:(NSString *)str do_mipmap:(BOOL)mipmap;
+load_texture_nocomp:(NSString *)str do_mipmap:(BOOL)mipmap;
+(void) can_do_pvr;
@end
@protocol TextureBase <NSObject>
-(GLuint) name;
- (void) dealloc;
@end
Code:
//
// TextureBase.m
// Aero
//
// Created by Mike Farrell on 2/6/10.
// Copyright 2010 Mike Farrell. All rights reserved.
//
#import "TextureBase.h"
#import "PVRTexture.h"
#import "Texture2D.h"
static BOOL can_do_pvr = NO;
@implementation TextureBase
+load_texture:(NSString *)str do_mipmap:(BOOL)mipmap
{
if(can_do_pvr)
{
id<TextureBase> pvrTexture = [ [PVRTexture alloc ] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:str ofType:@"pvr"]];
if(pvrTexture == nil)
NSLog(@"Failed to load %@.pvr", str);
else
{
if(mipmap)
{
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2.0f);
}
return pvrTexture;
}
}
else
{
return [ [ Texture2D alloc ] initWithImage:[UIImage imageWithContentsOfFile :[[NSBundle mainBundle] pathForResource:str ofType:@"png"]] do_mipmap:mipmap];
}
return nil;
}
+load_texture_nocomp:(NSString *)str do_mipmap:(BOOL)mipmap
{
return [ [ Texture2D alloc ] initWithImage:[UIImage imageWithContentsOfFile :[[NSBundle mainBundle] pathForResource:str ofType:@"png"]] do_mipmap:mipmap];
}
+(void) can_do_pvr
{
can_do_pvr = YES;
}
@end
Code:
//don't forget this in your esrenderer init routine
//....
NSString *extensionString = [NSString stringWithUTF8String:(char *)glGetString(GL_EXTENSIONS)];
NSArray *extensions = [extensionString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
BOOL pvr = FALSE;
for(NSString *oneExtension in extensions)
{
if([oneExtension compare:@"GL_IMG_texture_compression_pvrtc"] == NSOrderedSame)
pvr = TRUE;
}
if(pvr)
{
NSLog(@"Device can do pvr textures");
[ TextureBase can_do_pvr ];
}