/* Copyright (c) 2023 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "tileset/SpriteCollection.h" #include "utilities/IOUtilities.h" #include #include SpriteCollection * SpriteCollection_create(SpriteCollectionID identifier, const char * name, unsigned int spriteCount, Sprite * sprites) { SpriteCollection * spriteCollection = malloc(sizeof(*spriteCollection)); spriteCollection->identifier = identifier; spriteCollection->name = strdup_nullSafe(name); spriteCollection->spriteCount = spriteCount; spriteCollection->sprites = NULL; if (spriteCount > 0) { spriteCollection->sprites = malloc(spriteCount * sizeof(*spriteCollection->sprites)); for (unsigned int spriteIndex = 0; spriteIndex < spriteCount; spriteIndex++) { spriteCollection->sprites[spriteIndex] = sprites[spriteIndex]; spriteCollection->sprites[spriteIndex].name = strdup(sprites[spriteIndex].name); spriteCollection->sprites[spriteIndex].frames = memdup(sprites[spriteIndex].frames, sprites[spriteIndex].frameCount * sizeof(*sprites[spriteIndex].frames)); } } return spriteCollection; } SpriteCollection * SpriteCollection_copy(SpriteCollection * spriteCollection) { return SpriteCollection_create(spriteCollection->identifier, spriteCollection->name, spriteCollection->spriteCount, spriteCollection->sprites); } void SpriteCollection_dispose(SpriteCollection * spriteCollection) { for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { free(spriteCollection->sprites[spriteIndex].name); free(spriteCollection->sprites[spriteIndex].frames); } free(spriteCollection->name); free(spriteCollection); } SpriteCollection * SpriteCollection_deserializeMinimal(compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { DeserializationContext * context = deserializationContext; if (formatVersion > SPRITE_COLLECTION_FORMAT_VERSION) { return NULL; } SpriteCollectionID identifier = call_virtual(readUInt32, context, "id"); const char * name = call_virtual(readString, context, "name"); unsigned int spriteCount = call_virtual(beginArray, context, "sprites"); if (spriteCount == 0 || spriteCount > SPRITE_COLLECTION_ENTRY_COUNT_MAX) { return NULL; } Sprite sprites[spriteCount]; for (unsigned int spriteIndex = 0; spriteIndex < spriteCount; spriteIndex++) { call_virtual(beginStructure, context, NULL); sprites[spriteIndex].identifier = call_virtual(readUInt32, context, "id"); sprites[spriteIndex].name = (char *) call_virtual(readString, context, "name"); sprites[spriteIndex].origin.x = call_virtual(readInt16, context, "origin_x"); sprites[spriteIndex].origin.y = call_virtual(readInt16, context, "origin_y"); sprites[spriteIndex].frameCount = call_virtual(beginArray, context, "frames"); if (sprites[spriteIndex].frameCount == 0 || sprites[spriteIndex].frameCount > SPRITE_FRAME_COUNT_MAX) { for (unsigned int spriteIndex2 = 0; spriteIndex2 < spriteIndex; spriteIndex2++) { free(sprites[spriteIndex2].frames); } return NULL; } sprites[spriteIndex].frames = malloc(sprites[spriteIndex].frameCount * sizeof(*sprites[spriteIndex].frames)); for (unsigned int frameIndex = 0; frameIndex < sprites[spriteIndex].frameCount; frameIndex++) { call_virtual(beginStructure, context, NULL); sprites[spriteIndex].frames[frameIndex].imageID = call_virtual(readUInt32, context, "image_id"); sprites[spriteIndex].frames[frameIndex].offset.x = call_virtual(readInt16, context, "offset_x"); sprites[spriteIndex].frames[frameIndex].offset.y = call_virtual(readInt16, context, "offset_y"); sprites[spriteIndex].frames[frameIndex].duration = call_virtual(readUInt32, context, "duration"); sprites[spriteIndex].frames[frameIndex].imageIndex = UINT_MAX; call_virtual(endStructure, context); } sprites[spriteIndex].totalBounds = RECT4i_EMPTY; call_virtual(endArray, context); call_virtual(endStructure, context); } call_virtual(endArray, context); if (context->status != SERIALIZATION_ERROR_OK) { for (unsigned int spriteIndex = 0; spriteIndex < spriteCount; spriteIndex++) { free(sprites[spriteIndex].frames); } return NULL; } return SpriteCollection_create(identifier, name, spriteCount, sprites); } SpriteCollection * SpriteCollection_deserialize(compat_type(DeserializationContext *) deserializationContext) { deserialize_implementation_v2(SpriteCollection, SPRITE_COLLECTION); } void SpriteCollection_serializeMinimal(SpriteCollection * spriteCollection, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(writeUInt32, context, "id", spriteCollection->identifier); call_virtual(writeString, context, "name", spriteCollection->name); call_virtual(beginArray, context, "sprites"); for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { call_virtual(beginStructure, context, NULL); call_virtual(writeUInt32, context, "id", spriteCollection->sprites[spriteIndex].identifier); call_virtual(writeString, context, "name", spriteCollection->sprites[spriteIndex].name); call_virtual(writeInt16, context, "origin_x", spriteCollection->sprites[spriteIndex].origin.x); call_virtual(writeInt16, context, "origin_y", spriteCollection->sprites[spriteIndex].origin.y); call_virtual(beginArray, context, "frames"); for (unsigned int frameIndex = 0; frameIndex < spriteCollection->sprites[spriteIndex].frameCount; frameIndex++) { call_virtual(beginStructure, context, NULL); call_virtual(writeUInt32, context, "image_id", spriteCollection->sprites[spriteIndex].frames[frameIndex].imageID); call_virtual(writeInt16, context, "offset_x", spriteCollection->sprites[spriteIndex].frames[frameIndex].offset.x); call_virtual(writeInt16, context, "offset_y", spriteCollection->sprites[spriteIndex].frames[frameIndex].offset.y); call_virtual(writeUInt32, context, "duration", spriteCollection->sprites[spriteIndex].frames[frameIndex].duration); call_virtual(endStructure, context); } call_virtual(endArray, context); call_virtual(endStructure, context); } call_virtual(endArray, context); } void SpriteCollection_serialize(SpriteCollection * spriteCollection, compat_type(SerializationContext *) serializationContext) { serialize_implementation_v2(SpriteCollection, spriteCollection, SPRITE_COLLECTION); } void SpriteCollection_resolveImageIndexes(SpriteCollection * spriteCollection, ImageCollection * imageCollection) { for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { for (unsigned int frameIndex = 0; frameIndex < spriteCollection->sprites[spriteIndex].frameCount; frameIndex++) { spriteCollection->sprites[spriteIndex].frames[frameIndex].imageIndex = ImageCollection_getImageIndex(imageCollection, spriteCollection->sprites[spriteIndex].frames[frameIndex].imageID); } } } void SpriteCollection_calculateTotalBounds(SpriteCollection * spriteCollection, ImageCollection * imageCollection) { for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { Sprite_calculateTotalBounds(&spriteCollection->sprites[spriteIndex], imageCollection); } } SpriteID SpriteCollection_getUnusedSpriteID(SpriteCollection * spriteCollection, SpriteID afterSpriteID) { if (afterSpriteID == UINT32_MAX) { SpriteID spriteIDMax = 0; for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { if (spriteCollection->sprites[spriteIndex].identifier > spriteIDMax) { spriteIDMax = spriteCollection->sprites[spriteIndex].identifier; } } return spriteIDMax + 1; } SpriteID closestSpriteID = afterSpriteID + 1; bool occupied; do { occupied = false; for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { if (spriteCollection->sprites[spriteIndex].identifier == closestSpriteID) { occupied = true; closestSpriteID++; } } } while (occupied); return closestSpriteID; } Sprite * SpriteCollection_getSprite(SpriteCollection * spriteCollection, SpriteID spriteID) { for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { if (spriteCollection->sprites[spriteIndex].identifier == spriteID) { return &spriteCollection->sprites[spriteIndex]; } } return NULL; } unsigned int SpriteCollection_getSpriteIndex(SpriteCollection * spriteCollection, SpriteID spriteID) { for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { if (spriteCollection->sprites[spriteIndex].identifier == spriteID) { return spriteIndex; } } return UINT_MAX; } unsigned int SpriteCollection_getSpriteIndexWithName(SpriteCollection * spriteCollection, const char * spriteName) { for (unsigned int spriteIndex = 0; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { if (!strcmp(spriteCollection->sprites[spriteIndex].name, spriteName)) { return spriteIndex; } } return UINT_MAX; } void SpriteCollection_addSprite(SpriteCollection * spriteCollection, Sprite sprite) { SpriteCollection_insertSprite(spriteCollection, spriteCollection->spriteCount, sprite); } void SpriteCollection_insertSprite(SpriteCollection * spriteCollection, unsigned int spriteIndex, Sprite sprite) { assert(spriteIndex <= spriteCollection->spriteCount); spriteCollection->sprites = realloc(spriteCollection->sprites, (spriteCollection->spriteCount + 1) * sizeof(*spriteCollection->sprites)); for (unsigned int spriteIndex2 = spriteCollection->spriteCount; spriteIndex2 > spriteIndex; spriteIndex2--) { spriteCollection->sprites[spriteIndex2] = spriteCollection->sprites[spriteIndex2 - 1]; } spriteCollection->sprites[spriteIndex] = sprite; spriteCollection->spriteCount++; } void SpriteCollection_removeSpriteAtIndex(SpriteCollection * spriteCollection, unsigned int spriteIndex) { assert(spriteIndex < spriteCollection->spriteCount); free(spriteCollection->sprites[spriteIndex].name); free(spriteCollection->sprites[spriteIndex].frames); spriteCollection->spriteCount--; for (; spriteIndex < spriteCollection->spriteCount; spriteIndex++) { spriteCollection->sprites[spriteIndex] = spriteCollection->sprites[spriteIndex + 1]; } } void SpriteCollection_reorderSprite(SpriteCollection * spriteCollection, unsigned int fromIndex, unsigned int toIndex) { assert(fromIndex < spriteCollection->spriteCount); assert(toIndex < spriteCollection->spriteCount); Sprite swap = spriteCollection->sprites[fromIndex]; int direction = (toIndex > fromIndex) * 2 - 1; for (unsigned int swapIndex = fromIndex; swapIndex != toIndex; swapIndex += direction) { spriteCollection->sprites[swapIndex] = spriteCollection->sprites[swapIndex + direction]; } spriteCollection->sprites[toIndex] = swap; } Sprite Sprite_copy(Sprite * sprite) { return (Sprite) {sprite->identifier, strdup(sprite->name), sprite->origin, sprite->frameCount, memdup(sprite->frames, sprite->frameCount * sizeof(*sprite->frames)), sprite->totalBounds}; } unsigned int Sprite_getTotalDuration(Sprite * sprite) { unsigned int duration = 0; for (unsigned int frameIndex = 0; frameIndex < sprite->frameCount; frameIndex++) { duration += sprite->frames[frameIndex].duration; } return duration; } SpriteAnimationFrame * Sprite_getFrameAtTime(Sprite * sprite, int frameTime, bool loop) { return &sprite->frames[Sprite_getFrameIndexAtTime(sprite, frameTime, loop)]; } unsigned int Sprite_getFrameIndexAtTime(Sprite * sprite, int frameTime, bool loop) { unsigned int duration = 0; unsigned int effectiveFrameTime = 0; if (loop) { unsigned int totalDuration = Sprite_getTotalDuration(sprite); frameTime %= totalDuration; if (frameTime < 0) { frameTime = totalDuration + frameTime; } else { effectiveFrameTime = frameTime; } } else { if (frameTime >= 0) { effectiveFrameTime = frameTime; } } for (unsigned int frameIndex = 0; frameIndex < sprite->frameCount; frameIndex++) { if (duration + sprite->frames[frameIndex].duration > effectiveFrameTime) { return frameIndex; } duration += sprite->frames[frameIndex].duration; } return sprite->frameCount - 1; } void Sprite_addFrame(Sprite * sprite, SpriteAnimationFrame frame) { Sprite_insertFrame(sprite, sprite->frameCount, frame); } void Sprite_insertFrame(Sprite * sprite, unsigned int frameIndex, SpriteAnimationFrame frame) { assert(frameIndex <= sprite->frameCount); sprite->frames = realloc(sprite->frames, (sprite->frameCount + 1) * sizeof(*sprite->frames)); for (unsigned int frameIndex2 = sprite->frameCount; frameIndex2 > frameIndex; frameIndex2--) { sprite->frames[frameIndex2] = sprite->frames[frameIndex2 - 1]; } sprite->frames[frameIndex] = frame; sprite->frameCount++; } void Sprite_removeFrameAtIndex(Sprite * sprite, unsigned int frameIndex) { assert(frameIndex < sprite->frameCount); sprite->frameCount--; for (; frameIndex < sprite->frameCount; frameIndex++) { sprite->frames[frameIndex] = sprite->frames[frameIndex + 1]; } } void Sprite_reorderFrame(Sprite * sprite, unsigned int fromIndex, unsigned int toIndex) { assert(fromIndex < sprite->frameCount); assert(toIndex < sprite->frameCount); SpriteAnimationFrame swap = sprite->frames[fromIndex]; int direction = (toIndex > fromIndex) * 2 - 1; for (unsigned int swapIndex = fromIndex; swapIndex != toIndex; swapIndex += direction) { sprite->frames[swapIndex] = sprite->frames[swapIndex + direction]; } sprite->frames[toIndex] = swap; } void Sprite_calculateTotalBounds(Sprite * sprite, ImageCollection * imageCollection) { sprite->totalBounds = RECT4i_EMPTY; for (unsigned int frameIndex = 0; frameIndex < sprite->frameCount; frameIndex++) { if (sprite->frames[frameIndex].imageIndex < imageCollection->imageCount) { sprite->totalBounds = Rect4i_union(sprite->totalBounds, Rect4i_fromPositionAndSize(sprite->frames[frameIndex].offset, VECTOR2i(imageCollection->images[sprite->frames[frameIndex].imageIndex].size.x, imageCollection->images[sprite->frames[frameIndex].imageIndex].size.y))); } } }