summaryrefslogtreecommitdiffhomepage
path: root/loader/anim-traits.cpp
blob: 925fcfad6ee9729840d555b0c72bc543a48a0f33 (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
#include "anim-traits.hpp"
#include "atlas-loader-storage.hpp"
#include "anim-cell.hpp"
#include "src/anim-atlas.hpp"
#include "src/tile-defs.hpp"
#include "loader.hpp"
#include "compat/exception.hpp"
#include <cr/StringView.h>
#include <cr/Pointer.h>
#include <cr/Optional.h>
#include <cr/Move.h>
#include <mg/ImageData.h>
#include <mg/ImageView.h>

namespace floormat::loader_detail {

using anim_traits = atlas_loader_traits<anim_atlas>;
StringView anim_traits::loader_name() { return "anim_atlas"_s; }
auto anim_traits::atlas_of(const Cell& x) -> const std::shared_ptr<Atlas>& { return x.atlas; }
auto anim_traits::atlas_of(Cell& x) -> std::shared_ptr<Atlas>& { return x.atlas; }
StringView anim_traits::name_of(const Cell& x) { return x.name; }
StringView anim_traits::name_of(const Atlas& x) { return x.name(); }
String& anim_traits::name_of(Cell& x) { return x.name; }

void anim_traits::ensure_atlases_loaded(Storage& s)
{
    fm_debug_assert(s.name_map.empty());
    s.cell_array = {};
    s.name_map[loader.INVALID] = -1uz;
}

auto anim_traits::make_invalid_atlas(Storage& s) -> Pointer<Cell>
{
    fm_debug_assert(!s.invalid_atlas);

    constexpr auto size = Vector2ui{tile_size_xy*3/4};
    constexpr auto ground = Vector2i(size/2);

    auto frame = anim_frame {
        .ground = ground,
        .offset = {},
        .size = size,
    };
    auto groups = Array<anim_group>{ValueInit, 1};
    groups[0] = anim_group {
        .name = "n"_s,
        .frames = array({ frame }),
    };
    auto def = anim_def {
        .object_name = loader.INVALID,
        .anim_name = loader.INVALID,
        .groups = Utility::move(groups),
        .pixel_size = size,
        .scale = anim_scale::fixed{size.x(), true},
        .nframes = 1,
    };
    auto atlas = std::make_shared<class anim_atlas>(loader.INVALID, loader.make_error_texture(size), std::move(def));
    auto info = anim_cell {
        .atlas = atlas,
        .name = loader.INVALID,
    };
    return Pointer<anim_cell>{ InPlace, Utility::move(info) };
}

auto anim_traits::make_atlas(StringView name, const Cell& c) -> std::shared_ptr<Atlas>
{
    return {}; // todo
}

auto anim_traits::make_cell(StringView name) -> Optional<Cell>
{
    return { InPlace, Cell{ .atlas = {}, .name = name, } };
}

} // namespace floormat::loader_detail