From e1e539709d6e197cd2f2118e14598fd8c7b42c7b Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 7 Oct 2022 14:15:25 +0200 Subject: . --- src/chunk.hpp | 17 +++++++++++++---- src/tile.cpp | 6 +----- src/tile.hpp | 13 ++++++++++++- src/wall-mesh.hpp | 2 +- src/world.cpp | 5 +++++ src/world.hpp | 29 +++++++++++++++-------------- 6 files changed, 47 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/chunk.hpp b/src/chunk.hpp index e4ba217f..6c3706b8 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -5,6 +5,11 @@ namespace Magnum::Examples { +template +concept tile_iterator = requires(F fn, Tile& tile) { + { fn.operator()(tile, std::size_t{}, local_coords{}) } -> std::same_as; +}; + struct chunk final { constexpr tile& operator[](local_coords xy) { return _tiles[xy.to_index()]; } @@ -14,11 +19,15 @@ struct chunk final const auto& tiles() const { return _tiles; } auto& tiles() { return _tiles; } - template F> - constexpr inline void foreach_tile(F&& fun) { foreach_tile_(std::forward(fun)); } + template F> + constexpr inline void foreach_tile(F&& fun) { + foreach_tile_(std::forward(fun)); + } - template F> - constexpr inline void foreach_tile(F&& fun) const { foreach_tile_(std::forward(fun)); } + template F> + constexpr inline void foreach_const_tile(F&& fun) const { + const_cast(this)->foreach_tile_(std::forward(fun)); + } private: template diff --git a/src/tile.cpp b/src/tile.cpp index 41d11067..fe0e2da3 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -1,11 +1,7 @@ #include "tile.hpp" -#include "tile-atlas.hpp" namespace Magnum::Examples { -local_coords::local_coords(std::size_t x, std::size_t y) : x{(std::uint8_t)x}, y{(std::uint8_t)y} -{ - CORRADE_INTERNAL_ASSERT(x <= 0xff && y <= 0xff); -} + } // namespace Magnum::Examples diff --git a/src/tile.hpp b/src/tile.hpp index eadfa745..169c941b 100644 --- a/src/tile.hpp +++ b/src/tile.hpp @@ -1,4 +1,6 @@ #pragma once +#include "compat/defs.hpp" +#include "compat/assert.hpp" #include #include #include @@ -27,14 +29,23 @@ struct tile final tile_image ground_image, wall_north, wall_west; pass_mode passability = pass_shoot_through; + + constexpr tile() = default; + DECLARE_DEPRECATED_COPY_OPERATOR(tile); }; struct local_coords final { std::uint8_t x = 0, y = 0; constexpr local_coords() = default; - local_coords(std::size_t x, std::size_t y); + constexpr local_coords(std::size_t x, std::size_t y); constexpr local_coords(std::uint8_t x, std::uint8_t y) : x{x}, y{y} {} constexpr std::size_t to_index() const { return y*TILE_MAX_DIM + x; } }; +constexpr local_coords::local_coords(std::size_t x, std::size_t y) + : x{(std::uint8_t)x}, y{(std::uint8_t)y} +{ + ASSERT(x <= 0xff && y <= 0xff); +} + } //namespace Magnum::Examples diff --git a/src/wall-mesh.hpp b/src/wall-mesh.hpp index e9678e3e..e23799f4 100644 --- a/src/wall-mesh.hpp +++ b/src/wall-mesh.hpp @@ -19,7 +19,7 @@ struct wall_mesh final void draw(tile_shader& shader, chunk& c); private: - static constexpr auto COUNT1 = TILE_MAX_DIM*2, COUNT = COUNT1 * COUNT1; + static constexpr auto COUNT = TILE_MAX_DIM*2 * TILE_MAX_DIM*2; struct vertex final { std::array texcoords; diff --git a/src/world.cpp b/src/world.cpp index a8ca7085..2d19c7d2 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -2,7 +2,12 @@ namespace Magnum::Examples { +static_assert(sizeof(decltype(local_coords::x))*8 == 8); +static_assert(sizeof(decltype(chunk_coords::x))*8 == 16); +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); } // namespace Magnum::Examples diff --git a/src/world.hpp b/src/world.hpp index 21b2c764..99b984c2 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -6,23 +6,24 @@ namespace Magnum::Examples { struct chunk_coords final { std::int16_t x = 0, y = 0; -#if 0 - static_assert(std::is_same_v); - 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))); - constexpr chunk_coords(std::int16_t x, std::int16_t y); -#endif }; -#if 0 -constexpr chunk_coords::chunk_coords(std::int16_t x, std::int16_t y) : x{x}, y{y} { - using s = std::make_signed_t; -} -#endif - struct global_coords final { - chunk_coords chunk; - local_coords tile; + std::int32_t x = 0, y = 0; + constexpr chunk_coords chunk() const noexcept; + constexpr chunk_coords local() const noexcept; }; +constexpr chunk_coords global_coords::chunk() const noexcept { + constexpr std::uint32_t mask = 0xffff0000u; + const auto x_ = (std::int16_t)(std::uint16_t)((std::uint32_t)x & mask >> 24), + y_ = (std::int16_t)(std::uint16_t)((std::uint32_t)y & mask >> 24); + return {x_, y_}; +} + +constexpr chunk_coords global_coords::local() const noexcept { + const auto x_ = (std::uint8_t)x, y_ = (std::uint8_t)y; + return {x_, y_}; +} + } // namespace Magnum::Examples -- cgit v1.2.3