PVR Texture Format, Texture Cache

I have been looking around for tools to make my sprite sheet smaller. People say that PVR textures are the way to go.

So the graphics processor on iPhone is a design licensed from Power VR  (http://en.wikipedia.org/wiki/PowerVR). PVR textures are in such a format that Power VR graphics chips can easily digest, resulting in much faster processing. And smaller file size.

Sometime ago I bought Vladu Bogdan's SpriteHelper and LevelHelper toolsuite. In SpriteHelper, there is an option to save the resulting sprite sheet into a PVR format. But the quality is so low (it's lossily compressed). I'm looking for a tool that enables me to reduce the size and memory footprint of my sprite sheets without the quality degradation (lossless compression or uncompressed).

I used Level Helper in just one project that needed it. It's ok. I use Sprite Helper quite a lot but recently with the introduction of the Gatekeeper for Mac, it has become increasingly a pain to use. Really. It just frustrates me how Sprite Helper gets stuck at the beach ball of death phase when starting up. How it fails to save the plist, how it won't add  multiple images at once, etc. I have just grown sick of it now.

Anyway, back to PVR.

XCode comes with a tool to convert sprite sheet into PVR texture, but just as SpriteHelper, it uses lossy compression. The tool can be found in dir: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/texturetool

It's called texturetool.

Another tool: TexturePacker. I read people praising it, but it's paid app. Haven't tried.

Then there is this tool from Power VR themselves: PVRTexTool. It comes with both GU and CL interfaces. But I have not managed to get it working. I mean, I can't get it to convert my PNG files (generated by Sprite Helper) into pvr format.

So I fall back to texturetool. I created a small bash script to convert all PNG files in a directory into PVR format. Here it is:


#!/bin/bash

for file in `find ./ -name "*png"`
do
texturetool  -e PVRTC --bits-per-pixel-4 -f PVR -m  -o $file.pvr $file
done


The image quality is lower than the original PNG and in some cases the difference stands out. But I have not got much of a choice right now. Will tinker with PVRTexTool later when I got time.

I also learn about how sprite frame cache and texture cache work in Cocos2D. So basically texture cache is a dictionary with keys being the file/texture names and values the textures. One texture can (and should) contain a whole slew of images. Not just one. And you access each image by sprite frame cache.

So for instance you have image1.png, image2.png.... image50.png. You pack them into one big image using Sprite Helper or other tools. And then you have another file describing the position/coordinate of each image. This file is called the plist file. It's a dictionary with key being the individual image name (image1, etc) and value the coordinate of it in the big, packed image.

You create sprite frame cache from the plist (i.e you pass the plist name as the argument into sprite frame cache static methods). Then sprite frame cache will try to load the image with the same name as the plist into the texture cache. Sprite frame cache will expect the image to end with the png extension. If you have different image extension, such as pvr, you need to load the image manually into the texture  cache. Code snippet:


    CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    NSString *spriteFrameName = @"myplistfile.plist";
    NSString *textureName = @"myspritesheet.pvr";
    CCTexture2D *texture = [[CCTextureCache sharedTextureCache] textureForKey:textureName];
    [frameCache addSpriteFramesWithFile:spriteFrameName texture:texture];




0 komentar:

Posting Komentar