Skip to content

Commit

Permalink
Add support for packing spritesheets to a common divisor.
Browse files Browse the repository at this point in the history
  • Loading branch information
slembcke committed Apr 20, 2015
1 parent 99b900a commit c180299
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions SpriteBuilder/libs/Tupac/Tupac.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@property(nonatomic,copy) NSString* directoryPrefix;
@property(nonatomic,assign) int maxTextureSize;
@property(nonatomic,assign) int padding;
@property(nonatomic,assign) int divisor;
@property(nonatomic,assign) BOOL dither;
@property(nonatomic,assign) BOOL compress;
@property(nonatomic,assign) BOOL trim;
Expand Down
14 changes: 11 additions & 3 deletions SpriteBuilder/libs/Tupac/Tupac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ - (id)init
self.outputFormat = TupacOutputFormatCocos2D;
self.maxTextureSize = 2048;
self.padding = 1;
self.divisor = 1;
self.trim = YES;
}
return self;
Expand Down Expand Up @@ -210,6 +211,13 @@ + (NSRect) trimmedRectForImage:(CGImageRef)image
return [basename stringByAppendingPathExtension:ext];
}

// Pad a size and round the result up to a multiple of the divisor
static int
PadSize(int size, int padding, int divisor)
{
return (size + padding + divisor - 1)/divisor*divisor;
}

- (NSArray *)createTextureAtlasTrimSuffix:(NSString *)suffix
{
// Reset the error message
Expand Down Expand Up @@ -322,7 +330,7 @@ - (NSArray *)createTextureAtlasTrimSuffix:(NSString *)suffix
BOOL packingError = NO;
while (!packingError && !allFitted)
{
MaxRectsBinPack bin(outW, outH);
MaxRectsBinPack bin(outW - self.padding, outH - self.padding);

std::vector<TPRectSize> inRects;

Expand All @@ -332,8 +340,8 @@ - (NSArray *)createTextureAtlasTrimSuffix:(NSString *)suffix
NSRect trimRect = [imageInfo[@"trimRect"] rectValue];

inRects.push_back(TPRectSize());
inRects[numImages].width = (int) (trimRect.size.width + self.padding * 2);
inRects[numImages].height = (int) (trimRect.size.height + self.padding * 2);
inRects[numImages].width = PadSize(trimRect.size.width, self.padding, self.divisor);
inRects[numImages].height = PadSize(trimRect.size.height, self.padding, self.divisor);
inRects[numImages].idx = numImages;

numImages++;
Expand Down

0 comments on commit c180299

Please sign in to comment.