/* 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 "gamemath/Rect4i.h" #include "tileset/LayeredTileMap.h" #include "utilities/IOUtilities.h" #include LayeredTileMap * LayeredTileMap_create(const char * tilesetName, Vector2i origin, unsigned int width, unsigned int height, unsigned int layerCount, struct LayeredTileMap_layer * layers, bool copyLayers) { LayeredTileMap * tileMapData = malloc(sizeof(*tileMapData)); tileMapData->tilesetName = strdup_nullSafe(tilesetName); tileMapData->origin = origin; tileMapData->width = width; tileMapData->height = height; tileMapData->layerCount = layerCount; if (layerCount == 0) { tileMapData->layers = NULL; } else { tileMapData->layers = malloc(sizeof(*tileMapData->layers) * layerCount); if (copyLayers) { for (unsigned int layerIndex = 0; layerIndex < layerCount; layerIndex++) { tileMapData->layers[layerIndex].name = strdup_nullSafe(layers[layerIndex].name); tileMapData->layers[layerIndex].offset = layers[layerIndex].offset; tileMapData->layers[layerIndex].tileMap = TileMap_copy(layers[layerIndex].tileMap); } } else { memcpy(tileMapData->layers, layers, sizeof(*tileMapData->layers) * layerCount); } } return tileMapData; } void LayeredTileMap_dispose(LayeredTileMap * tileMapData) { free(tileMapData->tilesetName); for (unsigned int layerIndex = 0; layerIndex < tileMapData->layerCount; layerIndex++) { free(tileMapData->layers[layerIndex].name); TileMap_dispose(tileMapData->layers[layerIndex].tileMap); } free(tileMapData->layers); free(tileMapData); } LayeredTileMap * LayeredTileMap_deserialize(compat_type(DeserializationContext *) deserializationContext) { DeserializationContext * context = deserializationContext; call_virtual(beginStructure, context, TILE_MAP_DATA_FORMAT_TYPE); const char * formatType = call_virtual(readString, context, "format_type"); if (context->status != SERIALIZATION_ERROR_OK || strcmp(formatType, TILE_MAP_DATA_FORMAT_TYPE)) { return false; } uint16_t formatVersion = call_virtual(readUInt16, context, "format_version"); if (context->status != SERIALIZATION_ERROR_OK || formatVersion > TILE_MAP_DATA_FORMAT_VERSION) { return false; } const char * tilesetName = call_virtual(readStringNullable, context, "tileset_name"); Vector2i origin; origin.x = call_virtual(readInt32, context, "origin_x"); origin.y = call_virtual(readInt32, context, "origin_y"); unsigned int width = call_virtual(readUInt32, context, "width"); unsigned int height = call_virtual(readUInt32, context, "height"); unsigned int layerCount = call_virtual(beginArray, context, "layers"); if (layerCount == 0 || context->status != SERIALIZATION_ERROR_OK) { return NULL; } struct LayeredTileMap_layer layers[layerCount]; for (unsigned int layerIndex = 0; layerIndex < layerCount; layerIndex++) { call_virtual(beginStructure, context, NULL); layers[layerIndex].name = (char *) call_virtual(readStringNullable, context, "name"); layers[layerIndex].offset.x = call_virtual(readInt32, context, "offset_x"); layers[layerIndex].offset.y = call_virtual(readInt32, context, "offset_y"); call_virtual(readBoolean, context, "visible"); if (formatVersion > 0) { call_virtual(readBoolean, context, "write_enabled"); } call_virtual(readFloat, context, "opacity"); unsigned int layerWidth = call_virtual(readUInt32, context, "width"); unsigned int layerHeight = call_virtual(readUInt32, context, "height"); size_t blobLength; const void * blobData = call_virtual(readBlob, context, "tiles", &blobLength); if (blobLength != layerWidth * layerHeight * 4) { for (unsigned int layerIndex2 = 0; layerIndex2 < layerIndex; layerIndex2++) { TileMap_dispose(layers[layerIndex2].tileMap); } return NULL; } layers[layerIndex].tileMap = TileMap_create(layerWidth, layerHeight); memcpy(layers[layerIndex].tileMap->tiles, blobData, blobLength); #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ for (unsigned int tileIndex = 0; tileIndex < layerWidth * layerHeight; tileIndex++) { layers[layerIndex].tileMap->tiles[tileIndex] = swapEndian32(layers[layerIndex].tileMap->tiles[tileIndex]); } #endif call_virtual(endStructure, context); } call_virtual(endArray, context); call_virtual(endStructure, context); if (context->status != SERIALIZATION_ERROR_OK) { for (unsigned int layerIndex = 0; layerIndex < layerCount; layerIndex++) { TileMap_dispose(layers[layerIndex].tileMap); } return NULL; } for (unsigned int layerIndex = 0; layerIndex < layerCount; layerIndex++) { layers[layerIndex].name = strdup_nullSafe(layers[layerIndex].name); } return LayeredTileMap_create(tilesetName, origin, width, height, layerCount, layers, false); }