summaryrefslogtreecommitdiffhomepage
path: root/loader/vobj.cpp
blob: ebc35b165a04ae3d6f8bc6bd37796f3ca2aed253 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "impl.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 "loader/vobj-cell.hpp"
#include <Corrade/Containers/ArrayViewStl.h>
#include <Corrade/Containers/StridedArrayView.h>
#include <Corrade/Utility/Path.h>
#include <Magnum/Trade/ImageData.h>

namespace floormat::loader_detail {
struct vobj {
    String name, descr, image_filename;
};
} // namespace floormat::loader_detail

namespace nlohmann {

using floormat::loader_detail::vobj;

template<>
struct adl_serializer<vobj> {
    static void to_json(json& j, const vobj& val);
    static void from_json(const json& j, vobj& val);
};

void 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 adl_serializer<vobj>::from_json(const json& j, vobj& val)
{
    val = {};
    val.name = j["name"];
    if (j.contains("description"))
        val.descr = j["description"];
    else
        val.descr = val.name;
    val.image_filename = j["image"];
}

} // namespace nlohmann

namespace floormat::loader_detail {

bptr<class 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 = (unsigned)size[1], height = (unsigned)size[0];
    def.pixel_size = { width, height };
    def.nframes = 1;
    def.fps = 0;
    {
        auto group = anim_group {
            .name = "n"_s,
            .frames = { InPlaceInit, {
                anim_frame {
                    .ground = Vector2i(def.pixel_size/2),
                    .size = def.pixel_size,
                }},
            },
        };
        def.groups = Array<anim_group>{1};
        def.groups[0] = move(group);
    }
    auto atlas = bptr<class anim_atlas>(InPlace, name, tex, move(def));
    return atlas;
}

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());

    for (const auto& [name, descr, img_name] : vec)
    {
        auto atlas = make_vobj_anim_atlas(name, img_name);
        auto info = vobj_cell{name, descr, atlas};
        vobjs.push_back(move(info));
        const auto& x = vobjs.back();
        vobj_atlas_map[x.atlas->name()] = &x;
    }

    fm_assert(!vobjs.empty());
}

ArrayView<const vobj_cell> loader_impl::vobj_list()
{
    if (vobjs.empty())
        get_vobj_list();
    fm_assert(!vobjs.empty());
    return vobjs;
}

const struct vobj_cell& 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