#include "impl.hpp" #include "ground-info.hpp" #include "src/ground-atlas.hpp" #include "compat/exception.hpp" #include "serialize/json-helper.hpp" #include "serialize/corrade-string.hpp" #include #include #include #include namespace floormat { using nlohmann::json; using loader_detail::loader_impl; [[maybe_unused]] static void from_json(const json& j, ground_info& val) { } [[maybe_unused]] static void to_json(json& j, const ground_info& val) { } } // namespace floormat namespace floormat::loader_detail { std::shared_ptr loader_impl::get_ground_atlas(StringView name, StringView dir, Vector2ub size, pass_mode pass) { fm_assert(name != ""_s); char buf[FILENAME_MAX]; auto filename = make_atlas_path(buf, dir, name); auto tex = texture(""_s, filename); auto atlas = std::make_shared(filename, name, tex, size, pass); return atlas; } // todo copypasta from wall-atlas.cpp std::shared_ptr loader_impl::ground_atlas(StringView name, bool fail_ok) noexcept(false) { fm_soft_assert(check_atlas_name(name)); auto it = ground_atlas_map.find(name); if (it != ground_atlas_map.end()) [[likely]] { if (it->second == (ground_info*)-1) [[unlikely]] { if (fail_ok) [[likely]] goto missing_ok; else 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); else return it->second->atlas; } else { if (fail_ok) [[likely]] goto missing; else goto error; } missing: { missing_ground_atlases.push_back(String { AllocatedInit, name }); auto string_view = StringView{missing_ground_atlases.back()}; ground_atlas_map[string_view] = (ground_info*)-1; } if (name != "") DBG_nospace << "ground_atlas '" << name << "' doesn't exist"; missing_ok: return make_invalid_ground_atlas().atlas; error: fm_throw("no such ground atlas '{}'"_cf, name); } ArrayView loader_impl::ground_atlas_list() noexcept(false) { if (ground_atlas_map.empty()) [[unlikely]] get_ground_atlas_list(); fm_assert(!ground_atlas_map.empty()); return ground_atlas_array; } void loader_impl::get_ground_atlas_list() { fm_assert(ground_atlas_map.empty()); ground_atlas_array = json_helper::from_json>( Path::join(loader_::GROUND_TILESET_PATH, "ground.json"_s)); ground_atlas_array.shrink_to_fit(); ground_atlas_map.clear(); ground_atlas_map.reserve(ground_atlas_array.size()*2); for (auto& x : ground_atlas_array) { } fm_assert(!ground_atlas_map.empty()); } } // namespace floormat::loader_detail