Dawn/src/dawn/display/Tileset.hpp

83 lines
2.2 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
struct Tile {
public:
glm::vec2 uv0;
glm::vec2 uv1;
};
struct Tileset {
public:
std::vector<struct Tile> tiles;
};
struct TilesetGrid : public Tileset{
public:
int32_t rows;
int32_t columns;
TilesetGrid(
int32_t columns,
int32_t rows,
int32_t width,
int32_t height,
int32_t gapX,
int32_t gapY,
int32_t borderX,
int32_t borderY
) {
assertTrue(columns >= 1);
assertTrue(rows >= 1);
assertTrue(width >= 1);
assertTrue(height >= 1);
assertTrue(gapX >= 0);
assertTrue(gapY >= 0);
assertTrue(borderX >= 0);
assertTrue(borderY >= 0);
assertTrue(width >= (columns + (gapX * columns) + borderX + borderX));
assertTrue(height >= (rows + (gapY * rows) + borderY + borderY));
this->rows = rows;
this->columns = columns;
// Calculate division sizes (pixels)
float_t divX = (
(float_t)width - ((float_t)borderX * 2.0f) -
((float_t)gapX * ((float_t)columns - 1))
) / columns;
float_t divY = (
(float_t)height - ((float_t)borderY * 2.0f) -
((float_t)gapY * ((float_t)rows - 1))
) / rows;
// Calculate the division sizes (units)
float_t tdivX = divX / (float_t)width;
float_t tdivY = divY / (float_t)height;
for(int32_t y = 0; y < rows; y++) {
for(int32_t x = 0; x < columns; x++) {
struct Tile tile;
tile.uv0.x = (borderX + (divX * x) + (gapX * x)) / width;
tile.uv1.x = tile.uv0.x + tdivX;
tile.uv0.y = (borderY + (divY * y) + (gapY * y)) / height;
tile.uv1.y = tile.uv0.y + tdivY;
this->tiles.push_back(tile);
}
}
}
struct Tile getTile(int32_t row, int32_t column) {
assertTrue(row > 0 && row < this->rows);
assertTrue(column > 0 && column < this->columns);
return this->tiles[row + (column * this->rows)];
}
};
}