/* 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/TilesetAdjacencyBlendMap.h" #include "utilities/IOUtilities.h" #include #include TilesetAdjacencyBlendMap * TilesetAdjacencyBlendMap_create(TilesetAdjacencyBlendMapID identifier, const char * name, unsigned int pairCount, TileAdjacencyPair * pairs) { TilesetAdjacencyBlendMap * blendMap = malloc(sizeof(*blendMap)); blendMap->identifier = identifier; blendMap->name = strdup_nullSafe(name); blendMap->pairCount = pairCount; blendMap->pairs = memdup(pairs, pairCount * sizeof(*pairs)); return blendMap; } TilesetAdjacencyBlendMap * TilesetAdjacencyBlendMap_copy(TilesetAdjacencyBlendMap * blendMap) { return TilesetAdjacencyBlendMap_create(blendMap->identifier, blendMap->name, blendMap->pairCount, blendMap->pairs); } void TilesetAdjacencyBlendMap_dispose(TilesetAdjacencyBlendMap * blendMap) { free(blendMap->name); free(blendMap); } void TilesetAdjacencyBlendMap_serializeMinimal(TilesetAdjacencyBlendMap * blendMap, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(writeUInt32, context, "id", blendMap->identifier); call_virtual(writeString, context, "name", blendMap->name); call_virtual(beginArray, context, "pairs"); for (unsigned int pairIndex = 0; pairIndex < blendMap->pairCount; pairIndex++) { call_virtual(beginStructure, context, NULL); call_virtual(writeUInt32, context, "tile_1", blendMap->pairs[pairIndex].tileID1); call_virtual(writeUInt32, context, "tile_2", blendMap->pairs[pairIndex].tileID2); call_virtual(writeUInt32, context, "image_id", blendMap->pairs[pairIndex].sliceImageID); call_virtual(endStructure, context); } call_virtual(endArray, context); } void TilesetAdjacencyBlendMap_serialize(TilesetAdjacencyBlendMap * blendMap, compat_type(SerializationContext *) serializationContext) { serialize_implementation_v2(TilesetAdjacencyBlendMap, blendMap, TILESET_ADJACENCY_BLEND_MAP); } TilesetAdjacencyBlendMap * TilesetAdjacencyBlendMap_deserializeMinimal(compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { DeserializationContext * context = deserializationContext; if (context->status != SERIALIZATION_ERROR_OK || formatVersion > TILESET_ADJACENCY_BLEND_MAP_FORMAT_VERSION) { return NULL; } TilesetAdjacencyBlendMap * blendMap = TilesetAdjacencyBlendMap_create(0, NULL, 0, NULL); blendMap->identifier = call_virtual(readUInt32, context, "id"); blendMap->name = strdup_nullSafe(call_virtual(readString, context, "name")); blendMap->pairCount = call_virtual(beginArray, context, "pairs"); if (blendMap->pairCount > 0) { if (blendMap->pairCount > TILESET_ADJACENCY_BLEND_MAP_PAIR_COUNT_MAX) { blendMap->pairCount = 0; TilesetAdjacencyBlendMap_dispose(blendMap); return NULL; } blendMap->pairs = malloc(blendMap->pairCount * sizeof(*blendMap->pairs)); for (unsigned int pairIndex = 0; pairIndex < blendMap->pairCount; pairIndex++) { call_virtual(beginStructure, context, NULL); blendMap->pairs[pairIndex].tileID1 = call_virtual(readUInt32, context, "tile_1"); blendMap->pairs[pairIndex].tileID2 = call_virtual(readUInt32, context, "tile_2"); blendMap->pairs[pairIndex].sliceImageID = call_virtual(readUInt32, context, "image_id"); call_virtual(endStructure, context); } } call_virtual(endArray, context); if (context->status != SERIALIZATION_ERROR_OK) { TilesetAdjacencyBlendMap_dispose(blendMap); return NULL; } return blendMap; } TilesetAdjacencyBlendMap * TilesetAdjacencyBlendMap_deserialize(compat_type(DeserializationContext *) deserializationContext) { deserialize_implementation_v2(TilesetAdjacencyBlendMap, TILESET_ADJACENCY_BLEND_MAP); } void TilesetAdjacencyBlendMap_addTileAdjacencyPair(TilesetAdjacencyBlendMap * blendMap, TileAdjacencyPair adjacency) { TilesetAdjacencyBlendMap_insertTileAdjacencyPair(blendMap, blendMap->pairCount, adjacency); } void TilesetAdjacencyBlendMap_insertTileAdjacencyPair(TilesetAdjacencyBlendMap * blendMap, unsigned int pairIndex, TileAdjacencyPair adjacency) { assert(pairIndex <= blendMap->pairCount); blendMap->pairs = realloc(blendMap->pairs, (blendMap->pairCount + 1) * sizeof(*blendMap->pairs)); for (unsigned int pairIndex2 = blendMap->pairCount; pairIndex2 > pairIndex; pairIndex2--) { blendMap->pairs[pairIndex2] = blendMap->pairs[pairIndex2 - 1]; } blendMap->pairs[pairIndex] = adjacency; blendMap->pairCount++; } TileAdjacencyPair * TilesetAdjacencyBlendMap_getTileAdjacencyPair(TilesetAdjacencyBlendMap * blendMap, TileID tileID1, TileID tileID2, unsigned int * outPairIndex) { for (unsigned int pairIndex = 0; pairIndex < blendMap->pairCount; pairIndex++) { if ((blendMap->pairs[pairIndex].tileID1 == tileID1 && blendMap->pairs[pairIndex].tileID2 == tileID2) || (blendMap->pairs[pairIndex].tileID1 == tileID2 && blendMap->pairs[pairIndex].tileID2 == tileID1)) { if (outPairIndex != NULL) { *outPairIndex = pairIndex; } return &blendMap->pairs[pairIndex]; } } if (outPairIndex != NULL) { *outPairIndex = UINT_MAX; } return NULL; } void TilesetAdjacencyBlendMap_removeTileAdjacencyPairAtIndex(TilesetAdjacencyBlendMap * blendMap, unsigned int pairIndex) { if (pairIndex < blendMap->pairCount) { blendMap->pairCount--; for (; pairIndex < blendMap->pairCount; pairIndex++) { blendMap->pairs[pairIndex] = blendMap->pairs[pairIndex + 1]; } } } void TilesetAdjacencyBlendMap_reorderTileAdjacencyPair(TilesetAdjacencyBlendMap * blendMap, unsigned int fromPairIndex, unsigned int toPairIndex) { assert(fromPairIndex < blendMap->pairCount); assert(toPairIndex < blendMap->pairCount); TileAdjacencyPair swap = blendMap->pairs[fromPairIndex]; int direction = (toPairIndex > fromPairIndex) * 2 - 1; for (unsigned int swapIndex = fromPairIndex; swapIndex != toPairIndex; swapIndex += direction) { blendMap->pairs[swapIndex] = blendMap->pairs[swapIndex + direction]; } blendMap->pairs[toPairIndex] = swap; }