summaryrefslogtreecommitdiffhomepage
path: root/loader
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-05-17 17:06:16 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-05-17 17:08:52 +0200
commit8138cc9a269844b6c0a84c193a7a43aec7010592 (patch)
tree1d7c1f1b37c7f714fd0976bfa839e0b18ca2385d /loader
parent08b89c6575947e8fdcba9b6c329c25aa8472e52b (diff)
wip vobj
Diffstat (limited to 'loader')
-rw-r--r--loader/impl.cpp1
-rw-r--r--loader/impl.hpp7
-rw-r--r--loader/loader.cpp3
-rw-r--r--loader/loader.hpp4
-rw-r--r--loader/vobj-info.hpp15
-rw-r--r--loader/vobj.cpp102
6 files changed, 132 insertions, 0 deletions
diff --git a/loader/impl.cpp b/loader/impl.cpp
index 7e29616b..34da792a 100644
--- a/loader/impl.cpp
+++ b/loader/impl.cpp
@@ -1,6 +1,7 @@
#include "impl.hpp"
#include "compat/assert.hpp"
#include "loader/scenery.hpp"
+#include "loader/vobj-info.hpp"
#include <cstring>
#include <Corrade/Containers/Pair.h>
#include <Magnum/Trade/ImageData.h>
diff --git a/loader/impl.hpp b/loader/impl.hpp
index da3326b4..d0159a3d 100644
--- a/loader/impl.hpp
+++ b/loader/impl.hpp
@@ -23,7 +23,9 @@ struct loader_impl final : loader_
tsl::robin_map<StringView, std::shared_ptr<struct tile_atlas>> tile_atlas_map;
tsl::robin_map<StringView, std::shared_ptr<struct anim_atlas>> anim_atlas_map;
+ tsl::robin_map<StringView, const struct vobj_info*> vobj_atlas_map;
std::vector<String> anim_atlases;
+ std::vector<struct vobj_info> vobjs;
std::vector<serialized_scenery> sceneries_array;
tsl::robin_map<StringView, const serialized_scenery*> sceneries_map;
@@ -43,6 +45,11 @@ struct loader_impl final : loader_
void get_scenery_list();
static anim_def deserialize_anim(StringView filename);
+ void get_vobj_list();
+ std::shared_ptr<struct anim_atlas> make_vobj_anim_atlas(StringView name, StringView image_filename);
+ const struct vobj_info& vobj(StringView name) override;
+ ArrayView<struct vobj_info> vobj_list() override;
+
void set_application_working_directory();
StringView startup_directory() noexcept override;
static void system_init();
diff --git a/loader/loader.cpp b/loader/loader.cpp
index eccfb350..d6076d09 100644
--- a/loader/loader.cpp
+++ b/loader/loader.cpp
@@ -30,6 +30,8 @@ StringView loader_::strip_prefix(StringView name)
return name.exceptPrefix(ANIM_PATH.size());
if (name.hasPrefix(SCENERY_PATH))
return name.exceptPrefix(SCENERY_PATH.size());
+ if (name.hasPrefix(VOBJ_PATH))
+ return name.exceptPrefix(VOBJ_PATH.size());
return name;
}
@@ -37,5 +39,6 @@ const StringView loader_::IMAGE_PATH = "images/"_s;
const StringView loader_::ANIM_PATH = "anim/"_s;
const StringView loader_::SCENERY_PATH = "scenery/"_s;
const StringView loader_::TEMP_PATH = "../../../"_s;
+const StringView loader_::VOBJ_PATH = "vobj/"_s;
} // namespace floormat
diff --git a/loader/loader.hpp b/loader/loader.hpp
index f2617f04..3ed519e9 100644
--- a/loader/loader.hpp
+++ b/loader/loader.hpp
@@ -16,6 +16,7 @@ namespace floormat {
struct tile_atlas;
struct anim_atlas;
struct scenery_proto;
+struct vobj_info;
struct loader_
{
@@ -32,6 +33,8 @@ struct loader_
virtual const scenery_proto& scenery(StringView name) noexcept(false) = 0;
virtual StringView startup_directory() noexcept = 0;
static StringView strip_prefix(StringView name);
+ virtual const vobj_info& vobj(StringView name) = 0;
+ virtual ArrayView<struct vobj_info> vobj_list() = 0;
loader_(const loader_&) = delete;
loader_& operator=(const loader_&) = delete;
@@ -42,6 +45,7 @@ struct loader_
static const StringView ANIM_PATH;
static const StringView SCENERY_PATH;
static const StringView TEMP_PATH;
+ static const StringView VOBJ_PATH;
protected:
loader_();
diff --git a/loader/vobj-info.hpp b/loader/vobj-info.hpp
new file mode 100644
index 00000000..8bc7d189
--- /dev/null
+++ b/loader/vobj-info.hpp
@@ -0,0 +1,15 @@
+#pragma once
+#include <memory>
+#include <Corrade/Containers/String.h>
+
+namespace floormat {
+
+struct anim_atlas;
+
+struct vobj_info final
+{
+ String name, descr;
+ std::shared_ptr<anim_atlas> atlas;
+};
+
+} // namespace floormat
diff --git a/loader/vobj.cpp b/loader/vobj.cpp
new file mode 100644
index 00000000..9d167368
--- /dev/null
+++ b/loader/vobj.cpp
@@ -0,0 +1,102 @@
+#include "impl.hpp"
+#include "loader/vobj-info.hpp"
+#include "serialize/json-helper.hpp"
+#include "serialize/corrade-string.hpp"
+#include "src/anim-atlas.hpp"
+#include "src/anim.hpp"
+#include "compat/exception.hpp"
+#include <Corrade/Containers/ArrayViewStl.h>
+
+namespace floormat::loader_detail {
+struct vobj {
+ String name, descr, image_filename;
+};
+} // namespace floormat::loader_detail
+
+using floormat::loader_detail::vobj;
+
+template<>
+struct nlohmann::adl_serializer<vobj> {
+ static void to_json(json& j, const vobj& val);
+ static void from_json(const json& j, vobj& val);
+};
+
+void nlohmann::adl_serializer<vobj>::to_json(json& j, const vobj& val)
+{
+ j["name"] = val.name;
+ if (val.descr)
+ j["description"] = val.descr;
+ j["image"] = val.image_filename;
+}
+
+void nlohmann::adl_serializer<vobj>::from_json(const json& j, vobj& val)
+{
+ val.name = j["name"];
+ if (j.contains("description"))
+ val.descr = j["description"];
+ else
+ val.descr = val.name;
+ val.image_filename = j["image"];
+}
+
+namespace floormat::loader_detail {
+
+std::shared_ptr<struct anim_atlas> loader_impl::make_vobj_anim_atlas(StringView name, StringView image_filename)
+{
+ auto tex = texture(VOBJ_PATH, image_filename);
+ anim_def def;
+ def.object_name = name;
+ const auto size = tex.pixels().size();
+ const auto width = size[1], height = size[0];
+ def.pixel_size = { (unsigned)width, (unsigned)height };
+ def.nframes = 1;
+ def.fps = 0;
+ anim_group g;
+ g.name = "n"_s;
+ anim_frame f;
+ f.size = def.pixel_size;
+ g.frames = { f };
+ def.groups = { std::move(g) };
+
+ auto atlas = std::make_shared<struct anim_atlas>(name, tex, std::move(def));
+ return atlas;
+}
+
+void loader_impl::get_vobj_list()
+{
+ vobjs.clear();
+ vobj_atlas_map.clear();
+
+ auto vec = json_helper::from_json<std::vector<struct vobj>>(Path::join(VOBJ_PATH, "vobj.json"));
+
+ vobjs.reserve(vec.size());
+ vobj_atlas_map.reserve(2*vec.size());
+
+ for (const auto& [name, descr, img_name] : vec)
+ {
+ auto atlas = make_vobj_anim_atlas(name, img_name);
+ auto info = vobj_info{name, descr, atlas};
+ vobjs.push_back(std::move(info));
+ const auto& x = vobjs.back();
+ vobj_atlas_map[x.atlas->name()] = &x;
+ }
+}
+
+ArrayView<vobj_info> loader_impl::vobj_list()
+{
+ if (vobjs.empty())
+ get_vobj_list();
+ return vobjs;
+}
+
+const struct vobj_info& loader_impl::vobj(StringView name)
+{
+ if (vobjs.empty())
+ get_vobj_list();
+ auto it = vobj_atlas_map.find(name);
+ if (it == vobj_atlas_map.end())
+ fm_throw("no such vobj '{}'"_cf, name);
+ return *it->second;
+}
+
+} // namespace floormat::loader_detail