diff options
-rw-r--r-- | editor/ground-editor.cpp | 13 | ||||
-rw-r--r-- | editor/ground-editor.hpp | 3 | ||||
-rw-r--r-- | loader/ground-atlas.cpp | 14 | ||||
-rw-r--r-- | loader/impl.cpp | 2 | ||||
-rw-r--r-- | loader/impl.hpp | 2 | ||||
-rw-r--r-- | loader/loader.hpp | 2 | ||||
-rw-r--r-- | loader/texture.cpp | 2 | ||||
-rw-r--r-- | loader/vobj.cpp | 6 | ||||
-rw-r--r-- | serialize/ground-atlas.cpp | 21 | ||||
-rw-r--r-- | serialize/ground-atlas.hpp | 14 | ||||
-rw-r--r-- | src/ground-atlas.cpp | 8 | ||||
-rw-r--r-- | src/ground-atlas.hpp | 14 | ||||
-rw-r--r-- | test/bitmask.cpp | 2 | ||||
-rw-r--r-- | test/json.cpp | 2 |
14 files changed, 66 insertions, 39 deletions
diff --git a/editor/ground-editor.cpp b/editor/ground-editor.cpp index 2a74952c..f88bc25a 100644 --- a/editor/ground-editor.cpp +++ b/editor/ground-editor.cpp @@ -2,6 +2,7 @@ #include "src/ground-atlas.hpp" #include "src/world.hpp" #include "src/random.hpp" +#include "loader/ground-info.hpp" #include "keys.hpp" #include "loader/loader.hpp" #include "compat/exception.hpp" @@ -17,18 +18,16 @@ ground_editor::ground_editor() void ground_editor::load_atlases() { - for (const auto& atlas : loader.ground_atlases("ground.json"_s)) - { - auto& [_, vec] = _permutation; - vec.reserve(atlas->num_tiles()); - _atlases[atlas->name()] = atlas; - } + fm_assert(_atlases.empty()); + for (const auto& g : loader.ground_atlas_list()) + _atlases[g.name] = &g; + fm_assert(!_atlases.empty()); } std::shared_ptr<ground_atlas> ground_editor::maybe_atlas(StringView str) { if (auto it = _atlases.find(str); it != _atlases.end()) - return it->second; + return it->second->atlas; else return nullptr; } diff --git a/editor/ground-editor.hpp b/editor/ground-editor.hpp index 46c9dc3a..9ff9a198 100644 --- a/editor/ground-editor.hpp +++ b/editor/ground-editor.hpp @@ -11,6 +11,7 @@ namespace floormat { struct world; +struct ground_info; class ground_editor final { @@ -23,7 +24,7 @@ class ground_editor final std::vector<decltype(tile_image_proto::variant)> variant; }; - std::map<StringView, std::shared_ptr<ground_atlas>> _atlases; + std::map<StringView, const ground_info*> _atlases; tile_image_proto _selected_tile; tuple _permutation; selection_mode _selection_mode = sel_none; diff --git a/loader/ground-atlas.cpp b/loader/ground-atlas.cpp index c4e4a516..2835db2d 100644 --- a/loader/ground-atlas.cpp +++ b/loader/ground-atlas.cpp @@ -1,5 +1,4 @@ #include "impl.hpp" -#include "ground-info.hpp" #include "src/ground-atlas.hpp" #include "compat/exception.hpp" #include "serialize/json-helper.hpp" @@ -17,15 +16,16 @@ using loader_detail::loader_impl; namespace floormat::loader_detail { -std::shared_ptr<ground_atlas> loader_impl::get_ground_atlas(StringView name, StringView dir, Vector2ub size, pass_mode pass) +std::shared_ptr<ground_atlas> loader_impl::get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) { fm_assert(name != "<invalid>"_s); char buf[FILENAME_MAX]; - auto filename = make_atlas_path(buf, dir, name); + auto filename = make_atlas_path(buf, loader.GROUND_TILESET_PATH, name); auto tex = texture(""_s, filename); - auto atlas = std::make_shared<class ground_atlas>(filename, name, tex, size, pass); + auto info = ground_info{name, {}, size, pass}; + auto atlas = std::make_shared<class ground_atlas>(info, filename, tex); return atlas; } @@ -45,7 +45,7 @@ std::shared_ptr<class ground_atlas> loader_impl::ground_atlas(StringView name, b goto error; } else if (!it->second->atlas) - return it->second->atlas = get_ground_atlas(name, loader.GROUND_TILESET_PATH, it->second->size, it->second->pass); + return it->second->atlas = get_ground_atlas(name, it->second->size, it->second->pass); else return it->second->atlas; } @@ -110,8 +110,8 @@ const ground_info& loader_impl::make_invalid_ground_atlas() return *invalid_ground_atlas; auto atlas = std::make_shared<class ground_atlas>( - ""_s, loader.INVALID, make_error_texture(Vector2ui(iTILE_SIZE2)), - Vector2ub{1,1}, pass_mode::pass); + ground_info{loader.INVALID, {}, Vector2ub{1,1}, pass_mode::pass}, + ""_s, make_error_texture(Vector2ui(iTILE_SIZE2))); invalid_ground_atlas = Pointer<ground_info>{ InPlaceInit, atlas->name(), atlas, atlas->num_tiles2(), atlas->pass_mode()}; diff --git a/loader/impl.cpp b/loader/impl.cpp index 1ea7b7b6..0f15e74c 100644 --- a/loader/impl.cpp +++ b/loader/impl.cpp @@ -2,8 +2,8 @@ #include "compat/assert.hpp" #include "loader/scenery.hpp" #include "loader/vobj-info.hpp" -#include "loader/ground-info.hpp" #include "loader/wall-info.hpp" +#include "src/ground-atlas.hpp" #include <Corrade/Containers/Pair.h> #include <Magnum/Trade/ImageData.h> diff --git a/loader/impl.hpp b/loader/impl.hpp index 0b718527..5f1a8ff0 100644 --- a/loader/impl.hpp +++ b/loader/impl.hpp @@ -65,7 +65,7 @@ struct loader_impl final : loader_ ArrayView<const ground_info> ground_atlas_list() noexcept(false) override; void get_ground_atlas_list(); const ground_info& make_invalid_ground_atlas(); - std::shared_ptr<class ground_atlas> get_ground_atlas(StringView name, StringView path, Vector2ub size, pass_mode pass) noexcept(false) override; + std::shared_ptr<class ground_atlas> get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false) override; // >-----> anim >-----> tsl::robin_map<StringView, std::shared_ptr<class anim_atlas>> anim_atlas_map; diff --git a/loader/loader.hpp b/loader/loader.hpp index b62988ca..cc519765 100644 --- a/loader/loader.hpp +++ b/loader/loader.hpp @@ -25,7 +25,7 @@ struct loader_ { virtual StringView shader(StringView filename) noexcept = 0; virtual Trade::ImageData2D texture(StringView prefix, StringView filename) noexcept(false) = 0; - virtual std::shared_ptr<class ground_atlas> get_ground_atlas(StringView name, StringView path, Vector2ub size, pass_mode pass) noexcept(false) = 0; + virtual std::shared_ptr<class ground_atlas> get_ground_atlas(StringView name, Vector2ub size, pass_mode pass) noexcept(false) = 0; virtual std::shared_ptr<class ground_atlas> ground_atlas(StringView filename, bool fail_ok = false) noexcept(false) = 0; virtual ArrayView<const String> anim_atlas_list() = 0; virtual std::shared_ptr<class anim_atlas> anim_atlas(StringView name, StringView dir = ANIM_PATH) noexcept(false) = 0; diff --git a/loader/texture.cpp b/loader/texture.cpp index 59e8f25a..a68a6337 100644 --- a/loader/texture.cpp +++ b/loader/texture.cpp @@ -18,7 +18,7 @@ Trade::ImageData2D loader_impl::texture(StringView prefix, StringView filename_) constexpr size_t max_extension_length = 16; const auto N = prefix.size(); - if (N > 0) + if (N > 0) [[likely]] fm_assert(prefix[N-1] == '/'); fm_soft_assert(filename_.size() + prefix.size() + max_extension_length + 1 < FILENAME_MAX); fm_soft_assert(check_atlas_name(filename_)); diff --git a/loader/vobj.cpp b/loader/vobj.cpp index a309fd6a..66aef1cd 100644 --- a/loader/vobj.cpp +++ b/loader/vobj.cpp @@ -46,7 +46,7 @@ namespace floormat::loader_detail { std::shared_ptr<class anim_atlas> loader_impl::make_vobj_anim_atlas(StringView name, StringView image_filename) { - auto tex = texture(VOBJ_PATH, image_filename, false); + auto tex = texture(VOBJ_PATH, image_filename); anim_def def; def.object_name = name; const auto size = tex.pixels().size(); @@ -67,10 +67,12 @@ std::shared_ptr<class anim_atlas> loader_impl::make_vobj_anim_atlas(StringView n void loader_impl::get_vobj_list() { + fm_assert(vobjs.empty()); + vobjs.clear(); vobj_atlas_map.clear(); - auto vec = json_helper::from_json<std::vector<struct vobj>>(Path::join(VOBJ_PATH, "vobj.json")); + vec.shrink_to_fit(); vobjs.reserve(vec.size()); vobj_atlas_map.reserve(2*vec.size()); diff --git a/serialize/ground-atlas.cpp b/serialize/ground-atlas.cpp index 3522e85c..517b8186 100644 --- a/serialize/ground-atlas.cpp +++ b/serialize/ground-atlas.cpp @@ -2,7 +2,7 @@ #include "serialize/corrade-string.hpp" #include "serialize/magnum-vector.hpp" #include "serialize/pass-mode.hpp" -#include "loader/ground-info.hpp" +#include "loader/loader.hpp" #include <tuple> #include <nlohmann/json.hpp> @@ -14,15 +14,13 @@ using namespace floormat; namespace nlohmann { -#if 0 -void adl_serializer<ground_info>::to_json(json& j, const ground_info& x) +void adl_serializer<ground_def>::to_json(json& j, const ground_def& x) { using nlohmann::to_json; j = std::tuple<StringView, Vector2ub, pass_mode>{x.name, x.size, x.pass}; } -#endif -void adl_serializer<ground_info>::from_json(const json& j, ground_info& val) +void adl_serializer<ground_def>::from_json(const json& j, ground_def& val) { using nlohmann::from_json; val.name = j["name"]; @@ -31,4 +29,17 @@ void adl_serializer<ground_info>::from_json(const json& j, ground_info& val) val.pass = j["pass-mode"]; } +void adl_serializer<std::shared_ptr<ground_atlas>>::to_json(json& j, const std::shared_ptr<const ground_atlas>& x) +{ + j = std::tuple<StringView, Vector2ub, pass_mode>{x->name(), x->num_tiles2(), x->pass_mode()}; +} + +void adl_serializer<std::shared_ptr<ground_atlas>>::from_json(const json& j, std::shared_ptr<ground_atlas>& val) +{ + char buf[FILENAME_MAX]; + ground_def info = j; + auto path = loader.make_atlas_path(buf, loader.GROUND_TILESET_PATH, info.name); + val = std::make_shared<ground_atlas>(std::move(info), path, loader.texture(""_s, path)); +} + } // namespace nlohmann diff --git a/serialize/ground-atlas.hpp b/serialize/ground-atlas.hpp index 7ca57ede..fc8acec5 100644 --- a/serialize/ground-atlas.hpp +++ b/serialize/ground-atlas.hpp @@ -1,5 +1,5 @@ #pragma once -#include "loader/ground-info.hpp" +#include "src/ground-atlas.hpp" #include <nlohmann/json_fwd.hpp> namespace floormat { @@ -11,9 +11,15 @@ struct ground_info; namespace nlohmann { template<> -struct adl_serializer<floormat::ground_info> final { - static void to_json(json& j, const floormat::ground_info& x) = delete; - static void from_json(const json& j, floormat::ground_info& x); +struct adl_serializer<floormat::ground_def> final { + static void to_json(json& j, const floormat::ground_def& x); + static void from_json(const json& j, floormat::ground_def& x); +}; + +template<> +struct adl_serializer<std::shared_ptr<floormat::ground_atlas>> final { + static void to_json(json& j, const std::shared_ptr<const floormat::ground_atlas>& x); + static void from_json(const json& j, std::shared_ptr<floormat::ground_atlas>& x); }; } // namespace nlohmann diff --git a/src/ground-atlas.cpp b/src/ground-atlas.cpp index a64264ff..f918f940 100644 --- a/src/ground-atlas.cpp +++ b/src/ground-atlas.cpp @@ -12,14 +12,14 @@ namespace floormat { using namespace floormat::Quads; -ground_atlas::ground_atlas(StringView path, StringView name, const ImageView2D& image, Vector2ub tile_count, enum pass_mode p) : - texcoords_{make_texcoords_array(Vector2ui(image.size()), tile_count)}, - path_{path}, name_{name}, size_{image.size()}, dims_{tile_count}, passability{p} +ground_atlas::ground_atlas(ground_def info, String path, const ImageView2D& image) : + texcoords_{make_texcoords_array(Vector2ui(image.size()), info.size)}, + path_{std::move(path)}, name_{std::move(info.name)}, size_{image.size()}, dims_{info.size}, passability{info.pass} { constexpr auto variant_max = std::numeric_limits<variant_t>::max(); fm_soft_assert(num_tiles() <= variant_max); fm_soft_assert(dims_[0] > 0 && dims_[1] > 0); - fm_soft_assert(size_ % Vector2ui{tile_count} == Vector2ui()); + fm_soft_assert(size_ % Vector2ui{info.size} == Vector2ui()); tex_.setLabel(path_) .setWrapping(GL::SamplerWrapping::ClampToEdge) .setMagnificationFilter(GL::SamplerFilter::Nearest) diff --git a/src/ground-atlas.hpp b/src/ground-atlas.hpp index 94da682f..83b4705c 100644 --- a/src/ground-atlas.hpp +++ b/src/ground-atlas.hpp @@ -1,6 +1,7 @@ #pragma once #include "src/pass-mode.hpp" #include "src/quads.hpp" +#include "loader/ground-info.hpp" #include <array> #include <memory> #include <Corrade/Containers/Optional.h> @@ -11,6 +12,15 @@ namespace floormat { +struct ground_def +{ + String name; + Vector2ub size; + pass_mode pass; +}; + +class ground_atlas; + class ground_atlas final { using quad = Quads::quad; @@ -27,7 +37,7 @@ class ground_atlas final enum pass_mode passability; public: - ground_atlas(StringView path, StringView name, const ImageView2D& img, Vector2ub tile_count, enum pass_mode pass_mode); + ground_atlas(ground_def info, String path, const ImageView2D& img); texcoords texcoords_for_id(size_t id) const; [[maybe_unused]] Vector2ui pixel_size() const { return size_; } size_t num_tiles() const; @@ -39,6 +49,4 @@ public: static constexpr enum pass_mode default_pass_mode = pass_mode::pass; }; - - } // namespace floormat diff --git a/test/bitmask.cpp b/test/bitmask.cpp index 0fae6a23..ef8f3b1c 100644 --- a/test/bitmask.cpp +++ b/test/bitmask.cpp @@ -23,7 +23,7 @@ static_assert(data_nbytes == 128); void bitmask_test() { - auto img = loader.texture(loader.SCENERY_PATH, "door-close"_s, false); + auto img = loader.texture(loader.SCENERY_PATH, "door-close"_s); auto bitmask = anim_atlas::make_bitmask(img); fm_assert(img.pixelSize() == 4 && (size_t)img.size().product() >= data_nbytes); #ifdef DO_GENERATE diff --git a/test/json.cpp b/test/json.cpp index 3d7a5f0f..ad0cee37 100644 --- a/test/json.cpp +++ b/test/json.cpp @@ -41,7 +41,7 @@ void test_app::test_json() // NOLINT(readability-convert-member-functions-to-sta fm_assert(Path::exists(Path::join(loader.TEMP_PATH, "CMakeCache.txt"))); const auto output_dir = Path::join(loader.TEMP_PATH, "test/."_s); { - auto atlas = loader.ground_atlas("metal1", { 2, 2 }, pass_mode::pass); + auto atlas = loader.get_ground_atlas("metal1", { 2, 2 }, pass_mode::pass); json_helper::to_json(atlas, Path::join(output_dir, "atlas.json")); } { |