Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 02-07-2010, 03:57 PM   #1 (permalink)
mlfarrell
Registered Member
 
Join Date: Feb 2010
Posts: 223
Default Class for handling pvr and nonpvr

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 ];
    }
mlfarrell is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,868
Threads: 89,225
Posts: 380,691
Top Poster: BrianSlick (7,129)
Welcome to our newest member, stokesreece
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.