From 3acbe0d4063f5ba3060a19dcbdca91615015db88 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 21 Feb 2022 15:01:18 +0100 Subject: flush --- loader-impl.cpp | 2 ++ loader.hpp | 3 +-- tile.cpp | 4 ++-- tile.hpp | 44 +++++++++++++++++++++++++++++--------------- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/loader-impl.cpp b/loader-impl.cpp index 63b16a10..1f22dfe3 100644 --- a/loader-impl.cpp +++ b/loader-impl.cpp @@ -12,6 +12,8 @@ namespace Magnum::Examples { +using atlas_ptr = std::shared_ptr; + struct loader_impl final : loader_ { const Utility::Resource shader_res{"game/shaders"}; diff --git a/loader.hpp b/loader.hpp index 0406d4e2..9e177211 100644 --- a/loader.hpp +++ b/loader.hpp @@ -9,13 +9,12 @@ namespace Magnum::Examples { struct atlas_texture; -using atlas_ptr = std::shared_ptr; struct loader_ { virtual std::string shader(const std::string& filename) = 0; virtual Trade::ImageData2D tile_texture(const std::string& filename) = 0; - virtual atlas_ptr tile_atlas(const std::string& filename) = 0; + virtual std::shared_ptr tile_atlas(const std::string& filename) = 0; static void destroy(); loader_(const loader_&) = delete; diff --git a/tile.cpp b/tile.cpp index 3fe28959..85f98100 100644 --- a/tile.cpp +++ b/tile.cpp @@ -3,9 +3,9 @@ namespace Magnum::Examples { -chunk::tile_index_type chunk::make_tile_indices() noexcept +chunk::tile_index_array_type chunk::make_tile_indices() noexcept { - tile_index_type array; + tile_index_array_type array; for (unsigned i = 0; i < N*N; i++) array[i] = (UnsignedByte)i; return array; diff --git a/tile.hpp b/tile.hpp index f47f9077..ed3a0077 100644 --- a/tile.hpp +++ b/tile.hpp @@ -1,5 +1,4 @@ #pragma once -#include "loader.hpp" #include "atlas.hpp" #include "hash.hpp" #include "defs.hpp" @@ -13,10 +12,10 @@ namespace Magnum::Examples { -struct tile_image final : std::tuple +struct tile_image final { - constexpr int variant() const noexcept { return std::get<1>(*this); } - atlas_ptr atlas() const noexcept { return std::get<0>(*this); } + std::shared_ptr atlas; + int variant = -1; }; struct tile final @@ -27,27 +26,29 @@ struct tile final tile_image ground_image_; pass_mode passability_ = pass_obscured; - explicit operator bool() const noexcept { return !!std::get<0>(ground_image_); } + explicit operator bool() const noexcept { return !!ground_image_.atlas; } }; -struct local_coords final : std::pair { +struct local_coords final { + std::uint8_t x = 0, y = 0; constexpr std::size_t to_index() const noexcept; }; -struct chunk_coords final : std::pair { +struct chunk_coords final { + Short x = 0, y = 0; constexpr std::size_t to_index() const noexcept; }; -struct global_coords final : std::pair {}; - struct chunk final { static constexpr std::size_t N = 16; static constexpr std::size_t TILE_COUNT = N*N; - using tile_index_type = std::array; - static tile_index_type make_tile_indices() noexcept; - tile_index_type indices = make_tile_indices(); + 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) }; + + std::array indices = make_tile_indices(); constexpr struct tile& tile(local_coords xy); constexpr struct tile& tile(local_coords xy) const { return const_cast(*this).tile(xy); } @@ -58,11 +59,13 @@ struct chunk final private: template constexpr void foreach_tile_(F&& fun); + static std::array make_tile_indices() noexcept; + std::array tiles = {}; }; constexpr std::size_t local_coords::to_index() const noexcept { - return second*chunk::N + first; + return y*chunk::N + x; } constexpr struct tile& chunk::operator[](std::size_t i) { @@ -93,11 +96,11 @@ constexpr void chunk::foreach_tile_(F&& fun) constexpr std::size_t chunk_coords::to_index() const noexcept { - using unsigned_type = std::make_unsigned_t; + 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(std::size_t)/2); - return (std::size_t)(unsigned_type)second * N + (std::size_t)(unsigned_type)first; + return (std::size_t)(unsigned_type)y * N + (std::size_t)(unsigned_type)x; } struct hash_chunk final { @@ -109,9 +112,20 @@ struct hash_chunk final { struct world final { explicit world(); + 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) +{ + auto it = chunks.find(xy); + if (it != chunks.end()) + return it->second; + else + return chunks[xy] = fun(); +} + } //namespace Magnum::Examples -- cgit v1.2.3