Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2010, 02:57 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 98
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
Old 02-08-2010, 12:56 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 144
Default

Quote:
Originally Posted by mlfarrell View Post

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).
Actually, you can do better than that .... instead of maintaining two sets of images (say .pvr and .png) you could detect if the device supports the pvrtc extension and if it doesn't , decompress the pvr file and submit it as a regular image.
warmi is offline   Reply With Quote
Old 02-08-2010, 01:54 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 98
Default

Quote:
Originally Posted by warmi View Post
Actually, you can do better than that .... instead of maintaining two sets of images (say .pvr and .png) you could detect if the device supports the pvrtc extension and if it doesn't , decompress the pvr file and submit it as a regular image.
Awesome. You have a link to the code. I still keep pngs for my GUI
textures because pvr alpha compression looks bad but my for my 512x512s I should def do that
mlfarrell is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,350
Threads: 39,144
Posts: 171,614
Top Poster: smasher (2,577)
Welcome to our newest member, 17make
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:36 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0