From 5ec89ae3a42308dd5e43e5fed207824918123023 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 2 Oct 2022 01:09:14 +0200 Subject: a --- chunk.hpp | 64 ++++----------------------------------------------------------- main.cpp | 4 ++-- 2 files changed, 6 insertions(+), 62 deletions(-) diff --git a/chunk.hpp b/chunk.hpp index 2685e473..e783a0d2 100644 --- a/chunk.hpp +++ b/chunk.hpp @@ -47,13 +47,12 @@ struct chunk final constexpr tile& operator[](std::size_t i); constexpr const tile& operator[](std::size_t i) const; - // TODO use local_coords template - requires std::invocable + requires std::invocable constexpr inline void foreach_tile(F&& fun) { foreach_tile_(std::forward(fun)); } template - requires std::invocable + requires std::invocable constexpr inline void foreach_tile(F&& fun) const { foreach_tile_(std::forward(fun)); } private: @@ -90,63 +89,8 @@ constexpr void chunk::foreach_tile_(F&& fun) std::size_t k = 0; for (std::size_t j = 0; j < N; j++) for (std::size_t i = 0; i < N; i++, k++) - fun(const_cast(*this).tiles[k], - local_coords{(std::uint8_t)i, (std::uint8_t)j}, - k); + fun(const_cast(*this).tiles[k], k, + local_coords{(std::uint8_t)i, (std::uint8_t)j}); } -#if 0 -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; -} -#endif - -#if 0 -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)} -{ -} - -#endif - } // namespace Magnum::Examples diff --git a/main.cpp b/main.cpp index 017cd120..81f6abf2 100644 --- a/main.cpp +++ b/main.cpp @@ -88,9 +88,9 @@ using namespace Math::Literals; chunk app::make_test_chunk() { chunk c; - c.foreach_tile([&, this](tile& x, local_coords, std::size_t k) { + c.foreach_tile([&, this](tile& x, std::size_t k, local_coords) { //const auto& atlas = (pt.y*TILE_MAX_DIM+pt.x+1) % 2 == 0 ? floor1 : floor2; - const auto& atlas = floor1; + const auto& atlas = floor2; x.ground_image = { atlas, (std::uint8_t)(k % atlas->size()) }; }); return c; -- cgit v1.2.3