Dawn/src/dawn/display/Tileset.cpp

105 lines
3.2 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Tileset.hpp"
using namespace Dawn;
struct Tile Tileset::getTile(int32_t tile) {
assertTrue(tile >= 0, "Tileset::getTile: Tile must be greater than or equal to 0");
assertTrue(tile < this->tiles.size(), "Tileset::getTile: Tile is out of bounds");
return this->tiles[tile];
}
TilesetGrid::TilesetGrid() {
}
TilesetGrid::TilesetGrid(
Texture *texture,
int32_t columns,
int32_t rows
) : TilesetGrid(
columns, rows,
texture->getWidth(), texture->getHeight(),
0, 0,
0, 0
) {
}
TilesetGrid::TilesetGrid(
Texture &texture,
int32_t columns,
int32_t rows
) : TilesetGrid(
columns, rows,
texture.getWidth(), texture.getHeight(),
0, 0,
0, 0
) {
}
TilesetGrid::TilesetGrid(
int32_t columns,
int32_t rows,
int32_t w,
int32_t h,
int32_t gapX,
int32_t gapY,
int32_t borderX,
int32_t borderY
) {
assertTrue(columns >= 1, "TilesetGrid::TilesetGrid: Columns must be greater than or equal to 1");
assertTrue(rows >= 1, "TilesetGrid::TilesetGrid: Rows must be greater than or equal to 1");
assertTrue(w >= 1, "TilesetGrid::TilesetGrid: Width must be greater than or equal to 1");
assertTrue(h >= 1, "TilesetGrid::TilesetGrid: Height must be greater than or equal to 1");
assertTrue(gapX >= 0, "TilesetGrid::TilesetGrid: GapX must be greater than or equal to 0");
assertTrue(gapY >= 0, "TilesetGrid::TilesetGrid: GapY must be greater than or equal to 0");
assertTrue(borderX >= 0, "TilesetGrid::TilesetGrid: BorderX must be greater than or equal to 0");
assertTrue(borderY >= 0, "TilesetGrid::TilesetGrid: BorderY must be greater than or equal to 0");
assertTrue(w >= (columns + (gapX * columns) + borderX + borderX), "TilesetGrid::TilesetGrid: Width is too small");
assertTrue(h >= (rows + (gapY * rows) + borderY + borderY), "TilesetGrid::TilesetGrid: Height is too small");
this->rows = rows;
this->columns = columns;
// Calculate division sizes (pixels)
this->divX = (w - (borderX * 2) - (gapX * (columns - 1))) / columns;
this->divY = (h - (borderY * 2) - (gapY * (rows - 1))) / rows;
// Calculate the division sizes (units)
float_t tdivX = (float_t)this->divX / (float_t)w;
float_t tdivY = (float_t)this->divY / (float_t)h;
for(int32_t y = 0; y < rows; y++) {
for(int32_t x = 0; x < columns; x++) {
struct Tile tile;
tile.uv0.x = (borderX + ((float_t)this->divX * x) + (gapX * x)) / w;
tile.uv1.x = tile.uv0.x + tdivX;
tile.uv0.y = (borderY + ((float_t)this->divY * y) + (gapY * y)) / h;
tile.uv1.y = tile.uv0.y + tdivY;
this->tiles.push_back(tile);
}
}
}
float_t TilesetGrid::getTileWidth(int32_t tile) {
return this->divX;
}
float_t TilesetGrid::getTileHeight(int32_t tile) {
return this->divY;
}
struct Tile TilesetGrid::getTileFromGrid(int32_t column, int32_t row) {
assertTrue(row > 0 && row < this->rows, "TilesetGrid::getTileFromGrid: Row is out of bounds");
assertTrue(column > 0 && column < this->columns, "TilesetGrid::getTileFromGrid: Column is out of bounds");
return this->getTile(row + (column * this->rows));
}