summaryrefslogtreecommitdiffhomepage
path: root/loader/vobj.cpp
blob: 9d167368ff1a889a513dbb28a1c8d1402edf94c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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