/* Copyright (c) 2022 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/TileMapDiff.h" #include "utilities/IOUtilities.h" #include #include #include #define stemobject_implementation TileMapDiff stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); TileMapDiff * TileMapDiff_create(TileMap * newTileMap, TileMap * oldTileMap) { stemobject_create_implementation(init, newTileMap, oldTileMap) } bool TileMapDiff_init(TileMapDiff * self, TileMap * newTileMap, TileMap * oldTileMap) { call_super(init, self); self->oldWidth = oldTileMap->width; self->newWidth = newTileMap->width; self->oldHeight = oldTileMap->height; self->newHeight = newTileMap->height; self->oldTiles = NULL; self->newTiles = NULL; self->xorTiles = NULL; if (self->oldWidth != self->newWidth || self->oldHeight != self->newHeight) { self->oldTiles = memdup(oldTileMap->tiles, oldTileMap->width * oldTileMap->height * sizeof(oldTileMap->tiles[0])); self->newTiles = memdup(newTileMap->tiles, newTileMap->width * newTileMap->height * sizeof(oldTileMap->tiles[0])); } else { unsigned int width = newTileMap->width; unsigned int height = newTileMap->height; TileID * oldTiles = oldTileMap->tiles; TileID * newTiles = newTileMap->tiles; self->deltaMinY = UINT_MAX; for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { if (memcmp(oldTiles + rowIndex * width, newTiles + rowIndex * width, width * sizeof(oldTiles[0]))) { self->deltaMinY = rowIndex; break; } } if (self->deltaMinY != UINT_MAX) { self->deltaMaxY = UINT_MAX; for (unsigned int rowIndex = 0; rowIndex < height - self->deltaMinY; rowIndex++) { if (memcmp(oldTiles + (height - 1 - rowIndex) * width, newTiles + (height - 1 - rowIndex) * width, width * sizeof(oldTiles[0]))) { self->deltaMaxY = height - rowIndex; break; } } assert(self->deltaMaxY != UINT_MAX); self->deltaMinX = UINT_MAX; for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { for (unsigned int rowIndex = self->deltaMinY; rowIndex < self->deltaMaxY; rowIndex++) { if (oldTiles[rowIndex * width + columnIndex] != newTiles[rowIndex * width + columnIndex]) { self->deltaMinX = columnIndex; goto minFound; } } } minFound: assert(self->deltaMinX != UINT_MAX); self->deltaMaxX = UINT_MAX; for (unsigned int columnIndex = 0; columnIndex < width - self->deltaMinX; columnIndex++) { for (unsigned int rowIndex = self->deltaMinY; rowIndex < self->deltaMaxY; rowIndex++) { if (oldTiles[rowIndex * width + (width - 1 - columnIndex)] != newTiles[rowIndex * width + (width - 1 - columnIndex)]) { self->deltaMaxX = width - columnIndex; goto maxFound; } } } maxFound: assert(self->deltaMaxX != UINT_MAX); unsigned int deltaWidth = self->deltaMaxX - self->deltaMinX; unsigned int deltaHeight = self->deltaMaxY - self->deltaMinY; self->xorTiles = malloc(sizeof(self->xorTiles[0]) * deltaWidth * deltaHeight); for (unsigned int rowIndex = 0; rowIndex < deltaHeight; rowIndex++) { for (unsigned int columnIndex = 0; columnIndex < deltaWidth; columnIndex++) { self->xorTiles[rowIndex * deltaWidth + columnIndex] = oldTiles[(rowIndex + self->deltaMinY) * width + self->deltaMinX + columnIndex] ^ newTiles[(rowIndex + self->deltaMinY) * width + self->deltaMinX + columnIndex]; } } } } return true; } void TileMapDiff_dispose(TileMapDiff * self) { free(self->oldTiles); free(self->newTiles); free(self->xorTiles); call_super_virtual(dispose, self); } #define applyImplementation(age_to) \ if (self->oldWidth != self->newWidth || self->oldHeight != self->newHeight) { \ target->tiles = realloc(target->tiles, self->age_to##Width * self->age_to##Height * sizeof(self->age_to##Tiles[0])); \ memcpy(target->tiles, self->age_to##Tiles, self->age_to##Width * self->age_to##Height * sizeof(self->age_to##Tiles[0])); \ target->width = self->age_to##Width; \ target->height = self->age_to##Height; \ \ } else if (self->xorTiles != NULL) { \ unsigned int deltaMinX = self->deltaMinX; \ unsigned int deltaMinY = self->deltaMinY; \ unsigned int deltaWidth = self->deltaMaxX - deltaMinX; \ unsigned int deltaHeight = self->deltaMaxY - deltaMinY; \ unsigned int width = target->width; \ for (unsigned int rowIndex = 0; rowIndex < deltaHeight; rowIndex++) { \ for (unsigned int columnIndex = 0; columnIndex < deltaWidth; columnIndex++) { \ target->tiles[(rowIndex + deltaMinY) * width + deltaMinX + columnIndex] ^= self->xorTiles[rowIndex * deltaWidth + columnIndex]; \ } \ } \ } void TileMapDiff_revert(TileMapDiff * self, TileMap * target) { applyImplementation(old); } void TileMapDiff_apply(TileMapDiff * self, TileMap * target) { applyImplementation(new); }