Reshuffled

This commit is contained in:
2021-04-19 21:30:34 +10:00
parent cc94f7c775
commit ff8b84b542
45 changed files with 218 additions and 140 deletions

66
src/display/tileset.c Normal file
View File

@@ -0,0 +1,66 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "tileset.h"
tileset_t * tilesetCreate(
int32_t columns, int32_t rows,
int32_t width, int32_t height,
int32_t gapX, int32_t gapY,
int32_t borderX, int32_t borderY
) {
tileset_t *tileset;
float divX, divY, tdivX;
int32_t x, y, i;
tileset = malloc(sizeof(tileset_t));
if(tileset == NULL) return NULL;
tileset->count = rows * columns;
tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count);
if(tileset->divisions == NULL) {
free(tileset);
return NULL;
}
tileset->columns = columns;
tileset->rows = rows;
// Calculate division sizes (pixels)
divX = (width - (borderX * 2) - (gapX * (columns - 1))) / columns;
divY = (height - (borderY * 2) - (gapY * (rows - 1))) / rows;
// Calculate the division sizes (units)
divX = divX / width;
divY = divY / height;
// Calculate the divisions
i = -1;
for(y = 0; y < rows; y++) {
for(x = 0; x < columns; x++) {
tileset->divisions[++i].x0 = borderX + (divX * x) + (gapX * x);
tileset->divisions[i].y0 = borderY + (divY * y) + (gapY * y);
tileset->divisions[i].x1 = tileset->divisions[i].x0 + divX;
tileset->divisions[i].y1 = tileset->divisions[i].y0 + divY;
}
}
return tileset;
}
tilesetdiv_t tilesetGetDivision(tileset_t *tileset,
int32_t column, int32_t row
) {
return tileset->divisions[
(column % tileset->columns) + (row * tileset->columns)
];
}
void tilesetDispose(tileset_t *tileset) {
free(tileset->divisions);
free(tileset);
}