summaryrefslogtreecommitdiffhomepage
path: root/loader/wall-traits.cpp
blob: 9fbe4286e416463d350a93cbfa57679373855f8f (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
#include "wall-traits.hpp"
#include "compat/exception.hpp"
#include "compat/vector-wrapper.hpp"
#include "atlas-loader-storage.hpp"
#include "wall-cell.hpp"
#include "loader.hpp"
#include "src/tile-defs.hpp"
#include "src/wall-atlas.hpp"
#include <cr/StringView.h>
#include <cr/Pointer.h>
#include <mg/ImageData.h>
#include <mg/ImageView.h>

namespace floormat::loader_detail {

namespace { const auto placeholder_cell = wall_cell{}; }
using wall_traits = atlas_loader_traits<wall_atlas>;
StringView atlas_loader_traits<wall_atlas>::loader_name() { return "wall_atlas"_s; }
auto atlas_loader_traits<wall_atlas>::atlas_of(const Cell& x) -> const std::shared_ptr<Atlas>& { return x.atlas; }
auto atlas_loader_traits<wall_atlas>::atlas_of(Cell& x) -> std::shared_ptr<Atlas>& { return x.atlas; }
StringView atlas_loader_traits<wall_atlas>::name_of(const Cell& x) { return x.name; }
StringView atlas_loader_traits<wall_atlas>::name_of(const Atlas& x) { return x.name(); }

void wall_traits::ensure_atlases_loaded(Storage& st)
{
    if (!st.cell_array.empty()) [[likely]]
        return;
    fm_assert(st.name_map.empty());

    st.cell_array = wall_cell::load_atlases_from_json().vec;
    st.name_map.reserve(st.cell_array.size());

    for (auto& c : st.cell_array)
    {
        fm_soft_assert(c.name != "<invalid>"_s);
        fm_soft_assert(loader.check_atlas_name(c.name));
        StringView name = c.name;
        st.name_map[name] = &c;
    }

    fm_assert(!st.cell_array.empty());
}

auto wall_traits::make_invalid_atlas(Storage& st) -> const Cell&
{
    if (st.invalid_atlas) [[likely]]
        return *st.invalid_atlas;

    constexpr auto name = loader_::INVALID;
    constexpr auto frame_size = Vector2ui{tile_size_xy, tile_size_z};

    auto a = std::make_shared<class wall_atlas>(
        wall_atlas_def {
            Wall::Info{.name = name, .depth = 8},
            array<Wall::Frame>({{ {}, frame_size}, }),
            array<Wall::Direction>({{
                { .index = 0, .count = 1, .pixel_size = frame_size, .is_defined = true, }
            } }),
            {{ {.val = 0}, {}, }},
            {1u},
        }, name, loader.make_error_texture(frame_size));
    st.invalid_atlas = Pointer<wall_cell>{InPlaceInit, wall_cell{ .name = name, .atlas = std::move(a) } };
    return *st.invalid_atlas;
}

auto wall_traits::make_atlas(StringView name, const Cell&) -> std::shared_ptr<Atlas>
{
    char file_buf[fm_FILENAME_MAX], json_buf[fm_FILENAME_MAX];
    auto file = loader.make_atlas_path(file_buf, loader.WALL_TILESET_PATH, name);
    int json_size = std::snprintf(json_buf, std::size(json_buf), "%s.json", file_buf);
    fm_soft_assert(json_size != 0 && (size_t)json_size <= std::size_t(json_buf));
    auto json_name = StringView{json_buf, (size_t)json_size};
    auto def = wall_atlas_def::deserialize(json_name);
    auto tex = loader.texture(""_s, file);
    fm_soft_assert(name == def.header.name);
    fm_soft_assert(!def.frames.isEmpty());
    auto atlas = std::make_shared<class wall_atlas>(std::move(def), file, tex);
    return atlas;
}

} // namespace floormat::loader_detail