Saturday, February 7, 2009

PVRTC support in cocos2d


From cocos2d v0.7 onwards you can use non-raw PVRTC (PVR Texture compression) images. (Raw PVRTC images are supported since v0.5.2)

In order to load a non-raw PVRTC image you can use the same API that you were using to load PNG/BMP/JPEG images. eg:

// Sprite using PVRTC file
Sprite *img = [Sprite spriteWithFile:@"test_image.pvr"];

// Texture Atlas with PVRTC file
TextureAtlas *textureAtlas = [TextureAtlas textureAtlasWithFile: @"atlastest.pvr" capacity:3];

// TileMapAtlas with PVR file
TileMapAtlas *tilemap = [TileMapAtlas tileMapAtlasWithTileFile:@"tiles.pvr" mapFile:@"levelmap.tga" tileWidth:16 tileHeight:16];

// LabelAtlas with PVR file

LabelAtlas *label = [LabelAtlas labelAtlasWithString:@"123 Test" charMapFile:@"tuffy_bold_italic-charmap.pvr" itemWidth:48 itemHeight:64 startCharMap:' '];

As you can see, the PVRTC support is transparent .
Using PVRTC files has the following benefits:
  • It reduces the loading time (less footprint)
  • It is faster, for it consumes less bandwidth
  • It is also faster, since it implies less texture switches because more textures can be in memory at the same time (I might be wrong on this one :)
To convert a PNG file to PVRTC use this step:
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/texturetool -e PVRTC -f PVR --channel-weighting-linear --bits-per-pixel-4 -o test_image.pvr test_image.png
Edit: Fixed grammar errors

5 comments:

Anonymous said...

Excellent ... I've been waiting for some type of pvrtc transparency in cocos2d

Anonymous said...

PVRTC textures should have no bearing on the speed at which textures are switched in OpenGL. The state change to another section of VRAM, I believe, will be the same regardless of whether the texture is compressed or not. Placing more frames of a sprite within a texture will reduce the texture switches needed and that would speed things up.

I'm going to take a second look at cocos2d :)

riq said...

thanks for the info wendy.

Unknown said...

Could someone explain for a n00b what is the difference between non-raw PVRTC and raw PVRTC formats? I tried PVRTC compression in a recently released Cocos2d 0.63 game I worked on (http://excelltechmobile.net/boa.html), and I was only successful at compressing my background textures, which were of square dimensions equal to a power of two. Does non-raw PVRTC eliminate this requirement?

riq said...

Hi Ben,
The difference between the raw and non-raw format, is that the non-raw format has the meta-info in it's header, so you can load a non-raw format without additional info, since all the info is in it's header.

Apart from that, both formats have the same limitations:
. size: power of 2

But, if you want a smaller image, let's say "480 x 320" you can create a 512x512 image with transparent margins.