Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Nov 25, 2023
1 parent 042f183 commit a65ff5d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
65 changes: 36 additions & 29 deletions serialize/wall-atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <utility>
#include <string_view>
#include <Corrade/Containers/ArrayViewStl.h>
#include <Corrade/Containers/PairStl.h>
#include <Corrade/Containers/StringStl.h>
#include <Corrade/Containers/TripleStl.h>
#include <Magnum/ImageView.h>
#include <Magnum/Trade/ImageData.h>
#include <nlohmann/json.hpp>
Expand All @@ -21,6 +21,39 @@ namespace floormat {
using namespace floormat::Wall;
using namespace floormat::Wall::detail;

namespace {

struct direction_triple
{
std::vector<Direction> dirs;
std::array<DirArrayIndex, Direction_COUNT> map;
std::bitset<Direction_COUNT> mask;
};

direction_triple read_all_directions(const json& jroot)
{
std::bitset<Direction_COUNT> mask{0};
size_t count = 0;
for (auto [str, _] : wall_atlas::directions)
if (jroot.contains(str))
count++;
std::vector<Direction> array{count};
std::array<DirArrayIndex, Direction_COUNT> 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)
Expand Down Expand Up @@ -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<Wall::DirArrayIndex, Direction_COUNT>{});
atlas.direction_array = std::move(dirs);
atlas.direction_map = dir_indexes;
atlas.direction_mask = get_existing_directions(jroot);
atlas.direction_mask = mask;

return atlas;
}
Expand Down Expand Up @@ -203,32 +236,6 @@ Direction read_direction_metadata(const json& jroot, Direction_ dir)
return val;
}

[[nodiscard]] std::bitset<Direction_COUNT> get_existing_directions(const json& jroot)
{
std::bitset<Direction_COUNT> array{0};
for (uint8_t i = 0; auto [str, dir] : wall_atlas::directions)
if (jroot.contains(str))
array[i] = true;
return array;
}

Pair<std::vector<Direction>, std::array<DirArrayIndex, Direction_COUNT>> read_all_directions(const json& jroot)
{
size_t count = 0;
for (auto [str, _] : wall_atlas::directions)
if (jroot.contains(str))
count++;
std::vector<Direction> array{count};
std::array<DirArrayIndex, Direction_COUNT> 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")));
Expand Down
2 changes: 0 additions & 2 deletions serialize/wall-atlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ StringView direction_index_to_name(size_t i);
[[nodiscard]] std::vector<Frame> 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<Direction_COUNT> get_existing_directions(const json& jroot);
Pair<std::vector<Direction>, std::array<DirArrayIndex, Direction_COUNT>> read_all_directions(const json& jroot);
Info read_info_header(const json& jroot);

void write_all_frames(json& jroot, ArrayView<const Frame> array);
Expand Down

0 comments on commit a65ff5d

Please sign in to comment.