power of two

pull/1/head
cosmonaut 2022-07-19 17:27:56 -07:00
parent aeb8ed39f7
commit c5c7df6c91
1 changed files with 44 additions and 28 deletions

View File

@ -106,34 +106,19 @@ typedef struct Cram_Internal_Context
Cram_AtlasData *atlasData; Cram_AtlasData *atlasData;
} Cram_Internal_Context; } Cram_Internal_Context;
/* API functions */ /* Internal functions */
uint32_t Wellspring_LinkedVersion(void) /* https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */
static uint32_t Cram_Internal_NextPowerOfTwo(uint32_t v)
{ {
return WELLSPRING_COMPILED_VERSION; v--;
} v |= v >> 1;
v |= v >> 2;
Cram_Context* Cram_Init(Cram_ContextCreateInfo *createInfo) v |= v >> 4;
{ v |= v >> 8;
Cram_Internal_Context *context = Cram_malloc(sizeof(Cram_Internal_Context)); v |= v >> 16;
v++;
context->name = Cram_strdup(createInfo->name); return v;
context->width = createInfo->maxDimension;
context->height = createInfo->maxDimension;
context->padding = createInfo->padding;
context->trim = createInfo->trim;
context->images = Cram_malloc(INITIAL_DATA_CAPACITY * sizeof(Cram_Image));
context->imageCapacity = INITIAL_DATA_CAPACITY;
context->imageCount = 0;
context->atlasData = Cram_malloc(sizeof(Cram_AtlasData));
context->pixels = NULL;
return (Cram_Context*) context;
} }
static uint8_t Cram_Internal_IsImageEqual(Cram_Image *a, Cram_Image *b) static uint8_t Cram_Internal_IsImageEqual(Cram_Image *a, Cram_Image *b)
@ -220,6 +205,36 @@ static int8_t Cram_Internal_CopyPixels(
return 0; return 0;
} }
/* API functions */
uint32_t Wellspring_LinkedVersion(void)
{
return WELLSPRING_COMPILED_VERSION;
}
Cram_Context* Cram_Init(Cram_ContextCreateInfo *createInfo)
{
Cram_Internal_Context *context = Cram_malloc(sizeof(Cram_Internal_Context));
context->name = Cram_strdup(createInfo->name);
context->width = createInfo->maxDimension;
context->height = createInfo->maxDimension;
context->padding = createInfo->padding;
context->trim = createInfo->trim;
context->images = Cram_malloc(INITIAL_DATA_CAPACITY * sizeof(Cram_Image));
context->imageCapacity = INITIAL_DATA_CAPACITY;
context->imageCount = 0;
context->atlasData = Cram_malloc(sizeof(Cram_AtlasData));
context->pixels = NULL;
return (Cram_Context*) context;
}
void Cram_AddFile(Cram_Context *context, const char *path) void Cram_AddFile(Cram_Context *context, const char *path)
{ {
Cram_Internal_Context *internalContext = (Cram_Internal_Context*) context; Cram_Internal_Context *internalContext = (Cram_Internal_Context*) context;
@ -378,6 +393,7 @@ int8_t Cram_Pack(Cram_Context *context)
} }
} }
/* TODO: replace this with something that uses MaxRects algorithm */
stbrp_pack_rects(&rectPackContext, rects, numRects); stbrp_pack_rects(&rectPackContext, rects, numRects);
numRects = 0; numRects = 0;
@ -404,8 +420,8 @@ int8_t Cram_Pack(Cram_Context *context)
} }
} }
internalContext->width = maxWidth; internalContext->width = Cram_Internal_NextPowerOfTwo(maxWidth);
internalContext->height = maxHeight; internalContext->height = Cram_Internal_NextPowerOfTwo(maxHeight);
internalContext->pixels = Cram_realloc(internalContext->pixels, internalContext->width * internalContext->height * 4); internalContext->pixels = Cram_realloc(internalContext->pixels, internalContext->width * internalContext->height * 4);
Cram_memset(internalContext->pixels, 0, internalContext->width * internalContext->height * 4); Cram_memset(internalContext->pixels, 0, internalContext->width * internalContext->height * 4);