From 9436414c5002e86e64f90be3f10933fdba63943e Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 1 Oct 2022 19:35:43 +0200 Subject: a --- chunk.hpp | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 chunk.hpp (limited to 'chunk.hpp') diff --git a/chunk.hpp b/chunk.hpp new file mode 100644 index 00000000..99ad1778 --- /dev/null +++ b/chunk.hpp @@ -0,0 +1,162 @@ +#pragma once +#include "tile.hpp" +#include +#include + +namespace Magnum::Examples { + +static constexpr std::size_t TILE_MAX_DIM = 16; +static constexpr std::size_t TILE_COUNT = TILE_MAX_DIM*TILE_MAX_DIM; + +struct local_coords final { + std::uint8_t x = 0, y = 0; + constexpr std::size_t to_index() const noexcept; +}; + +constexpr std::size_t local_coords::to_index() const noexcept { + return y*TILE_MAX_DIM + x; +} + +struct chunk_coords final { + std::int16_t x = 0, y = 0; + constexpr std::size_t to_index() const noexcept; + + static constexpr std::size_t max_bits = sizeof(chunk_coords::x)*8 * 3 / 4; + static_assert(max_bits*4/3/8 == sizeof(decltype(chunk_coords::x))); +}; + +struct chunk_sampler_array final { + using shared_sampler = std::shared_ptr; + using sampler_tuple = std::pair; + + static constexpr inline int MAX_SAMPLERS = 16; + std::vector> samplers; + std::array sampler_map = {}; + + chunk_sampler_array(); + void ensure_sampler(std::size_t tile_id, const shared_sampler& x); + std::shared_ptr operator[](std::size_t tile_id) const; + void clear(); + void bind(); + + static_assert(MAX_SAMPLERS <= 0xff); +}; + +struct global_coords final { + std::uint32_t x = 0, y = 0; + constexpr global_coords() noexcept = default; + constexpr global_coords(decltype(x) x, decltype(y) y) noexcept : x{x}, y{y} {} + constexpr global_coords(chunk_coords c, local_coords tile) noexcept; +}; + +static_assert(std::is_same_v); +static_assert(std::is_same_v); + +struct chunk final +{ + using index_type = decltype(local_coords::x); + using tile_index_array_type = std::array; + //static constexpr inline local_coords center = { (index_type)(N/2), (index_type)(N/2) }; + + constexpr tile& operator[](local_coords xy); + constexpr const tile& operator[](local_coords xy) const; + constexpr tile& operator[](std::size_t i); + constexpr const tile& operator[](std::size_t i) const; + + template + requires std::invocable + constexpr inline void foreach_tile(F&& fun) { foreach_tile_(std::forward(fun)); } + + template + requires std::invocable + constexpr inline void foreach_tile(F&& fun) const { foreach_tile_(std::forward(fun)); } + +private: + template constexpr void foreach_tile_(F&& fun); + + std::array tiles = {}; + chunk_sampler_array samplers; +}; + +constexpr tile& chunk::operator[](std::size_t i) { + if (i >= TILE_COUNT) + throw OUT_OF_RANGE(i, 0, TILE_COUNT); + return tiles[i]; +} + +constexpr const tile& chunk::operator[](std::size_t i) const { + return const_cast(*this).operator[](i); +} + +constexpr const tile& chunk::operator[](local_coords xy) const { + return const_cast(*this).operator[](xy); +} + +constexpr tile& chunk::operator[](local_coords xy) +{ + auto idx = xy.to_index(); + return operator[](idx); +} + +template +constexpr void chunk::foreach_tile_(F&& fun) +{ + constexpr auto N = TILE_MAX_DIM; + for (unsigned j = 0; j < N; j++) + for (unsigned i = 0; i < N; i++) + { + unsigned idx = j*N + i; + fun(const_cast(*this).tiles[idx], i, j); + } +} + +constexpr std::size_t chunk_coords::to_index() const noexcept +{ + using unsigned_type = std::make_unsigned_t; + using limits = std::numeric_limits; + constexpr auto N = limits::max() + std::size_t{1}; + static_assert(sizeof(unsigned_type) <= sizeof(UnsignedInt)/2); + return (std::size_t)(unsigned_type)y * N + (std::size_t)(unsigned_type)x; +} + +struct hash_chunk final { + constexpr std::size_t operator()(chunk_coords xy) const noexcept { + return hash{}(xy.to_index()); + } +}; + +struct world final +{ + static_assert(sizeof(chunk_coords::x) <= sizeof(std::size_t)/2); + + explicit world() = default; + template std::shared_ptr ensure_chunk(chunk_coords xy, F&& fun); + +private: + std::unordered_map, hash_chunk> chunks; +}; + +template +std::shared_ptr world::ensure_chunk(chunk_coords xy, F&& fun) +{ + ASSERT(xy.x < 1 << chunk_coords::max_bits); + ASSERT(xy.y < 1 << chunk_coords::max_bits); + + auto it = chunks.find(xy); + if (it != chunks.end()) + return it->second; + else + { + std::shared_ptr ptr{fun()}; + ASSERT(ptr); + return chunks[xy] = std::move(ptr); + } +} + +constexpr global_coords::global_coords(chunk_coords c, local_coords tile) noexcept : + x{tile.x + ((std::uint32_t)(std::make_unsigned_t)c.x << chunk_coords::max_bits)}, + y{tile.y + ((std::uint32_t)(std::make_unsigned_t)c.y << chunk_coords::max_bits)} +{ +} + +} // namespace Magnum::Examples -- cgit v1.2.3