summaryrefslogtreecommitdiffhomepage
path: root/loader/anim-traits.cpp
blob: 09da71e88885240a102955cd9c54a2ed33d07db1 (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
#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 "serialize/json-helper.hpp"
#include "serialize/anim.hpp"
#include "compat/exception.hpp"
#include "compat/borrowed-ptr.inl"
#include <cr/StringView.h>
#include <cr/GrowableArray.h>
#include <cr/StridedArrayView.h>
#include <cr/Pointer.h>
#include <cr/Optional.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 bptr<Atlas>& { return x.atlas; }
auto anim_traits::atlas_of(Cell& x) -> bptr<Atlas>& { return x.atlas; }
StringView anim_traits::name_of(const Cell& x) { return x.name; }
String& anim_traits::name_of(Cell& x) { return x.name; }

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

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

    constexpr auto size = Vector2ui{tile_size_xy/2};
    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 = move(groups),
        .pixel_size = size,
        .scale = anim_scale::fixed{size.x(), true},
        .nframes = 1,
    };
    auto atlas = bptr<class anim_atlas>{InPlace, loader.INVALID, loader.make_error_texture(size), move(def)};
    auto info = anim_cell {
        .atlas = atlas,
        .name = loader.INVALID,
    };
    return info;
}

auto anim_traits::make_atlas(StringView name, const Cell&) -> bptr<Atlas>
{
    char buf[fm_FILENAME_MAX];
    auto json_path = loader.make_atlas_path(buf, {}, name, ".json"_s);
    auto anim_info = json_helper::from_json<struct anim_def>(json_path);

    for (anim_group& group : anim_info.groups)
    {
        if (!group.mirror_from.isEmpty())
        {
            const auto *begin = anim_info.groups.data(), *end = begin + anim_info.groups.size();
            const auto* it = std::find_if(begin, end, [&](const anim_group& x) { return x.name == group.mirror_from; });
            if (it == end)
                fm_throw("can't find group '{}' to mirror from '{}'"_cf, group.mirror_from, group.name);
            group.frames = array(ArrayView<const anim_frame>{it->frames.data(), it->frames.size()});
            for (anim_frame& f : group.frames)
                f.ground = Vector2i((Int)f.size[0] - f.ground[0], f.ground[1]);
        }
    }

    auto tex = loader.texture(""_s, name);

    fm_soft_assert(!anim_info.object_name.isEmpty());
    fm_soft_assert(anim_info.pixel_size.product() > 0);
    fm_soft_assert(!anim_info.groups.isEmpty());
    fm_soft_assert(anim_info.nframes > 0);
    fm_soft_assert(anim_info.nframes == 1 || anim_info.fps > 0);
    const auto size = tex.pixels().size();
    const auto width = size[1], height = size[0];
    fm_soft_assert(anim_info.pixel_size[0] == width && anim_info.pixel_size[1] == height);

    auto atlas = bptr<class anim_atlas>{InPlace, name, tex, move(anim_info)};
    return atlas;
}

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

} // namespace floormat::loader_detail