diff options
-rw-r--r-- | main/app.hpp | 4 | ||||
-rw-r--r-- | main/camera.cpp | 5 | ||||
-rw-r--r-- | main/draw.cpp | 3 | ||||
-rw-r--r-- | src/local-coords.hpp | 10 | ||||
-rw-r--r-- | src/tile-defs.hpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 6 | ||||
-rw-r--r-- | src/world.hpp | 31 | ||||
-rw-r--r-- | test/json.cpp | 13 |
8 files changed, 55 insertions, 19 deletions
diff --git a/main/app.hpp b/main/app.hpp index e5c0e34b..403b8888 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -75,8 +75,8 @@ struct app final : Platform::Application const void* _dummy = register_debug_callback(); tile_shader _shader; - tile_atlas_ floor1 = loader.tile_atlas("metal1.tga", {2, 2}); - tile_atlas_ floor2 = loader.tile_atlas("floor1.tga", {4, 4}); + tile_atlas_ floor1 = loader.tile_atlas("floor-tiles.tga", {44, 4}); + tile_atlas_ floor2 = loader.tile_atlas("metal1.tga", {2, 2}); tile_atlas_ wall1 = loader.tile_atlas("wood2.tga", {2, 2}); tile_atlas_ wall2 = loader.tile_atlas("wood1.tga", {2, 2}); chunk _chunk = make_test_chunk(); diff --git a/main/camera.cpp b/main/camera.cpp index bfbf6aa5..b0b8f001 100644 --- a/main/camera.cpp +++ b/main/camera.cpp @@ -15,6 +15,11 @@ void app::do_camera(float dt) else if (keys[key::camera_right]) camera_offset += Vector2(-1, 0) * dt * pixels_per_second; + { + const auto max_camera_offset = Vector2(windowSize() * 10); + camera_offset[0] = std::clamp(camera_offset[0], -max_camera_offset[0], max_camera_offset[0]); + camera_offset[1] = std::clamp(camera_offset[1], -max_camera_offset[1], max_camera_offset[1]); + } _shader.set_camera_offset(camera_offset); if (keys[key::camera_reset]) diff --git a/main/draw.cpp b/main/draw.cpp index b032e2a2..91277c34 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -52,8 +52,7 @@ void app::draw_chunk(chunk& c) fn(0, y); fn(x, y); - printf("%d %d -> %d %d\n", minx, miny, maxx, maxy); - fflush(stdout); + // TODO printf(""); // put breakpoint here } 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 <cstdint> +#include <concepts> #include <Magnum/Magnum.h> #include <Magnum/Math/Vector3.h> @@ -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<std::integral T> 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<std::integral T> +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<std::size_t>(x) < TILE_MAX_DIM && static_cast<std::size_t>(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<decltype(chunk_coords::x), decltype(chunk_coords::y static_assert(std::is_same_v<decltype(chunk_coords::x), decltype(chunk_coords::y)>); +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 diff --git a/test/json.cpp b/test/json.cpp index 0ecd9f54..67e23e01 100644 --- a/test/json.cpp +++ b/test/json.cpp @@ -15,18 +15,17 @@ static chunk make_test_chunk() { auto metal1 = loader.tile_atlas("share/game/images/metal1.tga", {2, 2}), metal2 = loader.tile_atlas("share/game/images/metal2.tga", {2, 2}), - metal3 = loader.tile_atlas("share/game/images/metal3.tga", {2, 2}); + tiles = loader.tile_atlas("share/game/images/tiles.tga", {8, 5}); constexpr auto N = TILE_MAX_DIM; chunk c; for (auto& [x, k, pt] : c) { - const auto& atlas = pt.x > N/2 && pt.y >= N/2 ? metal1 : metal2; - x.ground_image = { atlas, (std::uint8_t)(k % atlas->num_tiles().product()) }; + x.ground_image = { tiles, (std::uint8_t)(k % tiles->num_tiles().product()) }; } constexpr auto K = N/2; - c[{K, K }].wall_north = { metal3, 0 }; - c[{K, K }].wall_west = { metal3, 0 }; - c[{K, K+1}].wall_north = { metal3, 0 }; - c[{K+1, K }].wall_west = { metal3, 0 }; + c[{K, K }].wall_north = { metal1, 0 }; + c[{K, K }].wall_west = { metal2, 0 }; + c[{K, K+1}].wall_north = { metal1, 0 }; + c[{K+1, K }].wall_west = { metal2, 0 }; return c; } |