/* 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 "gamemath/Grid.h" #include #include void clampGridCopyRegion(int sourceX, int sourceY, int targetX, int targetY, unsigned int sourceWidth, unsigned int sourceHeight, unsigned int targetWidth, unsigned int targetHeight, unsigned int * ioWidth, unsigned int * ioHeight, unsigned int * outStartX, unsigned int * outStartY) { unsigned int width = *ioWidth; unsigned int height = *ioHeight; unsigned int startX = 0, startY = 0; if (sourceX + (int) width > (int) sourceWidth) { if (sourceX > (int) sourceWidth) { width = 0; } else { width = (int) sourceWidth - sourceX; } } if (sourceY + (int) height > (int) sourceHeight) { if (sourceY > (int) sourceHeight) { height = 0; } else { height = (int) sourceHeight - sourceY; } } if (targetX + (int) width > (int) targetWidth) { if (targetX > (int) targetWidth) { width = 0; } else { width = (int) targetWidth - targetX; } } if (targetY + (int) height > (int) targetHeight) { if (targetY > (int) targetHeight) { height = 0; } else { height = (int) targetHeight - targetY; } } if (sourceX < 0) { startX = -sourceX; } if (targetX < 0 && targetX < sourceX) { startX = -targetX; } if (sourceY < 0) { startY = -sourceY; } if (targetY < 0 && targetY < sourceY) { startY = -targetY; } *ioWidth = width; *ioHeight = height; *outStartX = startX; *outStartY = startY; } void copyGridCells(void * target, void * source, unsigned int sourceWidth, unsigned int sourceHeight, unsigned int targetWidth, unsigned int targetHeight, unsigned int bytesPerCell, int sourceX, int sourceY, int targetX, int targetY, unsigned int copyWidth, unsigned int copyHeight) { unsigned int startX = 0, startY = 0; clampGridCopyRegion(sourceX, sourceY, targetX, targetY, sourceWidth, sourceHeight, targetWidth, targetHeight, ©Width, ©Height, &startX, &startY); if (copyWidth > startX && copyHeight > 0) { target += (int) ((targetY * targetWidth + targetX + startX) * bytesPerCell); source += (int) ((sourceY * sourceWidth + sourceX + startX) * bytesPerCell); sourceWidth *= bytesPerCell; targetWidth *= bytesPerCell; copyWidth -= startX; copyWidth *= bytesPerCell; for (unsigned int rowIndex = startY; rowIndex < copyHeight; rowIndex++) { memcpy(target + rowIndex * targetWidth, source + rowIndex * sourceWidth, copyWidth); } } } #define flipHorizontal_implementation(primitive_type) { \ primitive_type * cells = cellsUntyped, swap; \ for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { \ for (unsigned int columnIndex = 0; columnIndex < width / 2; columnIndex++) { \ swap = cells[rowIndex * width + columnIndex]; \ cells[rowIndex * width + columnIndex] = cells[rowIndex * width + (width - columnIndex - 1)]; \ cells[rowIndex * width + (width - columnIndex - 1)] = swap; \ } \ } \ } void flipGridHorizontal(void * cellsUntyped, unsigned int width, unsigned int height, unsigned int bytesPerCell) { switch (bytesPerCell) { case 1: flipHorizontal_implementation(uint8_t); break; case 2: flipHorizontal_implementation(uint16_t); break; case 4: flipHorizontal_implementation(uint32_t); break; case 8: flipHorizontal_implementation(uint64_t); break; default: { uint8_t * cells = cellsUntyped, swap; for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { for (unsigned int columnIndex = 0; columnIndex < width / 2; columnIndex++) { for (unsigned int byteIndex = 0; byteIndex < bytesPerCell; byteIndex++) { swap = cells[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex]; cells[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex] = cells[(rowIndex * width + (width - columnIndex - 1)) * bytesPerCell + byteIndex]; cells[(rowIndex * width + (width - columnIndex - 1)) * bytesPerCell + byteIndex] = swap; } } } break; } } } void flipGridVertical(void * cells, unsigned int width, unsigned int height, unsigned int bytesPerCell) { uint8_t swapRow[width * bytesPerCell]; for (unsigned int rowIndex = 0; rowIndex < height / 2; rowIndex++) { memcpy(swapRow, cells + rowIndex * width * bytesPerCell, width * bytesPerCell); memcpy(cells + rowIndex * width * bytesPerCell, cells + (height - rowIndex - 1) * width * bytesPerCell, width * bytesPerCell); memcpy(cells + (height - rowIndex - 1) * width * bytesPerCell, swapRow, width * bytesPerCell); } } #define rotate90CW_implementation(primitive_type) { \ primitive_type * cells = cellsUntyped; \ primitive_type cellsCopy[width * height * bytesPerCell]; \ memcpy(cellsCopy, cells, width * height * bytesPerCell); \ for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { \ for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { \ cells[columnIndex * height + (height - rowIndex - 1)] = cellsCopy[rowIndex * width + columnIndex]; \ } \ } \ } void rotateGrid90CW(void * cellsUntyped, unsigned int width, unsigned int height, unsigned int bytesPerCell) { switch (bytesPerCell) { case 1: rotate90CW_implementation(uint8_t); break; case 2: rotate90CW_implementation(uint16_t); break; case 4: rotate90CW_implementation(uint32_t); break; case 8: rotate90CW_implementation(uint64_t); break; default: { uint8_t * cells = cellsUntyped; uint8_t cellsCopy[width * height * bytesPerCell]; memcpy(cellsCopy, cells, width * height * bytesPerCell); for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { for (unsigned int byteIndex = 0; byteIndex < bytesPerCell; byteIndex++) { cells[(columnIndex * height + (height - rowIndex - 1)) * bytesPerCell + byteIndex] = cellsCopy[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex]; } } } break; } } } #define rotate90CCW_implementation(primitive_type) { \ primitive_type * cells = cellsUntyped; \ primitive_type cellsCopy[width * height * bytesPerCell]; \ memcpy(cellsCopy, cells, width * height * bytesPerCell); \ for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { \ for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { \ cells[(width - columnIndex - 1) * height + rowIndex] = cellsCopy[rowIndex * width + columnIndex]; \ } \ } \ } void rotateGrid90CCW(void * cellsUntyped, unsigned int width, unsigned int height, unsigned int bytesPerCell) { switch (bytesPerCell) { case 1: rotate90CCW_implementation(uint8_t); break; case 2: rotate90CCW_implementation(uint16_t); break; case 4: rotate90CCW_implementation(uint32_t); break; case 8: rotate90CCW_implementation(uint64_t); break; default: { uint8_t * cells = cellsUntyped; uint8_t cellsCopy[width * height * bytesPerCell]; memcpy(cellsCopy, cells, width * height * bytesPerCell); for (unsigned int rowIndex = 0; rowIndex < height; rowIndex++) { for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { for (unsigned int byteIndex = 0; byteIndex < bytesPerCell; byteIndex++) { cells[((width - columnIndex - 1) * height + rowIndex) * bytesPerCell + byteIndex] = cellsCopy[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex]; } } } break; } } } #define rotate180_implementation(primitive_type) { \ primitive_type * cells = cellsUntyped, swap; \ for (unsigned int rowIndex = 0; rowIndex < height / 2; rowIndex++) { \ for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { \ swap = cells[rowIndex * width + columnIndex]; \ cells[rowIndex * width + columnIndex] = cells[(height - rowIndex - 1) * width + (width - columnIndex - 1)]; \ cells[(height - rowIndex - 1) * width + (width - columnIndex - 1)] = swap; \ } \ } \ if (height % 2) { \ unsigned int rowIndex = height / 2; \ for (unsigned int columnIndex = 0; columnIndex < width / 2; columnIndex++) { \ swap = cells[rowIndex * width + columnIndex]; \ cells[rowIndex * width + columnIndex] = cells[(height - rowIndex - 1) * width + (width - columnIndex - 1)]; \ cells[(height - rowIndex - 1) * width + (width - columnIndex - 1)] = swap; \ } \ } \ } void rotateGrid180(void * cellsUntyped, unsigned int width, unsigned int height, unsigned int bytesPerCell) { switch (bytesPerCell) { case 1: rotate180_implementation(uint8_t); break; case 2: rotate180_implementation(uint16_t); break; case 4: rotate180_implementation(uint32_t); break; case 8: rotate180_implementation(uint64_t); break; default: { uint8_t * cells = cellsUntyped, swap; for (unsigned int rowIndex = 0; rowIndex < height / 2; rowIndex++) { for (unsigned int columnIndex = 0; columnIndex < width; columnIndex++) { for (unsigned int byteIndex = 0; byteIndex < bytesPerCell; byteIndex++) { swap = cells[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex]; cells[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex] = cells[((height - rowIndex - 1) * width + (width - columnIndex - 1)) * bytesPerCell + byteIndex]; cells[((height - rowIndex - 1) * width + (width - columnIndex - 1)) * bytesPerCell + byteIndex] = swap; } } } if (height % 2) { unsigned int rowIndex = height / 2; for (unsigned int columnIndex = 0; columnIndex < width / 2; columnIndex++) { for (unsigned int byteIndex = 0; byteIndex < bytesPerCell; byteIndex++) { swap = cells[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex]; cells[(rowIndex * width + columnIndex) * bytesPerCell + byteIndex] = cells[((height - rowIndex - 1) * width + (width - columnIndex - 1)) * bytesPerCell + byteIndex]; cells[((height - rowIndex - 1) * width + (width - columnIndex - 1)) * bytesPerCell + byteIndex] = swap; } } } break; } } }