summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--loader/anim-traits.cpp5
-rw-r--r--loader/anim-traits.hpp4
-rw-r--r--loader/atlas-loader-storage.hpp3
-rw-r--r--loader/atlas-loader.hpp5
-rw-r--r--loader/atlas-loader.inl23
-rw-r--r--loader/ground-traits.cpp5
-rw-r--r--loader/ground-traits.hpp3
-rw-r--r--loader/scenery-atlas.cpp16
-rw-r--r--loader/scenery-cell.cpp16
-rw-r--r--loader/scenery-cell.hpp8
-rw-r--r--loader/scenery-traits.cpp54
-rw-r--r--loader/scenery-traits.hpp26
-rw-r--r--loader/scenery.cpp14
-rw-r--r--loader/wall-traits.cpp6
-rw-r--r--loader/wall-traits.hpp3
15 files changed, 149 insertions, 42 deletions
diff --git a/loader/anim-traits.cpp b/loader/anim-traits.cpp
index f5a9e03a..fca5be4d 100644
--- a/loader/anim-traits.cpp
+++ b/loader/anim-traits.cpp
@@ -23,7 +23,6 @@ StringView anim_traits::loader_name() { return "anim_atlas"_s; }
auto anim_traits::atlas_of(const Cell& x) -> const std::shared_ptr<Atlas>& { return x.atlas; }
auto anim_traits::atlas_of(Cell& x) -> std::shared_ptr<Atlas>& { return x.atlas; }
StringView anim_traits::name_of(const Cell& x) { return x.name; }
-StringView anim_traits::name_of(const Atlas& x) { return x.name(); }
String& anim_traits::name_of(Cell& x) { return x.name; }
void anim_traits::atlas_list(Storage& s)
@@ -33,7 +32,7 @@ void anim_traits::atlas_list(Storage& s)
s.name_map[loader.INVALID] = -1uz;
}
-auto anim_traits::make_invalid_atlas(Storage& s) -> Pointer<Cell>
+auto anim_traits::make_invalid_atlas(Storage& s) -> Cell
{
fm_debug_assert(!s.invalid_atlas);
@@ -63,7 +62,7 @@ auto anim_traits::make_invalid_atlas(Storage& s) -> Pointer<Cell>
.atlas = atlas,
.name = loader.INVALID,
};
- return Pointer<anim_cell>{ InPlace, Utility::move(info) };
+ return info;
}
auto anim_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr<Atlas>
diff --git a/loader/anim-traits.hpp b/loader/anim-traits.hpp
index 78bf6b45..517a3097 100644
--- a/loader/anim-traits.hpp
+++ b/loader/anim-traits.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "atlas-loader-fwd.hpp"
#include <memory>
+#include <cr/Optional.h>
namespace floormat { struct anim_cell; class anim_atlas; }
@@ -17,10 +18,9 @@ template<> struct atlas_loader_traits<anim_atlas>
static const std::shared_ptr<Atlas>& atlas_of(const Cell& x);
static std::shared_ptr<Atlas>& atlas_of(Cell& x);
static StringView name_of(const Cell& x);
- static StringView name_of(const Atlas& x);
static String& name_of(Cell& x);
static void atlas_list(Storage& st);
- static Pointer<Cell> make_invalid_atlas(Storage& st);
+ static Cell make_invalid_atlas(Storage& st);
static std::shared_ptr<Atlas> make_atlas(StringView name, const Cell& c);
static Optional<Cell> make_cell(StringView name);
};
diff --git a/loader/atlas-loader-storage.hpp b/loader/atlas-loader-storage.hpp
index aea94e48..f18b7756 100644
--- a/loader/atlas-loader-storage.hpp
+++ b/loader/atlas-loader-storage.hpp
@@ -3,6 +3,7 @@
#include "atlas-loader-fwd.hpp"
#include <vector>
#include <cr/StringView.h>
+#include <cr/Optional.h>
#include <tsl/robin_map.h>
namespace floormat::loader_detail {
@@ -19,7 +20,7 @@ struct atlas_storage
tsl::robin_map<StringView, size_t, hash_string_view> name_map;
std::vector<Cell> cell_array;
std::vector<String> missing_atlas_names;
- Pointer<Cell> invalid_atlas;
+ Optional<Cell> invalid_atlas;
~atlas_storage() noexcept = default;
};
diff --git a/loader/atlas-loader.hpp b/loader/atlas-loader.hpp
index e4afb5a0..226918f0 100644
--- a/loader/atlas-loader.hpp
+++ b/loader/atlas-loader.hpp
@@ -16,6 +16,7 @@ public:
using Traits = TRAITS;
using Atlas = ATLAS;
using Cell = typename TRAITS::Cell;
+ using AtlasPtr = std::decay_t<decltype(t.atlas_of(std::declval<const Cell&>()))>;
~atlas_loader() noexcept = default;
fm_DECLARE_DELETED_COPY_ASSIGNMENT(atlas_loader);
@@ -25,8 +26,8 @@ public:
atlas_loader() requires std::is_default_constructible_v<TRAITS>;
ArrayView<const Cell> atlas_list();
- const std::shared_ptr<Atlas>& get_atlas(StringView name, loader_policy p);
- std::shared_ptr<Atlas> make_atlas(StringView name, const Cell& cell);
+ const AtlasPtr& get_atlas(StringView name, loader_policy p);
+ AtlasPtr make_atlas(StringView name, const Cell& cell);
bool cell_exists(StringView name);
const Cell& get_cell(StringView name);
diff --git a/loader/atlas-loader.inl b/loader/atlas-loader.inl
index 3a07f2ab..c280ac33 100644
--- a/loader/atlas-loader.inl
+++ b/loader/atlas-loader.inl
@@ -62,10 +62,10 @@ auto atlas_loader<ATLAS, TRAITS>::atlas_list() -> ArrayView<const Cell>
}
template<typename ATLAS, typename TRAITS>
-const std::shared_ptr<ATLAS>& atlas_loader<ATLAS, TRAITS>::get_atlas(StringView name, loader_policy p)
+auto atlas_loader<ATLAS, TRAITS>::get_atlas(StringView name, const loader_policy p) -> const AtlasPtr&
{
atlas_list();
- const std::shared_ptr<Atlas>& invalid_atlas = t.atlas_of(get_invalid_atlas());
+ const AtlasPtr& invalid_atlas = t.atlas_of(get_invalid_atlas());
switch (p)
{
@@ -77,11 +77,14 @@ const std::shared_ptr<ATLAS>& atlas_loader<ATLAS, TRAITS>::get_atlas(StringView
default:
fm_abort("invalid loader_policy (%d)", (int)p);
}
+ CORRADE_ASSUME(p <= loader_policy::ignore);
if (name == loader.INVALID) [[unlikely]]
switch (p)
{
using enum loader_policy;
+ default:
+ std::unreachable();
case error:
goto error;
case ignore:
@@ -98,6 +101,8 @@ const std::shared_ptr<ATLAS>& atlas_loader<ATLAS, TRAITS>::get_atlas(StringView
switch (p)
{
using enum loader_policy;
+ default:
+ std::unreachable();
case error:
goto error;
case warn:
@@ -109,13 +114,13 @@ const std::shared_ptr<ATLAS>& atlas_loader<ATLAS, TRAITS>::get_atlas(StringView
{
Cell& c = s.cell_array[it->second];
fm_assert(t.name_of(c));
- std::shared_ptr<ATLAS>& atlas{t.atlas_of(c)};
+ AtlasPtr& atlas{t.atlas_of(c)};
if (!atlas) [[unlikely]]
{
atlas = make_atlas(name, c);
fm_assert(atlas);
fm_assert(t.atlas_of(c) == atlas);
- fm_assert(t.name_of(*atlas) == name);
+ //fm_assert(t.name_of(*atlas) == name);
return atlas;
}
else
@@ -141,10 +146,11 @@ const std::shared_ptr<ATLAS>& atlas_loader<ATLAS, TRAITS>::get_atlas(StringView
return t.atlas_of(c);
}
else
- {
switch (p)
{
using enum loader_policy;
+ default:
+ std::unreachable();
case error:
goto error;
case warn:
@@ -152,7 +158,6 @@ const std::shared_ptr<ATLAS>& atlas_loader<ATLAS, TRAITS>::get_atlas(StringView
case ignore:
return invalid_atlas;
}
- }
std::unreachable();
fm_assert(false);
@@ -171,7 +176,7 @@ missing_warn:
}
template<typename ATLAS, typename TRAITS>
-auto atlas_loader<ATLAS, TRAITS>::make_atlas(StringView name, const Cell& c) -> std::shared_ptr<Atlas>
+auto atlas_loader<ATLAS, TRAITS>::make_atlas(StringView name, const Cell& c) -> AtlasPtr
{
fm_assert(name != "<invalid>"_s);
fm_soft_assert(!t.name_of(c) || t.name_of(c) == name);
@@ -187,7 +192,7 @@ auto atlas_loader<ATLAS, TRAITS>::get_invalid_atlas() -> const Cell&
s.invalid_atlas = t.make_invalid_atlas(s);
fm_assert(s.invalid_atlas);
fm_assert(t.atlas_of(*s.invalid_atlas));
- fm_assert(t.name_of(*s.invalid_atlas) == loader.INVALID);
+ //fm_assert(t.name_of(*s.invalid_atlas) == loader.INVALID);
return *s.invalid_atlas;
}
@@ -211,10 +216,8 @@ void atlas_loader<ATLAS, TRAITS>::register_cell(Cell&& c)
String& name{t.name_of(c)};
if (name.isSmall())
name = String{AllocatedInit, name};
- const std::shared_ptr<Atlas>& atlas{t.atlas_of(c)};
fm_assert(!s.name_map.contains(name));
fm_soft_assert(loader.check_atlas_name(name));
- fm_assert(!atlas || t.name_of(*atlas) == name);
const size_t index{s.cell_array.size()};
s.cell_array.push_back(Utility::move(c));
s.name_map[ t.name_of(s.cell_array.back()) ] = index;
diff --git a/loader/ground-traits.cpp b/loader/ground-traits.cpp
index 4ad1d1c4..869cf214 100644
--- a/loader/ground-traits.cpp
+++ b/loader/ground-traits.cpp
@@ -19,7 +19,6 @@ StringView ground_traits::loader_name() { return "ground_atlas"_s; }
auto ground_traits::atlas_of(const Cell& x) -> const std::shared_ptr<Atlas>& { return x.atlas; }
auto ground_traits::atlas_of(Cell& x) -> std::shared_ptr<Atlas>& { return x.atlas; }
StringView ground_traits::name_of(const Cell& x) { return x.name; }
-StringView ground_traits::name_of(const Atlas& x) { return x.name(); }
String& ground_traits::name_of(Cell& x) { return x.name; }
void ground_traits::atlas_list(Storage& s)
@@ -29,13 +28,13 @@ void ground_traits::atlas_list(Storage& s)
s.name_map[loader.INVALID] = -1uz;
}
-auto ground_traits::make_invalid_atlas(Storage& s) -> Pointer<Cell>
+auto ground_traits::make_invalid_atlas(Storage& s) -> Cell
{
fm_debug_assert(!s.invalid_atlas);
auto atlas = std::make_shared<Atlas>(
ground_def{loader.INVALID, Vector2ub{1,1}, pass_mode::pass},
loader.make_error_texture(Vector2ui(tile_size_xy)));
- return Pointer<ground_cell>{ InPlaceInit, atlas, atlas->name(), atlas->num_tiles2(), atlas->pass_mode() };
+ return ground_cell{ atlas, atlas->name(), atlas->num_tiles2(), atlas->pass_mode() };
}
auto ground_traits::make_atlas(StringView name, const Cell& c) -> std::shared_ptr<Atlas>
diff --git a/loader/ground-traits.hpp b/loader/ground-traits.hpp
index bf66419c..f3fff4ee 100644
--- a/loader/ground-traits.hpp
+++ b/loader/ground-traits.hpp
@@ -17,10 +17,9 @@ template<> struct atlas_loader_traits<ground_atlas>
static const std::shared_ptr<Atlas>& atlas_of(const Cell& x);
static std::shared_ptr<Atlas>& atlas_of(Cell& x);
static StringView name_of(const Cell& x);
- static StringView name_of(const Atlas& x);
static String& name_of(Cell& x);
static void atlas_list(Storage& s);
- static Pointer<Cell> make_invalid_atlas(Storage& st);
+ static Cell make_invalid_atlas(Storage& st);
static std::shared_ptr<Atlas> make_atlas(StringView name, const Cell& c);
static Optional<Cell> make_cell(StringView name);
};
diff --git a/loader/scenery-atlas.cpp b/loader/scenery-atlas.cpp
index a93d9890..5471e69c 100644
--- a/loader/scenery-atlas.cpp
+++ b/loader/scenery-atlas.cpp
@@ -1 +1,17 @@
#include "impl.hpp"
+#include "impl.hpp"
+#include "atlas-loader.inl"
+#include "scenery-traits.hpp"
+#include "scenery-cell.hpp"
+#include "src/anim.hpp"
+#include "serialize/json-helper.hpp"
+#include "serialize/anim.hpp"
+
+namespace floormat {
+
+anim_def loader_::deserialize_anim_def(StringView filename) noexcept(false)
+{
+ return json_helper::from_json<anim_def>(filename);
+}
+
+} // namespace floormat
diff --git a/loader/scenery-cell.cpp b/loader/scenery-cell.cpp
index 03fefdcc..9a4a28d4 100644
--- a/loader/scenery-cell.cpp
+++ b/loader/scenery-cell.cpp
@@ -1 +1,17 @@
#include "scenery-cell.hpp"
+#include "compat/vector-wrapper.hpp"
+#include "src/anim-atlas.hpp"
+#include "loader/loader.hpp"
+#include "serialize/json-helper.hpp"
+#include "serialize/scenery.hpp"
+
+namespace floormat {
+
+vector_wrapper<const scenery_cell> scenery_cell::load_atlases_from_json()
+{
+ char buf[fm_FILENAME_MAX];
+ auto path = loader.make_atlas_path(buf, loader.SCENERY_PATH, "scenery.json"_s);
+ return { json_helper::from_json<std::vector<scenery_cell>>(path) };
+}
+
+} // namespace floormat
diff --git a/loader/scenery-cell.hpp b/loader/scenery-cell.hpp
index 8c954b5a..28f35368 100644
--- a/loader/scenery-cell.hpp
+++ b/loader/scenery-cell.hpp
@@ -1,13 +1,19 @@
#pragma once
+#include "compat/vector-wrapper-fwd.hpp"
#include "src/scenery.hpp"
-#include <Corrade/Containers/String.h>
+#include <memory>
+#include <cr/String.h>
namespace floormat {
+struct scenery_proto;
+
struct scenery_cell final
{
String name;
scenery_proto proto;
+
+ static vector_wrapper<const scenery_cell> load_atlases_from_json();
};
} // namespace floormat
diff --git a/loader/scenery-traits.cpp b/loader/scenery-traits.cpp
index 99664541..84cc42d9 100644
--- a/loader/scenery-traits.cpp
+++ b/loader/scenery-traits.cpp
@@ -1 +1,55 @@
#include "scenery-traits.hpp"
+#include "compat/assert.hpp"
+#include "compat/vector-wrapper.hpp"
+#include "atlas-loader-storage.hpp"
+#include "scenery-cell.hpp"
+#include "loader.hpp"
+#include "anim-cell.hpp"
+#include "src/tile-defs.hpp"
+#include "src/anim-atlas.hpp"
+#include "src/scenery.hpp"
+#include "serialize/json-helper.hpp"
+#include "serialize/anim.hpp"
+#include "serialize/scenery.hpp"
+#include <cr/Optional.h>
+#include <Corrade/Containers/StringView.h>
+
+namespace floormat::loader_detail {
+
+using scenery_traits = atlas_loader_traits<scenery_proto>;
+StringView scenery_traits::loader_name() { return "scenery_proto"_s; }
+auto scenery_traits::atlas_of(const Cell& x) -> const scenery_proto& { return x.proto; }
+auto scenery_traits::atlas_of(Cell& x) -> scenery_proto& { return x.proto; }
+StringView scenery_traits::name_of(const Cell& x) { return x.name; }
+String& scenery_traits::name_of(Cell& x) { return x.name; }
+
+void scenery_traits::atlas_list(Storage& s)
+{
+ fm_debug_assert(s.name_map.empty());
+ s.cell_array = scenery_cell::load_atlases_from_json().vec;
+}
+
+auto scenery_traits::make_invalid_atlas(Storage& s) -> Cell
+{
+ fm_debug_assert(!s.invalid_atlas);
+ auto proto = scenery_proto{};
+ proto.atlas = loader.invalid_anim_atlas().atlas;
+ proto.bbox_size = Vector2ub{tile_size_xy/2};
+ proto.subtype = generic_scenery_proto{false, true};
+ return { .name = loader.INVALID, .proto = proto, };
+}
+
+auto scenery_traits::make_atlas(StringView name, const Cell& c) -> scenery_proto
+{
+ auto p = c.proto;
+ p.atlas = loader.anim_atlas(name, loader.SCENERY_PATH);
+ fm_debug_assert(p.atlas);
+ return p;
+}
+
+auto scenery_traits::make_cell(StringView name) -> Optional<Cell>
+{
+ return { InPlace, Cell { .name = name, .proto = {}, } };
+}
+
+} // namespace floormat::loader_detail
diff --git a/loader/scenery-traits.hpp b/loader/scenery-traits.hpp
index 6f70f09b..13d52708 100644
--- a/loader/scenery-traits.hpp
+++ b/loader/scenery-traits.hpp
@@ -1 +1,27 @@
#pragma once
+#include "atlas-loader-fwd.hpp"
+#include <memory>
+
+namespace floormat { struct scenery_cell; struct scenery_proto; }
+
+namespace floormat::loader_detail {
+
+template<> struct atlas_loader_traits<scenery_proto>
+{
+ using Atlas = scenery_proto;
+ using Cell = scenery_cell;
+ using Self = atlas_loader_traits<Atlas>;
+ using Storage = atlas_storage<Atlas, Self>;
+
+ static StringView loader_name();
+ static const scenery_proto& atlas_of(const Cell& x);
+ static scenery_proto& atlas_of(Cell& x);
+ static StringView name_of(const Cell& x);
+ static String& name_of(Cell& x);
+ static void atlas_list(Storage& st);
+ static Cell make_invalid_atlas(Storage& st);
+ static scenery_proto make_atlas(StringView name, const Cell& c);
+ static Optional<Cell> make_cell(StringView name);
+};
+
+} // namespace floormat::loader_detail
diff --git a/loader/scenery.cpp b/loader/scenery.cpp
index 879e72d9..6fbaba45 100644
--- a/loader/scenery.cpp
+++ b/loader/scenery.cpp
@@ -2,23 +2,13 @@
#include "compat/assert.hpp"
#include "compat/exception.hpp"
#include "src/ground-atlas.hpp"
-#include "serialize/json-helper.hpp"
-#include "serialize/anim.hpp"
#include "serialize/scenery.hpp"
#include "scenery-cell.hpp"
+#include "serialize/json-helper.hpp"
#include "loader/anim-cell.hpp"
#include <Corrade/Containers/ArrayViewStl.h>
#include <Corrade/Utility/Path.h>
-namespace floormat {
-
-anim_def loader_::deserialize_anim_def(StringView filename) noexcept(false)
-{
- return json_helper::from_json<anim_def>(filename);
-}
-
-} // namespace floormat
-
namespace floormat::loader_detail {
void loader_impl::get_scenery_list()
@@ -38,7 +28,7 @@ void loader_impl::get_scenery_list()
sceneries_map.clear();
sceneries_map.reserve(sceneries_array.size() * 2);
- for (const scenery_cell& s : sceneries_array)
+ for (scenery_cell& s : sceneries_array)
{
if (sceneries_map.contains(s.name))
fm_abort("duplicate scenery name '%s'", s.name.data());
diff --git a/loader/wall-traits.cpp b/loader/wall-traits.cpp
index 83d6a836..0820629a 100644
--- a/loader/wall-traits.cpp
+++ b/loader/wall-traits.cpp
@@ -8,7 +8,6 @@
#include "compat/vector-wrapper.hpp"
#include <cr/StringView.h>
#include <cr/Optional.h>
-#include <cr/Pointer.h>
#include <mg/ImageData.h>
#include <mg/ImageView.h>
@@ -19,7 +18,6 @@ StringView wall_traits::loader_name() { return "wall_atlas"_s; }
auto wall_traits::atlas_of(const Cell& x) -> const std::shared_ptr<Atlas>& { return x.atlas; }
auto wall_traits::atlas_of(Cell& x) -> std::shared_ptr<Atlas>& { return x.atlas; }
StringView wall_traits::name_of(const Cell& x) { return x.name; }
-StringView wall_traits::name_of(const Atlas& x) { return x.name(); }
String& wall_traits::name_of(Cell& x) { return x.name; }
void wall_traits::atlas_list(Storage& s)
@@ -29,7 +27,7 @@ void wall_traits::atlas_list(Storage& s)
s.name_map[loader.INVALID] = -1uz;
}
-auto wall_traits::make_invalid_atlas(Storage& s) -> Pointer<Cell>
+auto wall_traits::make_invalid_atlas(Storage& s) -> Cell
{
fm_debug_assert(!s.invalid_atlas);
constexpr auto name = loader_::INVALID;
@@ -45,7 +43,7 @@ auto wall_traits::make_invalid_atlas(Storage& s) -> Pointer<Cell>
{{ {.val = 0}, {}, }},
{1u},
}, name, loader.make_error_texture(frame_size));
- return Pointer<wall_cell>{InPlaceInit, wall_cell{ .atlas = std::move(a), .name = name, } };
+ return { .atlas = std::move(a), .name = name, };
}
auto wall_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr<Atlas>
diff --git a/loader/wall-traits.hpp b/loader/wall-traits.hpp
index 2de0d96a..9233cb97 100644
--- a/loader/wall-traits.hpp
+++ b/loader/wall-traits.hpp
@@ -17,10 +17,9 @@ template<> struct atlas_loader_traits<wall_atlas>
static const std::shared_ptr<Atlas>& atlas_of(const Cell& x);
static std::shared_ptr<Atlas>& atlas_of(Cell& x);
static StringView name_of(const Cell& x);
- static StringView name_of(const Atlas& x);
static String& name_of(Cell& x);
static void atlas_list(Storage& st);
- static Pointer<Cell> make_invalid_atlas(Storage& st);
+ static Cell make_invalid_atlas(Storage& st);
static std::shared_ptr<Atlas> make_atlas(StringView name, const Cell& c);
static Optional<Cell> make_cell(StringView name);
};