Files
anm2ed/tests/draw.cpp
T
shweet 03bbb93ba6 Replace glm with vec.h
Vec2, IVec2, Vec3, Vec4, IVec4 and a column-major Mat4, with glm's formulas, in one header.
Free functions are vec_ and mat4_ prefixed. The leaked using namespace glm is gone.
Render goldens still match.
2026-11-22 18:15:02 -05:00

246 lines
12 KiB
C++

#include "common.h"
#include "math_util.h"
#include "model/draw.h"
#include "model/frames.h"
TEST_CASE("draw lists put the root first and each item's onion samples before it")
{
auto anm2 = model_load(file_load(CORPUS_DIR / "02_items.anm2"));
auto& animation = *anm2.animation_get(0);
auto draws = animation_draws_get(anm2, animation, {.time = 1.0f, .samples = {{-1.0f}, {1.0f}}});
REQUIRE(!draws.empty());
CHECK(draws.front().type == DrawType::ROOT);
for (int i = 0; i + 1 < (int)draws.size(); ++i)
if (draws[i].sample != -1)
CHECK((draws[i + 1].id == draws[i].id && draws[i + 1].type == draws[i].type));
auto layerDraws = std::ranges::count_if(
draws, [](const Draw& draw) { return draw.type == DrawType::LAYER && draw.sample == -1; });
CHECK(layerDraws > 0);
CHECK(animation_rect(anm2, animation, true) != Vec4(-1.0f));
}
TEST_CASE("root transforms fold into layer draws")
{
auto anm2 = model_load(file_load(CORPUS_DIR / "04_group_root_transform.anm2"));
auto& animation = *anm2.animation_get(0);
auto plain = animation_draws_get(anm2, animation, {});
auto rooted = animation_draws_get(anm2, animation, {.isRootTransform = true});
REQUIRE(plain.size() == rooted.size());
bool isAnyMoved = false;
for (int i = 0; i < (int)plain.size(); ++i)
isAnyMoved |= plain[i].type == DrawType::LAYER && plain[i].parent != rooted[i].parent;
CHECK(isAnyMoved);
}
TEST_CASE("a layer frame's depth moves it among the layers for that frame only")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "spin.anm2"));
auto layer_ids_get = [&](float time) {
std::vector<int> ids;
for (auto& draw : animation_draws_get(anm2, *anm2.animation_get(0), {.time = time}))
if (draw.type == DrawType::LAYER)
ids.push_back(draw.id);
return ids;
};
CHECK(layer_ids_get(0.0f) == std::vector{1, 0});
anm2.frame_edit({0, LAYER, 0, 0})->depth = -1;
CHECK(layer_ids_get(0.0f) == std::vector{0, 1});
CHECK(layer_ids_get(5.0f) == std::vector{1, 0});
auto text = model_to_string(anm2);
CHECK(game_document_get(text).find("Depth") == std::string::npos);
CHECK(model_load(text).frame_get({0, LAYER, 0, 0})->depth == -1);
CHECK(isaac_issues_get(anm2).depthFrames == 1);
}
TEST_CASE("a layer frame's blur reaches its child's draws and stays out of the game's copy")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "nested.anm2"));
anm2.frame_edit({0, LAYER, 1, 0})->blur = 6.0f;
bool isAnyChild = false;
for (auto& draw : animation_draws_get(anm2, *anm2.animation_get(0), {}))
if (draw.type == DrawType::LAYER && draw.id == 1)
{
isAnyChild = true;
CHECK(draw.frame.blur >= 6.0f);
}
CHECK(isAnyChild);
auto text = model_to_string(anm2);
CHECK(game_document_get(text).find("Blur") == std::string::npos);
CHECK(model_load(text).frame_get({0, LAYER, 1, 0})->blur == 6.0f);
CHECK(isaac_issues_get(anm2).blurFrames == 1);
}
TEST_CASE("an animation layer draws its child animation, from this file or a library")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "nested.anm2"));
auto spin = model_load(file_load(TEST_DIR / "render" / "spin.anm2"));
DrawOptions options = {.library_get = [&](const Model&, int id) { return id == 0 ? &spin : nullptr; }};
auto red_position_get = [&](int layerId) {
std::optional<Vec2> position{};
for (auto& draw : animation_draws_get(anm2, *anm2.animation_get(0), options))
if (draw.type == DrawType::LAYER && draw.id == layerId && draw.frame.crop == Vec2())
position = Vec2(draw.parent * Vec4(draw.frame.position, 0.0f, 1.0f));
return position;
};
// The library's red square, half size at (40, 0), starts on its fifth frame (at x 16).
CHECK(red_position_get(0) == Vec2(48.0f, 0.0f));
// This file's Inner holds its first frame however far the parent plays.
options.time = 1.0f;
CHECK(red_position_get(1) == Vec2(-40.0f, 0.0f));
// An animation never draws inside itself.
CHECK_FALSE(red_position_get(3));
// Once stops on the child's last frame. Loop wraps around it.
options.time = 6.0f;
anm2.frame_edit({0, LAYER, 1, 0})->childMode = ChildMode::ONCE;
CHECK(red_position_get(1) == Vec2(-40.0f, 10.0f));
anm2.frame_edit({0, LAYER, 1, 0})->childMode = ChildMode::LOOP;
CHECK(red_position_get(1) == Vec2(-40.0f, 0.0f));
// Without the library loaded, its layer draws nothing.
options.library_get = {};
CHECK_FALSE(red_position_get(0));
auto text = model_serialize(anm2, SERIALIZE_ANM2ED_DEFAULT);
CHECK(model_hash(model_load(text)) == model_hash(anm2));
CHECK(isaac_issues_get(anm2).animationLayers == 3);
CHECK(game_document_get(model_to_string(anm2)).find(" Animation=\"") == std::string::npos);
}
TEST_CASE("an alpha offset is kept by the editor's copy only")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "spin.anm2"));
anm2.frame_edit({0, LAYER, 0, 0})->colorOffset.a = -0.5f;
auto text = model_to_string(anm2);
CHECK(game_document_get(text).find("AlphaOffset") == std::string::npos);
CHECK(model_load(text).frame_get({0, LAYER, 0, 0})->colorOffset.a == doctest::Approx(-127.0f / 255.0f));
CHECK(isaac_issues_get(anm2).alphaOffsetFrames == 1);
}
TEST_CASE("only SVG spritesheets that layers show count against Rebirth")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "vector.anm2"));
CHECK(spritesheets_vector_used_get(anm2) == std::set{0});
anm2.content.layers.clear();
CHECK(spritesheets_vector_used_get(anm2).empty());
}
TEST_CASE("child animations fire their triggers as they reach them")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "nested.anm2"));
auto spin = model_load(file_load(TEST_DIR / "render" / "spin.anm2"));
spin.content.events.push_back({.id = 0, .name = "spun"});
spin.animation_edit(0)->triggers.frames.push_back({.atFrame = 5, .eventIds = {0}});
anm2.content.events.push_back({.id = 0, .name = "inner"});
anm2.animation_edit(1)->triggers.frames.push_back({.atFrame = 0, .eventIds = {0}});
auto library_get = [&](const Model&, int id) {
return id == 0 ? &spin : nullptr;
};
auto sources_get = [&](int time) {
std::vector<const Model*> sources;
for (auto& fire : animation_triggers_get(anm2, *anm2.animation_get(0), time, library_get))
sources.push_back(fire.source);
return sources;
};
// Inner (held on its first frame) fires once, when the parent starts. The library's Spin, started on its fifth
// frame, reaches its sixth (and its trigger) one frame later.
CHECK(sources_get(0) == std::vector<const Model*>{&anm2});
CHECK(sources_get(1) == std::vector<const Model*>{&spin});
CHECK(sources_get(2).empty());
}
TEST_CASE("flattening animation layers bakes their children into plain layers that draw the same")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "nested.anm2"));
auto spin = model_load(file_load(TEST_DIR / "render" / "spin.anm2"));
auto library_get = [&](const Model&, int id) {
return id == 0 ? &spin : nullptr;
};
auto flat = animation_layers_flatten(anm2, library_get, [](const Model&, const std::filesystem::path& path) {
return std::filesystem::path("library") / path;
});
CHECK(std::ranges::none_of(flat.content.layers, &Layer::is_animation));
CHECK(flat.content.spritesheets.back().path == std::filesystem::path("library") / "sheet.png");
// Every frame, the plain layers draw where the animation layers' children did.
auto points_get = [&](const Model& model, const LibraryGet& get, float time) {
std::vector<Vec2> points{};
for (auto& draw : animation_draws_get(model, *model.animation_get(0), {.time = time, .library_get = get}))
if (draw.type == DrawType::LAYER)
points.push_back(vec_round(Vec2(draw.parent * draw_quad_model_get(draw) * Vec4(1, 1, 0, 1))));
std::ranges::sort(points, [](Vec2 a, Vec2 b) { return std::tie(a.x, a.y) < std::tie(b.x, b.y); });
return points;
};
for (float time = 0.0f; time < 8.0f; time += 1.0f)
CHECK(points_get(flat, {}, time) == points_get(anm2, library_get, time));
auto text = model_to_string(anm2, {.gameModel = &flat});
auto game = game_document_get(text);
CHECK(game.find(" Animation=\"") == std::string::npos);
CHECK(game.find("spin/red") != std::string::npos);
CHECK(std::ranges::count_if(model_load(text).content.layers, &Layer::is_animation) == 3);
// Children's triggers join the flattened animation's, their events found (or added) by name.
spin.content.events.push_back({.id = 0, .name = "spun"});
spin.animation_edit(0)->triggers.frames.push_back({.atFrame = 5, .eventIds = {0}});
flat = animation_layers_flatten(anm2, library_get,
[](const Model&, const std::filesystem::path& path) { return path; });
auto& triggers = flat.animation_get(0)->triggers.frames;
REQUIRE(triggers.size() == 1);
CHECK(triggers.front().atFrame == 1);
CHECK(item_get(flat.content.events, triggers.front().eventIds.front())->name == "spun");
}
TEST_CASE("an animation layer's child sits where the layer's frame puts it")
{
auto anm2 = model_load(file_load(TEST_DIR / "render" / "nested.anm2"));
auto placement = layer_child_transform_get(*anm2.animation_get(0), 0, 0.0f, true);
REQUIRE(placement);
CHECK(Vec2(*placement * Vec4(16.0f, 0.0f, 0.0f, 1.0f)) == Vec2(48.0f, 0.0f));
CHECK_FALSE(layer_child_transform_get(*anm2.animation_get(0), 99, 0.0f, true));
}
TEST_CASE("the game's copy stands in for depth and alpha offsets")
{
auto anm2 = model_load(R"({ "sheets": [ { "path": "a.png" } ],
"layers": [ { "name": "a" }, { "name": "b" }, { "name": "c" } ],
"animations": { "A": { "length": 6, "layers": [
{ "layer": 0, "frames": [ { "crop": [0, 0], "size": [1, 1], "delay": 2 },
{ "crop": [1, 0], "size": [1, 1], "delay": 2, "depth": 2, "ease": "lerp" },
{ "crop": [1, 0], "size": [1, 1], "pos": [8, 0], "delay": 2, "depth": -1 } ] },
{ "layer": 1, "frames": [ { "crop": [2, 0], "size": [1, 1], "delay": 6, "depth": -1 } ] },
{ "layer": 2, "frames": [ { "crop": [3, 0], "size": [1, 1], "delay": 6, "offset": [0, 0, 0, -64] } ] } ] } } })");
auto game = isaac_bridge(anm2);
// Every frame, the same crops draw in the same order and places, with no depth left.
auto draws_get = [](const Model& model, float time) {
std::vector<std::pair<Vec2, Vec2>> draws{};
for (auto& draw : animation_draws_get(model, *model.animation_get(0), {.time = time}))
if (draw.type == DrawType::LAYER && draw.frame.isVisible)
draws.emplace_back(draw.frame.crop, draw.frame.position);
return draws;
};
for (float time = 0.0f; time < 6.0f; time += 0.5f)
CHECK(draws_get(game, time) == draws_get(anm2, time));
CHECK(isaac_issues_get(game).depthFrames == 0);
CHECK(std::ranges::any_of(game.content.layers, [](const Layer& layer) { return layer.name == "a (depth +2)"; }));
// The alpha offset joins the alpha tint.
auto& offsetFrame = *game.frame_get({0, LAYER, 2, 0});
CHECK(offsetFrame.colorOffset.a == 0.0f);
CHECK(float_to_uint8(offsetFrame.tint.a) == 255 - 64);
// A document saved with Ignore and Save remembers it, in either format.
anm2.info.isIsaacIgnored = true;
CHECK(model_load(model_to_string(anm2)).info.isIsaacIgnored);
CHECK(model_load(model_to_json(anm2)).info.isIsaacIgnored);
}