rutile-game/src/rutile/core/resources/tile_set.cppm

90 lines
2.9 KiB
Text
Raw Normal View History

2026-04-28 19:45:41 +02:00
module;
#include <cassert>
2026-06-16 21:53:19 +02:00
export module rutile.core.resources.tile_set;
2026-04-28 19:45:41 +02:00
2026-05-16 01:58:45 +02:00
import std;
2026-06-16 21:53:19 +02:00
import rutile.core.common;
import rutile.core.resources.texture;
2026-04-28 19:45:41 +02:00
import wrappers.sdl;
export namespace rutile
2026-04-28 19:45:41 +02:00
{
using TileSetID = unsigned int;
using TileID = unsigned int;
struct TileDefinition
{
sdl::FRect atlas_src_rect;
};
class TileSet
{
2026-06-16 21:53:19 +02:00
// Shared pointer to the tileset atlas texture (managed by ResourceServer)
ResourcePtr<Texture> texture_ptr_;
2026-04-28 19:45:41 +02:00
// Size of each tile in pixels
// TODO: Do we need this?
// int tile_size_;
// Tile definitions: Maps tile ID to a struct describing the tile, e.g. the texture source boundaries,
// collision data, etc.
std::vector<TileDefinition> tile_definitions_;
public:
TileSet() = delete;
explicit TileSet(
2026-06-16 21:53:19 +02:00
const ResourcePtr<Texture>& texture_ptr,
2026-04-28 19:45:41 +02:00
std::vector<TileDefinition> tile_definitions
)
2026-06-16 21:53:19 +02:00
: texture_ptr_{texture_ptr},
2026-04-28 19:45:41 +02:00
tile_definitions_{std::move(tile_definitions)}
{}
static TileSet create_from_texture(
2026-06-16 21:53:19 +02:00
const ResourcePtr<Texture>& texture_ptr,
2026-04-28 19:45:41 +02:00
const int tile_size,
const std::vector<sdl::Point>& tile_atlas_coords
)
{
assert(tile_size > 0);
// TODO: These probably shouldn't be asserts...
2026-06-16 21:53:19 +02:00
assert(texture_ptr->get_width() % tile_size == 0);
assert(texture_ptr->get_height() % tile_size == 0);
2026-04-28 19:45:41 +02:00
std::vector<TileDefinition> tile_definitions;
// TODO: Should we automatically create tile 0 (no tile) instead of listing it in tile_atlas_coords?
for (const auto& [x, y] : tile_atlas_coords) {
const TileDefinition tile{
{
static_cast<float>(x * tile_size),
static_cast<float>(y * tile_size),
static_cast<float>(tile_size),
static_cast<float>(tile_size),
}
};
// TODO: These probably shouldn't be asserts...
2026-06-16 21:53:19 +02:00
assert(tile.atlas_src_rect.x + tile.atlas_src_rect.w <= texture_ptr->get_width());
assert(tile.atlas_src_rect.y + tile.atlas_src_rect.h <= texture_ptr->get_height());
2026-04-28 19:45:41 +02:00
tile_definitions.push_back(tile);
}
2026-06-16 21:53:19 +02:00
return TileSet(texture_ptr, tile_definitions);
2026-04-28 19:45:41 +02:00
}
2026-06-16 21:53:19 +02:00
constexpr const Texture& get_texture() const
2026-04-28 19:45:41 +02:00
{
2026-06-16 21:53:19 +02:00
return *texture_ptr_;
2026-04-28 19:45:41 +02:00
}
constexpr const TileDefinition& get_tile_definition(const TileID tile_id) const
{
return tile_definitions_.at(tile_id);
}
};
}