I was mostly just curious as to how well the algorithm I used packed something similar to your screenshots.
Most animations have transparency that can be stripped off to save space. This is nice to get more sprites per texture and especially important if you are using PVRTC on the iPhone.
Parsing my tools output on the iPhone goes like this:
@interface PackedImages : NSObject {
NSMutableDictionary *_nameToImage;
}
- (id) init:(NSString*)packFileName;
- (Image*) getImageNamed:(NSString*)name;
- (BOOL) hasImageNamed:(NSString*)name;
@end
@implementation PackedImages
- (id) init:(NSString*)packFileName {
self = [super init];
if (!self) return nil;
_nameToImage = [[NSMutableDictionary alloc] init];
NSString *packPath = [[NSBundle mainBundle] pathForResource:packFileName ofType:@"pack"];
NSLog(@"Loading packed images: %@", packPath);
FILE *file = fopen([packPath UTF8String], "rt");
Image *pageImage = nil;
while (true) {
char nameString[256];
if (fscanf(file, "%s", &nameString) == EOF) {
[pageImage release];
break;
}
NSString *name = [NSString stringWithCString:nameString encoding:NSASCIIStringEncoding];
if ([name length] == 0) {
[pageImage release];
pageImage = nil;
} else if (pageImage == nil) {
pageImage = [[Image alloc] initWithImageNamed:name];
} else {
int left, top, right, bottom, offsetX, offsetY, index;
fscanf(file, "%d", &left);
fscanf(file, "%d", &top);
fscanf(file, "%d", &right);
fscanf(file, "%d", &bottom);
fscanf(file, "%d", &offsetX);
fscanf(file, "%d", &offsetY);
if (!fscanf(file, "%d", &index)) index = 9999999;
Image *image = [[Image alloc] initWithSubImage:pageImage rect:CGRectMake(left, top, right - left, bottom - top)];
[image setOffset:offsetX y:offsetY];
[_nameToImage setValue:image forKey:name];
[image release];
}
}
return self;
}
- (Image*) getImageNamed:(NSString*)name {
Image* image = [_nameToImage valueForKey:name];
if (image == nil) NSLog(@"WARN: Packed image not found: %@", name);
return image;
}
- (BOOL) hasImageNamed:(NSString*)name {
return [_nameToImage valueForKey:name] != nil;
}
- (void) dealloc {
[_nameToImage release];
[super dealloc];
}
@end
Of course, you need a nice Image class. This doesn’t preserve the order that the Java PackedImages class does. The nice part is the upper left of the image before transparency is stripped is preserved.