Skip to content

Commit

Permalink
Fix spritesheet generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
slembcke committed Apr 20, 2015
1 parent 053ed84 commit 99b900a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
3 changes: 1 addition & 2 deletions SpriteBuilder/ccBuilder/CCBDirectoryPublisher.m
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,12 @@ - (PublishSpriteSheetOperation *)createSpriteSheetOperation:(NSString *)publishD
statusProgress:_publishingTaskStatusProgress];

NSString *spriteSheetCacheDir = [_projectSettings.tempSpriteSheetCacheDirectory stringByAppendingPathComponent:subPath];
NSString *resolutionCacheDir = [spriteSheetCacheDir stringByAppendingPathComponent:[NSString stringWithFormat:@"resources-%@", resolution]];

operation.publishDirectory = publishDirectory;
operation.publishedPNGFiles = _publishedPNGFiles;
operation.srcSpriteSheetDate = srcSpriteSheetDate;
operation.resolution = resolution;
operation.srcDirs = @[resolutionCacheDir, spriteSheetCacheDir];
operation.srcDir = spriteSheetCacheDir;
operation.spriteSheetFile = spriteSheetFile;
operation.subPath = subPath;
operation.osType = _osType;
Expand Down
2 changes: 1 addition & 1 deletion SpriteBuilder/ccBuilder/PublishSpriteSheetOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@property (nonatomic, copy) NSString *spriteSheetFile;
@property (nonatomic) CCBPublisherOSType osType;
@property (nonatomic, copy) NSString *subPath;
@property (nonatomic, strong) NSArray *srcDirs;
@property (nonatomic, copy) NSString *srcDir;
@property (nonatomic, copy) NSNumber *resolution;
@property (nonatomic, copy) NSDate *srcSpriteSheetDate;
@property (nonatomic, copy) NSString *publishDirectory;
Expand Down
9 changes: 5 additions & 4 deletions SpriteBuilder/ccBuilder/PublishSpriteSheetOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ - (void)assertProperties
{
NSAssert(_spriteSheetFile != nil, @"spriteSheetFile should not be nil");
NSAssert(_subPath != nil, @"subPath should not be nil");
NSAssert(_srcDirs != nil, @"srcDirs should not be nil");
NSAssert(_srcDir != nil, @"srcDir should not be nil");
NSAssert(_resolution != nil, @"resolution should not be nil");
NSAssert(_srcSpriteSheetDate != nil, @"srcSpriteSheetDate should not be nil");
NSAssert(_publishDirectory != nil, @"publishDirectory should not be nil");
Expand All @@ -73,7 +73,8 @@ - (void)publishSpriteSheet

[self configurePacker];

NSArray *createdFiles = [_packer createTextureAtlasFromDirectoryPaths:_srcDirs];
NSString *suffix = [NSString stringWithFormat:@"-%dx", self.resolution.intValue];
NSArray *createdFiles = [_packer createTextureAtlasFromDirectoryPath:_srcDir withSuffix:suffix];

[self addCreatedPNGFilesToCreatedFilesSet:createdFiles];

Expand Down Expand Up @@ -190,8 +191,8 @@ - (void)cancel

- (NSString *)description
{
return [NSString stringWithFormat:@"file: %@, res: %@, osType: %i, filefull: %@, srcdirs: %@, publishDirectory: %@, date: %@",
[_spriteSheetFile lastPathComponent], _resolution, _osType, _spriteSheetFile, _srcDirs, _publishDirectory, _srcSpriteSheetDate];
return [NSString stringWithFormat:@"file: %@, res: %@, osType: %i, filefull: %@, srcdir: %@, publishDirectory: %@, date: %@",
[_spriteSheetFile lastPathComponent], _resolution, _osType, _spriteSheetFile, _srcDir, _publishDirectory, _srcSpriteSheetDate];
}


Expand Down
4 changes: 2 additions & 2 deletions SpriteBuilder/libs/Tupac/Tupac.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@

+ (NSRect) trimmedRectForImage:(CGImageRef)image;

- (NSArray *)createTextureAtlasFromDirectoryPaths:(NSArray *)dirs;
- (NSArray *)createTextureAtlas;
- (NSArray *)createTextureAtlasFromDirectoryPath:(NSString *)dir withSuffix:(NSString *)suffix;
- (NSArray *)createTextureAtlasTrimSuffix:(NSString *)suffix;

- (void)cancel;

Expand Down
63 changes: 27 additions & 36 deletions SpriteBuilder/libs/Tupac/Tupac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,17 @@ + (NSRect) trimmedRectForImage:(CGImageRef)image
return NSMakeRect(x, y, wTrimmed, hTrimmed);
}

- (NSArray *)createTextureAtlas
static NSString *
TrimSuffix(NSString *filename, NSString *suffix)
{
NSString *basename = [filename stringByDeletingPathExtension];
if([basename hasSuffix:suffix]) basename = [basename substringToIndex:basename.length - suffix.length];
NSString *ext = [filename pathExtension];

return [basename stringByAppendingPathExtension:ext];
}

- (NSArray *)createTextureAtlasTrimSuffix:(NSString *)suffix
{
// Reset the error message
if (errorMessage)
Expand Down Expand Up @@ -504,7 +514,7 @@ - (NSArray *)createTextureAtlas
{
// Get info about the image
NSString* filename = self.filenames[(NSUInteger) outRects[index].idx];
NSString* exportFilename = [filename lastPathComponent];
NSString* exportFilename = TrimSuffix([filename lastPathComponent], suffix);
if (directoryPrefix_) exportFilename = [directoryPrefix_ stringByAppendingPathComponent:exportFilename];
NSDictionary* imageInfo = imageInfos[(NSUInteger) outRects[index].idx];

Expand Down Expand Up @@ -540,7 +550,7 @@ - (NSArray *)createTextureAtlas
@"sourceSize" : NSStringFromSize(NSMakeSize(wSrc, hSrc))};
}

metadata[@"textureFileName"] = textureFileName;
metadata[@"textureFileName"] = TrimSuffix(textureFileName, suffix);
metadata[@"format"] = @2;
metadata[@"size"] = NSStringFromSize(NSMakeSize(outW, outH));

Expand Down Expand Up @@ -574,52 +584,33 @@ - (void)generatePreviewImage:(NSString *)pngFilename
}
}

- (NSArray *) createTextureAtlasFromDirectoryPaths:(NSArray *)dirs
- (NSArray *) createTextureAtlasFromDirectoryPath:(NSString *)dir withSuffix:(NSString *)suffix;
{
NSFileManager* fm = [NSFileManager defaultManager];

// Build a list of all file names from all directories
NSMutableSet* allFiles = [NSMutableSet set];
// Add all the absolute file names to an array from the correct directories
NSMutableArray* absoluteFilepaths = [NSMutableArray array];

for (NSString* dir in dirs)
{
NSArray* files = [fm contentsOfDirectoryAtPath:dir error:NULL];
NSArray* files = [fm contentsOfDirectoryAtPath:dir error:NULL];

if (cancelled_)
{
return nil;
}

for (NSString* file in files)
{
NSString *lower = [[file pathExtension] lowercaseString];
if ([lower isEqualToString:@"png"] || [lower isEqualToString:@"psd"])
{
[allFiles addObject:[file lastPathComponent]];
}
}
if (cancelled_)
{
return nil;
}

// Add all the absolute file names to an array from the correct directories
NSMutableArray* absoluteFilepaths = [NSMutableArray array];
for (NSString* file in allFiles)

for (NSString* file in files)
{
for (NSString* dir in dirs)
NSString *ext = [[file pathExtension] lowercaseString];
NSString *basename = [file stringByDeletingPathExtension];
if ([basename hasSuffix:suffix] && ([ext isEqualToString:@"png"] || [ext isEqualToString:@"psd"]))
{
NSString* absFilepath = [dir stringByAppendingPathComponent:file];

if ([fm fileExistsAtPath:absFilepath])
{
[absoluteFilepaths addObject:absFilepath];
//foundFile = YES;
break;
}
[absoluteFilepaths addObject:[dir stringByAppendingPathComponent:file]];
}
}

// Generate the sprite sheet
self.filenames = absoluteFilepaths;
return [self createTextureAtlas];
return [self createTextureAtlasTrimSuffix:suffix];
}

- (void)cancel
Expand Down

0 comments on commit 99b900a

Please sign in to comment.