misc fixes

pull/1/head
cosmonaut 2022-07-22 11:38:30 -07:00
parent b919917b21
commit 812d142aa0
2 changed files with 16 additions and 11 deletions

View File

@ -68,7 +68,7 @@ typedef struct Cram_Internal_Context
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
uint32_t padding; int32_t padding;
uint8_t trim; uint8_t trim;
uint8_t *pixels; uint8_t *pixels;
@ -262,6 +262,7 @@ void Cram_Internal_Score(
} }
} }
/* Check if a contains b */
static inline uint8_t Cram_Internal_Contains(Rect* a, Rect* b) static inline uint8_t Cram_Internal_Contains(Rect* a, Rect* b)
{ {
return b->x >= a->x && return b->x >= a->x &&
@ -289,7 +290,7 @@ void Cram_Internal_PruneRects(RectPackContext* context)
if (context->freeRectangleCapacity < context->freeRectangleCount + context->newFreeRectangleCount) if (context->freeRectangleCapacity < context->freeRectangleCount + context->newFreeRectangleCount)
{ {
context->freeRectangleCapacity = context->freeRectangleCount + context->newFreeRectangleCount; context->freeRectangleCapacity = Cram_max(context->freeRectangleCapacity * 2, context->freeRectangleCount + context->newFreeRectangleCount);
context->freeRectangles = Cram_realloc(context->freeRectangles, sizeof(Rect) * context->freeRectangleCapacity); context->freeRectangles = Cram_realloc(context->freeRectangles, sizeof(Rect) * context->freeRectangleCapacity);
} }
@ -298,6 +299,8 @@ void Cram_Internal_PruneRects(RectPackContext* context)
context->freeRectangles[context->freeRectangleCount] = context->newFreeRectangles[i]; context->freeRectangles[context->freeRectangleCount] = context->newFreeRectangles[i];
context->freeRectangleCount += 1; context->freeRectangleCount += 1;
} }
context->newFreeRectangleCount = 0;
} }
static inline void Cram_Internal_AddNewFreeRect(RectPackContext *context, Rect rect) static inline void Cram_Internal_AddNewFreeRect(RectPackContext *context, Rect rect)
@ -405,8 +408,6 @@ void Cram_Internal_PlaceRect(RectPackContext *context, Rect *rect)
} }
Cram_Internal_PruneRects(context); Cram_Internal_PruneRects(context);
context->newFreeRectangleCount = 0;
} }
/* Given rects with width and height, modifies rects with packed x and y positions. */ /* Given rects with width and height, modifies rects with packed x and y positions. */
@ -654,7 +655,6 @@ int8_t Cram_Pack(Cram_Context *context)
if (Cram_Internal_PackRects(rectPackContext, packerRects, numRects) < 0) if (Cram_Internal_PackRects(rectPackContext, packerRects, numRects) < 0)
{ {
fprintf(stderr, "Not enough room! Packing aborted!");
return -1; return -1;
} }

View File

@ -78,7 +78,7 @@ static void dirwalk(char *dir)
void print_help() void print_help()
{ {
fprintf(stdout, "Usage: cram input_dir output_dir atlas_name [--padding padding_value] [--trim] [--dimension max_dimension]"); fprintf(stdout, "Usage: cram input_dir output_dir atlas_name [--padding padding_value] [--notrim] [--dimension max_dimension]");
} }
uint8_t check_dir_exists(char *path) uint8_t check_dir_exists(char *path)
@ -123,20 +123,21 @@ int main(int argc, char *argv[])
if (strcmp(arg, "--padding") == 0) if (strcmp(arg, "--padding") == 0)
{ {
i += 1; i += 1;
createInfo.padding = atoi(argv[argc]); createInfo.padding = atoi(argv[i]);
if (createInfo.padding < 0) if (createInfo.padding < 0)
{ {
fprintf(stderr, "Padding must be equal to or greater than 0!"); fprintf(stderr, "Padding must be equal to or greater than 0!");
return 1; return 1;
} }
} }
else if (strcmp(arg, "--trim") == 0) else if (strcmp(arg, "--notrim") == 0)
{ {
createInfo.trim = 1; createInfo.trim = 0;
} }
else if (strcmp(arg, "--dimension") == 0) else if (strcmp(arg, "--dimension") == 0)
{ {
createInfo.maxDimension = atoi(argv[argc]); i += 1;
createInfo.maxDimension = atoi(argv[i]);
if (createInfo.maxDimension < 0 || createInfo.maxDimension > 8192) if (createInfo.maxDimension < 0 || createInfo.maxDimension > 8192)
{ {
fprintf(stderr, "Padding must be between 0 and 8192!"); fprintf(stderr, "Padding must be between 0 and 8192!");
@ -188,7 +189,11 @@ int main(int argc, char *argv[])
dirwalk(inputDirPath); dirwalk(inputDirPath);
Cram_Pack(context); if (Cram_Pack(context) < 0)
{
fprintf(stderr, "Not enough room! Packing aborted!");
return 1;
}
Cram_GetPixelData(context, &pixelData, &width, &height); Cram_GetPixelData(context, &pixelData, &width, &height);