diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-12 12:48:46 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-11-12 12:48:46 +0100 |
commit | 664cfa76b05756cb5ee22cf9bfe463822aeae6a3 (patch) | |
tree | c74e8ac82d599082d0aef749201a47aa0b68c6e7 | |
parent | d258f0a4dbc3ad3f0b47a50e8618099d041bd164 (diff) |
a
-rw-r--r-- | serialize/wall-atlas.cpp | 39 | ||||
-rw-r--r-- | serialize/wall-atlas.hpp | 1 | ||||
-rw-r--r-- | src/wall-atlas.cpp | 1 | ||||
-rw-r--r-- | src/wall-atlas.hpp | 15 | ||||
-rw-r--r-- | test/json/wall-atlas-02_groups.json | 11 | ||||
-rw-r--r-- | wall-tileset-tool/main.cpp | 7 |
6 files changed, 58 insertions, 16 deletions
diff --git a/serialize/wall-atlas.cpp b/serialize/wall-atlas.cpp index 81bb742a..5db79e05 100644 --- a/serialize/wall-atlas.cpp +++ b/serialize/wall-atlas.cpp @@ -14,15 +14,9 @@ namespace floormat::Wall::detail { using namespace std::string_view_literals; -namespace { - -constexpr StringView direction_names[] = { "n"_s, "e"_s, "s"_s, "w"_s, }; - -} // namespace - uint8_t direction_index_from_name(StringView s) { - for (uint8_t i = 0; auto n : direction_names) + for (uint8_t i = 0; auto [n, _] : wall_atlas::directions) if (n == s) return i; else @@ -33,8 +27,8 @@ uint8_t direction_index_from_name(StringView s) StringView direction_index_to_name(size_t i) { - fm_soft_assert(i < arraySize(direction_names)); - return direction_names[i]; + fm_soft_assert(i < arraySize(wall_atlas::directions)); + return wall_atlas::directions[i].name; } Group read_group_metadata(const json& jgroup) @@ -46,10 +40,12 @@ Group read_group_metadata(const json& jgroup) { int count = 0, index = -1; bool has_count = jgroup.contains("count"sv) && (count = jgroup["count"sv]) != 0, - has_index = jgroup.contains("index"sv); + has_index = jgroup.contains("offset"sv); + if (has_index) + index = jgroup["offset"sv]; fm_soft_assert(has_count == has_index); fm_soft_assert(!has_index || index >= 0 && index < 1 << 20); - // todo check index within range; + // todo check index within range } if (jgroup.contains("pixel-size"sv)) @@ -85,6 +81,8 @@ Direction read_direction_metadata(const json& jroot, Direction_ dir) val.*memfn = read_group_metadata(jdir[s]); } + val.top.pixel_size = val.top.pixel_size.flipped(); + return val; } @@ -104,7 +102,7 @@ void write_group_metadata(json& jgroup, const Group& val) fm_assert(jgroup.is_object()); fm_assert(jgroup.empty()); - jgroup["index"sv] = val.index; + jgroup["offset"sv] = val.index; jgroup["count"sv] = val.count; jgroup["pixel-size"sv] = val.pixel_size; jgroup["tint-mult"sv] = Vector4(val.tint_mult); @@ -132,6 +130,19 @@ void write_direction_metadata(json& jdir, const Direction& dir) } } +void write_all_directions(json& jroot, const wall_atlas& a) +{ + for (auto [name, i] : wall_atlas::directions) + { + if (const auto* dir = a.direction((size_t)i)) + { + auto jdir = json{json::value_t::object}; + write_direction_metadata(jdir, *dir); + jroot[name] = jdir; + } + } +} + void write_info_header(json& jroot, const Info& info) { jroot["name"sv] = info.name; @@ -149,7 +160,9 @@ using namespace floormat::Wall::detail; void adl_serializer<std::shared_ptr<wall_atlas>>::to_json(json& j, const std::shared_ptr<const wall_atlas>& x) { - + fm_assert(x != nullptr); + write_info_header(j, x->info()); + write_all_directions(j, *x); } void adl_serializer<std::shared_ptr<wall_atlas>>::from_json(const json& j, std::shared_ptr<wall_atlas>& x) diff --git a/serialize/wall-atlas.hpp b/serialize/wall-atlas.hpp index 17ed5a33..edb8b29b 100644 --- a/serialize/wall-atlas.hpp +++ b/serialize/wall-atlas.hpp @@ -27,6 +27,7 @@ Info read_info_header(const json& jroot); void write_group_metadata(json& jgroup, const Group& val); void write_direction_metadata(json& jdir, const Direction& dir); +void write_all_directions(json& jroot, const wall_atlas& a); void write_info_header(json& jroot, const Info& info); } // namespace floormat::Wall::detail diff --git a/src/wall-atlas.cpp b/src/wall-atlas.cpp index 94134523..96758983 100644 --- a/src/wall-atlas.cpp +++ b/src/wall-atlas.cpp @@ -90,6 +90,7 @@ auto wall_atlas::direction(size_t dir) const -> const Direction* uint8_t wall_atlas::direction_count() const { return (uint8_t)_dir_array.size(); } auto wall_atlas::raw_frame_array() const -> ArrayView<const Frame> { return _frame_array; } +auto wall_atlas::info() const -> const Info& { return _info; } StringView wall_atlas::name() const { return _info.name; } size_t wall_atlas::enum_to_index(enum rotation r) diff --git a/src/wall-atlas.hpp b/src/wall-atlas.hpp index 8df0c1ec..3d97c0e7 100644 --- a/src/wall-atlas.hpp +++ b/src/wall-atlas.hpp @@ -117,9 +117,22 @@ public: const Direction* direction(size_t dir) const; ArrayView<const Frame> frames(const Group& a) const; ArrayView<const Frame> raw_frame_array() const; + const Info& info() const; static size_t enum_to_index(enum rotation x); -}; + struct dir_tuple + { + StringView name; + Direction_ direction; + }; + + static constexpr dir_tuple directions[] = { + { "n"_s, Direction_::N }, + { "e"_s, Direction_::E }, + { "s"_s, Direction_::S }, + { "w"_s, Direction_::W }, + }; +}; } // namespace floormat diff --git a/test/json/wall-atlas-02_groups.json b/test/json/wall-atlas-02_groups.json index 3c021733..b429ad02 100644 --- a/test/json/wall-atlas-02_groups.json +++ b/test/json/wall-atlas-02_groups.json @@ -1,12 +1,19 @@ { "name": "foo", "depth": 42, - "frames": [], + "frames": [ + { + "offset": "0 x 0", + "size": "64 x 192" + } + ], "n": { }, "w": { "wall": { - "default-tint": false + "default-tint": false, + "offset": 0, + "count": 1 }, "side": { "pixel-size": "42 x 192", diff --git a/wall-tileset-tool/main.cpp b/wall-tileset-tool/main.cpp index eab78c16..2e78742d 100644 --- a/wall-tileset-tool/main.cpp +++ b/wall-tileset-tool/main.cpp @@ -37,6 +37,13 @@ Triple<options, Arguments, bool> parse_cmdline(int argc, const char* const* argv options opts; //Path::exists(args.value<StringView>()); + opts.output_dir = Path::join(loader.startup_directory(), args.value<StringView>("output")); + opts.input_file = Path::join(loader.startup_directory(), args.value<StringView>("input.json")); + opts.input_dir = Path::split(opts.input_file).first(); + + if (opts.output_dir.isEmpty()) + opts.output_dir = opts.input_dir; + return { std::move(opts), std::move(args), false }; } |