From 69aaa0205ae096654015924c75fdf43fbe16f768 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 16 Oct 2022 23:15:43 +0200 Subject: a --- src/local-coords.hpp | 10 +++++++--- src/tile-defs.hpp | 2 +- src/world.cpp | 6 ++++++ src/world.hpp | 31 +++++++++++++++++++++++++++---- 4 files changed, 41 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/local-coords.hpp b/src/local-coords.hpp index 3bb2a33c..272c2877 100644 --- a/src/local-coords.hpp +++ b/src/local-coords.hpp @@ -2,6 +2,7 @@ #include "compat/assert.hpp" #include "tile-defs.hpp" #include +#include #include #include @@ -11,7 +12,8 @@ struct local_coords final { std::uint8_t x = 0, y = 0; explicit constexpr local_coords(std::size_t idx) noexcept; constexpr local_coords() noexcept = default; - constexpr local_coords(std::size_t x, std::size_t y) noexcept; + template requires (sizeof(T) <= sizeof(std::size_t)) + constexpr local_coords(T x, T y) noexcept; constexpr local_coords(std::uint8_t x, std::uint8_t y) noexcept : x{x}, y{y} {} constexpr std::size_t to_index() const noexcept { return y*TILE_MAX_DIM + x; } }; @@ -23,10 +25,12 @@ constexpr local_coords::local_coords(std::size_t index) noexcept : ASSERT(index < TILE_COUNT); } -constexpr local_coords::local_coords(std::size_t x, std::size_t y) noexcept +template +requires (sizeof(T) <= sizeof(std::size_t)) +constexpr local_coords::local_coords(T x, T y) noexcept : x{(std::uint8_t)x}, y{(std::uint8_t)y} { - ASSERT(x <= 0xff && y <= 0xff); + ASSERT(static_cast(x) < TILE_MAX_DIM && static_cast(y) < TILE_MAX_DIM); } } // namespace floormat diff --git a/src/tile-defs.hpp b/src/tile-defs.hpp index 41377cc0..ec59e19f 100644 --- a/src/tile-defs.hpp +++ b/src/tile-defs.hpp @@ -5,6 +5,6 @@ namespace floormat { constexpr inline std::size_t TILE_MAX_DIM = 16; constexpr inline std::size_t TILE_COUNT = TILE_MAX_DIM*TILE_MAX_DIM; -constexpr inline float TILE_SIZE[3] = { 64, 64, 128 }; +constexpr inline float TILE_SIZE[3] = { 128, 128, 384 }; } // namespace floormat diff --git a/src/world.cpp b/src/world.cpp index 3a04c4c7..63209bf7 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -9,5 +9,11 @@ static_assert(std::is_same_v); +static_assert(global_coords{{-1, -1}, {}} != global_coords{}); +static_assert(global_coords{15, 15}.chunk() == global_coords{}.chunk()); +static_assert(global_coords{15, 16}.chunk() != global_coords{}.chunk()); +static_assert(global_coords{(1 + (1<<15)) << 4 | 3, (2 + (1<<15)) << 4 | 4} == global_coords{{1, 2}, {3, 4}}); + + } // namespace floormat diff --git a/src/world.hpp b/src/world.hpp index b21502e0..8cbaa710 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -6,15 +6,38 @@ namespace floormat { struct chunk_coords final { std::int16_t x = 0, y = 0; + + constexpr bool operator==(const chunk_coords& other) const noexcept = default; }; struct global_coords final { - std::int16_t cx = 0, cy = 0; - std::int32_t x = 0, y = 0; + std::uint32_t x = 0, y = 0; - constexpr global_coords(chunk_coords c, local_coords xy) - : cx{c.x}, cy{c.y}, x{xy.x}, y{xy.y} + constexpr global_coords(chunk_coords c, local_coords xy) : + x{ std::uint32_t(c.x + (1 << 15)) << 4 | (xy.x & 0x0f) }, + y{ std::uint32_t(c.y + (1 << 15)) << 4 | (xy.y & 0x0f) } {} + constexpr global_coords(std::uint32_t x, std::uint32_t y) noexcept : x{x}, y{y} {} + constexpr global_coords() noexcept = default; + + constexpr local_coords local() const noexcept; + constexpr chunk_coords chunk() const noexcept; + + constexpr bool operator==(const global_coords& other) const noexcept = default; }; +constexpr local_coords global_coords::local() const noexcept +{ + return { (std::uint8_t)(x % TILE_MAX_DIM), (std::uint8_t)(y % TILE_MAX_DIM) }; +} + + +constexpr chunk_coords global_coords::chunk() const noexcept +{ + return { + (std::int16_t)(std::int32_t(x >> 4) - (1 << 15)), + (std::int16_t)(std::int32_t(y >> 4) - (1 << 15)), + }; +} + } // namespace floormat -- cgit v1.2.3