summaryrefslogtreecommitdiffhomepage
path: root/loader/scenery.cpp
blob: 879e72d986fb70c658cefff001af27f49116307d (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
#include "impl.hpp"
#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 "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()
{
    sceneries_array.clear();
    sceneries_array = json_helper::from_json<std::vector<scenery_cell>>(Path::join(SCENERY_PATH, "scenery.json"));

    if constexpr(true) // todo!
    {
        auto proto = scenery_proto{};
        proto.atlas = invalid_anim_atlas().atlas;
        proto.bbox_size = Vector2ub{20};
        proto.subtype = generic_scenery_proto{false, true};
        sceneries_array.push_back({ .name = INVALID, .proto = proto });
    }

    sceneries_map.clear();
    sceneries_map.reserve(sceneries_array.size() * 2);

    for (const scenery_cell& s : sceneries_array)
    {
        if (sceneries_map.contains(s.name))
            fm_abort("duplicate scenery name '%s'", s.name.data());
        sceneries_map[s.name] = &s;
    }

    fm_assert(!sceneries_map.empty());
}

ArrayView<const scenery_cell> loader_impl::sceneries()
{
    if (sceneries_array.empty()) [[likely]]
        get_scenery_list();
    fm_assert(!sceneries_array.empty());
    return sceneries_array;
}

const scenery_proto& loader_impl::scenery(StringView name) noexcept(false)
{
    fm_soft_assert(check_atlas_name(name));
    if (sceneries_array.empty())
        get_scenery_list();
    fm_assert(!sceneries_array.empty());
    auto it = sceneries_map.find(name);
    if (it == sceneries_map.end())
        fm_throw("no such scenery: '{}'"_cf, name);
    return it->second->proto;
}

} // namespace floormat::loader_detail