From a65ff5d518b4ec8c28d99ce7bbe79c988529690a Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 25 Nov 2023 22:31:43 +0100 Subject: [PATCH] a --- serialize/wall-atlas.cpp | 65 ++++++++++++++++++++++------------------ serialize/wall-atlas.hpp | 2 -- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/serialize/wall-atlas.cpp b/serialize/wall-atlas.cpp index d2f4fa77..0a288b1f 100644 --- a/serialize/wall-atlas.cpp +++ b/serialize/wall-atlas.cpp @@ -8,8 +8,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -21,6 +21,39 @@ namespace floormat { using namespace floormat::Wall; using namespace floormat::Wall::detail; +namespace { + +struct direction_triple +{ + std::vector dirs; + std::array map; + std::bitset mask; +}; + +direction_triple read_all_directions(const json& jroot) +{ + std::bitset mask{0}; + size_t count = 0; + for (auto [str, _] : wall_atlas::directions) + if (jroot.contains(str)) + count++; + std::vector array{count}; + std::array map = {}; + for (uint8_t i = 0, pos = 0; i < std::size(wall_atlas::directions); i++) + { + auto [str, dir] = wall_atlas::directions[i]; + if (jroot.contains(str)) + { + mask[i] = true; + map[i] = {.val = pos}; + array[pos++] = read_direction_metadata(jroot, dir); + } + } + return { std::move(array), std::move(map), mask }; +} + +} // namespace + bool wall_atlas_def::operator==(const wall_atlas_def& other) const noexcept { if (header != other.header) @@ -56,12 +89,12 @@ wall_atlas_def wall_atlas_def::deserialize(StringView filename) atlas.header = read_info_header(jroot); fm_soft_assert(loader.check_atlas_name(atlas.header.name)); atlas.frames = read_all_frames(jroot); - auto [dirs, dir_indexes] = read_all_directions(jroot); + auto [dirs, dir_indexes, mask] = read_all_directions(jroot); fm_soft_assert(!dirs.empty()); fm_soft_assert(dir_indexes != std::array{}); atlas.direction_array = std::move(dirs); atlas.direction_map = dir_indexes; - atlas.direction_mask = get_existing_directions(jroot); + atlas.direction_mask = mask; return atlas; } @@ -203,32 +236,6 @@ Direction read_direction_metadata(const json& jroot, Direction_ dir) return val; } -[[nodiscard]] std::bitset get_existing_directions(const json& jroot) -{ - std::bitset array{0}; - for (uint8_t i = 0; auto [str, dir] : wall_atlas::directions) - if (jroot.contains(str)) - array[i] = true; - return array; -} - -Pair, std::array> read_all_directions(const json& jroot) -{ - size_t count = 0; - for (auto [str, _] : wall_atlas::directions) - if (jroot.contains(str)) - count++; - std::vector array{count}; - std::array map = {}; - for (uint8_t i = 0; auto [str, dir] : wall_atlas::directions) - if (jroot.contains(str)) - { - map[(size_t)dir] = {.val = i}; - array[i++] = read_direction_metadata(jroot, dir); - } - return { std::move(array), std::move(map) }; -} - Info read_info_header(const json& jroot) { fm_soft_assert(jroot.contains(("name"))); diff --git a/serialize/wall-atlas.hpp b/serialize/wall-atlas.hpp index 0d1f80b2..3342ca49 100644 --- a/serialize/wall-atlas.hpp +++ b/serialize/wall-atlas.hpp @@ -20,8 +20,6 @@ StringView direction_index_to_name(size_t i); [[nodiscard]] std::vector read_all_frames(const json& jroot); [[nodiscard]] Group read_group_metadata(const json& jgroup); [[nodiscard]] Direction read_direction_metadata(const json& jroot, Direction_ dir); -[[nodiscard]] std::bitset get_existing_directions(const json& jroot); -Pair, std::array> read_all_directions(const json& jroot); Info read_info_header(const json& jroot); void write_all_frames(json& jroot, ArrayView array);