Remove namespaces, and make file-local helpers static

Names that only a namespace told apart get prefixes: TOOL_PAN, FONT_BOLD, ICON_FOLDER, COLOR_WHITE, path_to_utf8 and so on.
The OpenGL shader class is ShaderProgram, and its uniform ShaderUniform, beside the document's Shader and Uniform.
Free functions a member of the same name would hide are called through :: for now.
Two unused shader window helpers are gone.
This commit is contained in:
shweet committed 2026-11-22 17:25:09 -05:00
1 parent 7fb2c607c2
commit 666f65bf3a
174 files changed
+2396 -3154

No files matched your search

+8 -11
View File
@@ -8,9 +8,6 @@
#include "image.h"
#include "model/xml.h"
using namespace anm2ed;
using namespace anm2ed::resource;
TEST_CASE("Image png round trip and uid tracking")
{
const std::vector<uint8_t> pixels{255, 0, 0, 255, 0, 255, 0, 128, 0, 0, 255, 0, 10, 20, 30, 40};
@@ -66,7 +63,7 @@ TEST_CASE("AssetStore keys assets by content")
TEST_CASE("an SVG spritesheet loads in SVG units and draws again at any scale")
{
Image image(test::TEST_DIR / "render" / "vector.svg");
Image image(TEST_DIR / "render" / "vector.svg");
REQUIRE(image.is_valid());
CHECK(image.is_vector());
CHECK(image.size == glm::ivec2(32, 16));
@@ -82,7 +79,7 @@ TEST_CASE("an SVG spritesheet loads in SVG units and draws again at any scale")
TEST_CASE("SVG spritesheets compose into SVGs that draw the same")
{
Image vector(test::TEST_DIR / "render" / "vector.svg");
Image vector(TEST_DIR / "render" / "vector.svg");
REQUIRE(vector.is_vector());
// The circle (left half) and the square (right half) swapped: each part is a clipped view of the drawing.
@@ -111,7 +108,7 @@ TEST_CASE("an SVG's shapes meet without a seam")
<rect x="0" y="0" width="10.5" height="4" fill="#804020"/>
<rect x="10.5" y="0" width="10" height="4" fill="#804020"/></svg>)");
auto path = std::filesystem::temp_directory_path() / "anm2ed_seam.svg";
test::file_save(path, *text);
file_save(path, *text);
Image image(path);
std::filesystem::remove(path);
REQUIRE(image.is_vector());
@@ -123,17 +120,17 @@ TEST_CASE("an SVG's shapes meet without a seam")
TEST_CASE("Embedded resources load")
{
for (auto& info : friends::FRIENDS)
for (auto& info : FRIENDS)
{
INFO("friend: ", info.name);
model::Model model{};
Model model{};
std::string errorString{};
CHECK(model::model_load_string(model, std::string_view((const char*)info.anm2->data, info.anm2->size), &errorString));
CHECK(model_load_string(model, std::string_view((const char*)info.anm2->data, info.anm2->size), &errorString));
CHECK(model.animations_count_get() > 0);
CHECK(Image(info.png->data, info.png->size).is_valid());
}
for (auto& icon : icon::ICONS)
for (auto& icon : ICONS)
CHECK(std::string_view((const char*)icon.svg->data, icon.svg->size).starts_with("<svg"));
for (auto& font : font::FONTS)
for (auto& font : FONTS)
CHECK(font->size > 0);
}
+18 -21
View File
@@ -12,8 +12,6 @@
#include "model/json.h"
#include "model/xml.h"
namespace anm2ed::test
{
inline const std::filesystem::path TEST_DIR = ANM2ED_TEST_DIR;
inline const std::filesystem::path CORPUS_DIR = TEST_DIR / "corpus";
inline const std::filesystem::path GOLDEN_DIR = TEST_DIR / "golden";
@@ -22,10 +20,10 @@ namespace anm2ed::test
{"editor", SERIALIZE_EDITOR_DEFAULT},
{"isaac", SERIALIZE_ISAAC_DEFAULT},
{"anm2ed", SERIALIZE_ANM2ED_DEFAULT}};
inline constexpr std::pair<const char*, types::merge::Type> MERGES[] = {{"merge_prepend", types::merge::PREPEND},
{"merge_append", types::merge::APPEND},
{"merge_replace", types::merge::REPLACE},
{"merge_ignore", types::merge::IGNORE}};
inline constexpr std::pair<const char*, Merge> MERGES[] = {{"merge_prepend", MERGE_PREPEND},
{"merge_append", MERGE_APPEND},
{"merge_replace", MERGE_REPLACE},
{"merge_ignore", MERGE_IGNORE}};
using Variant = std::pair<std::string, std::string>;
@@ -52,27 +50,27 @@ namespace anm2ed::test
return fixtures;
}
inline model::Model model_load(const std::string& text)
inline Model model_load(const std::string& text)
{
model::Model model{};
REQUIRE(model::model_load_string(model, text));
Model model{};
REQUIRE(model_load_string(model, text));
return model;
}
// Every serialization and merge the editor can produce for a document; goldens lock each one down.
inline std::vector<Variant> variants_get(const model::Model& model)
inline std::vector<Variant> variants_get(const Model& model)
{
std::vector<Variant> variants{};
for (auto [name, flags] : PRESETS)
variants.emplace_back(name, model::model_serialize(model, flags));
variants.emplace_back("save", model::model_to_string(model));
variants.emplace_back(name, model_serialize(model, flags));
variants.emplace_back("save", model_to_string(model));
for (auto [name, type] : MERGES)
{
auto merged = model;
std::set<int> sources{0, 1, 2};
edit::animations_merge(merged, 0, sources, type, type == types::merge::PREPEND || type == types::merge::REPLACE,
animations_merge(merged, 0, sources, type, type == MERGE_PREPEND || type == MERGE_REPLACE,
{});
variants.emplace_back(name, model::model_serialize(merged, SERIALIZE_ANM2ED_DEFAULT));
variants.emplace_back(name, model_serialize(merged, SERIALIZE_ANM2ED_DEFAULT));
}
return variants;
}
@@ -88,19 +86,19 @@ namespace anm2ed::test
}
// Save/reload properties every document must hold.
inline void invariants_check(const model::Model& model)
inline void invariants_check(const Model& model)
{
// The first save may repair invalid input (dangling ids, duplicates); after that, saves must be fixed points.
auto text = model::model_to_string(model);
auto text = model_to_string(model);
auto reloaded = model_load(text);
auto resaved = model::model_to_string(reloaded);
auto resaved = model_to_string(reloaded);
CHECK(resaved == text);
CHECK(model::model_hash(model_load(resaved)) == model::model_hash(reloaded));
CHECK(model_hash(model_load(resaved)) == model_hash(reloaded));
// Loading only the game's view and re-saving it for the game must not change it.
auto game = game_document_get(model::model_to_string(model));
CHECK(model::model_serialize(model_load(game), SERIALIZE_ISAAC_DEFAULT | SERIALIZE_PURGE_DEFAULT_COLORS) == game);
auto game = game_document_get(model_to_string(model));
CHECK(model_serialize(model_load(game), SERIALIZE_ISAAC_DEFAULT | SERIALIZE_PURGE_DEFAULT_COLORS) == game);
}
// Compares against a stored golden; ANM2ED_UPDATE_GOLDEN (or a missing golden when isBootstrap) rewrites it.
@@ -115,4 +113,3 @@ namespace anm2ed::test
REQUIRE(std::filesystem::exists(path));
CHECK(file_load(path) == text);
}
}
+2 -5
View File
@@ -1,8 +1,5 @@
#include "common.h"
using namespace anm2ed;
using namespace anm2ed::test;
// Private corpus run: ANM2ED_CORPUS=<file listing .anm2 paths>. Baselines are written on first sight to
// ANM2ED_CORPUS_BASELINE (default ~/.cache/anm2ed/corpus) and compared on every later run. Nothing here is committed.
TEST_CASE("local corpus matches baselines" * doctest::skip(!is_env_set("ANM2ED_CORPUS")))
@@ -17,9 +14,9 @@ TEST_CASE("local corpus matches baselines" * doctest::skip(!is_env_set("ANM2ED_C
if (line.empty()) continue;
INFO("file: ", line);
auto key = std::format("{:016x}", std::hash<std::string>{}(line));
model::Model anm2{};
Model anm2{};
std::string error{};
if (!model::model_load(anm2, line, &error))
if (!model_load(anm2, line, &error))
{
golden_check(baseline / (key + ".error.txt"), error, true);
continue;
+55 -58
View File
@@ -4,36 +4,33 @@
#include "model/draw.h"
#include "model/frames.h"
using namespace anm2ed;
using namespace anm2ed::test;
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 = model::animation_draws_get(anm2, animation, {.time = 1.0f, .samples = {{-1.0f}, {1.0f}}});
auto draws = animation_draws_get(anm2, animation, {.time = 1.0f, .samples = {{-1.0f}, {1.0f}}});
REQUIRE(!draws.empty());
CHECK(draws.front().type == model::DrawType::ROOT);
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 model::Draw& draw)
{ return draw.type == model::DrawType::LAYER && draw.sample == -1; });
auto layerDraws = std::ranges::count_if(draws, [](const Draw& draw)
{ return draw.type == DrawType::LAYER && draw.sample == -1; });
CHECK(layerDraws > 0);
CHECK(model::animation_rect(anm2, animation, true) != glm::vec4(-1.0f));
CHECK(animation_rect(anm2, animation, true) != glm::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 = model::animation_draws_get(anm2, animation, {});
auto rooted = model::animation_draws_get(anm2, animation, {.isRootTransform = true});
auto plain = animation_draws_get(anm2, animation, {});
auto rooted = animation_draws_get(anm2, animation, {.isRootTransform = true});
REQUIRE(plain.size() == rooted.size());
bool isAnyMoved{};
for (int i = 0; i < (int)plain.size(); ++i)
isAnyMoved |= plain[i].type == model::DrawType::LAYER && plain[i].parent != rooted[i].parent;
isAnyMoved |= plain[i].type == DrawType::LAYER && plain[i].parent != rooted[i].parent;
CHECK(isAnyMoved);
}
@@ -43,8 +40,8 @@ TEST_CASE("a layer frame's depth moves it among the layers for that frame only")
auto layer_ids_get = [&](float time)
{
std::vector<int> ids{};
for (const auto& draw : model::animation_draws_get(anm2, *anm2.animation_get(0), {.time = time}))
if (draw.type == model::DrawType::LAYER) ids.push_back(draw.id);
for (const 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});
@@ -53,10 +50,10 @@ TEST_CASE("a layer frame's depth moves it among the layers for that frame only")
CHECK(layer_ids_get(0.0f) == std::vector{0, 1});
CHECK(layer_ids_get(5.0f) == std::vector{1, 0});
auto text = model::model_to_string(anm2);
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(model::isaac_issues_get(anm2).depthFrames == 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")
@@ -64,30 +61,30 @@ TEST_CASE("a layer frame's blur reaches its child's draws and stays out of the g
auto anm2 = model_load(file_load(TEST_DIR / "render" / "nested.anm2"));
anm2.frame_edit({0, LAYER, 1, 0})->blur = 6.0f;
bool isAnyChild{};
for (const auto& draw : model::animation_draws_get(anm2, *anm2.animation_get(0), {}))
if (draw.type == model::DrawType::LAYER && draw.id == 1)
for (const 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::model_to_string(anm2);
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(model::isaac_issues_get(anm2).blurFrames == 1);
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"));
model::DrawOptions options{.library_get = [&](const model::Model&, int id) { return id == 0 ? &spin : nullptr; }};
DrawOptions options{.library_get = [&](const Model&, int id) { return id == 0 ? &spin : nullptr; }};
auto red_position_get = [&](int layerId)
{
std::optional<glm::vec2> position{};
for (const auto& draw : model::animation_draws_get(anm2, *anm2.animation_get(0), options))
if (draw.type == model::DrawType::LAYER && draw.id == layerId && draw.frame.crop == glm::vec2())
for (const auto& draw : animation_draws_get(anm2, *anm2.animation_get(0), options))
if (draw.type == DrawType::LAYER && draw.id == layerId && draw.frame.crop == glm::vec2())
position = glm::vec2(draw.parent * glm::vec4(draw.frame.position, 0.0f, 1.0f));
return position;
};
@@ -111,28 +108,28 @@ TEST_CASE("an animation layer draws its child animation, from this file or a lib
options.library_get = {};
CHECK_FALSE(red_position_get(0));
auto text = model::model_serialize(anm2, SERIALIZE_ANM2ED_DEFAULT);
CHECK(model::model_hash(model_load(text)) == model::model_hash(anm2));
CHECK(model::isaac_issues_get(anm2).animationLayers == 3);
CHECK(game_document_get(model::model_to_string(anm2)).find(" Animation=\"") == std::string::npos);
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::model_to_string(anm2);
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(model::isaac_issues_get(anm2).alphaOffsetFrames == 1);
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(model::spritesheets_vector_used_get(anm2) == std::set{0});
CHECK(spritesheets_vector_used_get(anm2) == std::set{0});
anm2.content.layers.clear();
CHECK(model::spritesheets_vector_used_get(anm2).empty());
CHECK(spritesheets_vector_used_get(anm2).empty());
}
TEST_CASE("child animations fire their triggers as they reach them")
@@ -143,19 +140,19 @@ TEST_CASE("child animations fire their triggers as they reach them")
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::Model&, int id) { return id == 0 ? &spin : nullptr; };
auto library_get = [&](const Model&, int id) { return id == 0 ? &spin : nullptr; };
auto sources_get = [&](int time)
{
std::vector<const model::Model*> sources{};
for (const auto& fire : model::animation_triggers_get(anm2, *anm2.animation_get(0), time, library_get))
std::vector<const Model*> sources{};
for (const 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::Model*>{&anm2});
CHECK(sources_get(1) == std::vector<const model::Model*>{&spin});
CHECK(sources_get(0) == std::vector<const Model*>{&anm2});
CHECK(sources_get(1) == std::vector<const Model*>{&spin});
CHECK(sources_get(2).empty());
}
@@ -163,51 +160,51 @@ TEST_CASE("flattening animation layers bakes their children into plain layers th
{
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::Model&, int id) { return id == 0 ? &spin : nullptr; };
auto library_get = [&](const Model&, int id) { return id == 0 ? &spin : nullptr; };
auto flat =
edit::animation_layers_flatten(anm2, library_get, [](const model::Model&, const std::filesystem::path& path)
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, &model::Layer::is_animation));
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& model, const model::LibraryGet& get, float time)
auto points_get = [&](const Model& model, const LibraryGet& get, float time)
{
std::vector<glm::vec2> points{};
for (const auto& draw :
model::animation_draws_get(model, *model.animation_get(0), {.time = time, .library_get = get}))
if (draw.type == model::DrawType::LAYER)
points.push_back(glm::round(glm::vec2(draw.parent * model::draw_quad_model_get(draw) * glm::vec4(1, 1, 0, 1))));
animation_draws_get(model, *model.animation_get(0), {.time = time, .library_get = get}))
if (draw.type == DrawType::LAYER)
points.push_back(glm::round(glm::vec2(draw.parent * draw_quad_model_get(draw) * glm::vec4(1, 1, 0, 1))));
std::ranges::sort(points, [](glm::vec2 a, glm::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::model_to_string(anm2, {.gameModel = &flat});
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, &model::Layer::is_animation) == 3);
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 = edit::animation_layers_flatten(anm2, library_get,
[](const model::Model&, const std::filesystem::path& path) { return path; });
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(model::item_get(flat.content.events, triggers.front().eventIds.front())->name == "spun");
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 = model::layer_child_transform_get(*anm2.animation_get(0), 0, 0.0f, true);
auto placement = layer_child_transform_get(*anm2.animation_get(0), 0, 0.0f, true);
REQUIRE(placement);
CHECK(glm::vec2(*placement * glm::vec4(16.0f, 0.0f, 0.0f, 1.0f)) == glm::vec2(48.0f, 0.0f));
CHECK_FALSE(model::layer_child_transform_get(*anm2.animation_get(0), 99, 0.0f, true));
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")
@@ -220,30 +217,30 @@ TEST_CASE("the game's copy stands in for depth and alpha offsets")
{ "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 = edit::isaac_bridge(anm2);
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& model, float time)
auto draws_get = [](const Model& model, float time)
{
std::vector<std::pair<glm::vec2, glm::vec2>> draws{};
for (const auto& draw : model::animation_draws_get(model, *model.animation_get(0), {.time = time}))
if (draw.type == model::DrawType::LAYER && draw.frame.isVisible)
for (const 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(model::isaac_issues_get(game).depthFrames == 0);
CHECK(isaac_issues_get(game).depthFrames == 0);
CHECK(
std::ranges::any_of(game.content.layers, [](const model::Layer& layer) { return layer.name == "a (depth +2)"; }));
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(util::math::float_to_uint8(offsetFrame.tint.a) == 255 - 64);
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::model_to_string(anm2)).info.isIsaacIgnored);
CHECK(model_load(model::model_to_json(anm2)).info.isIsaacIgnored);
CHECK(model_load(model_to_string(anm2)).info.isIsaacIgnored);
CHECK(model_load(model_to_json(anm2)).info.isIsaacIgnored);
}
+21 -24
View File
@@ -1,8 +1,5 @@
#include "common.h"
using namespace anm2ed;
using namespace anm2ed::test;
TEST_CASE("per-fixture invariants")
{
for (const auto& fixture : fixtures_get())
@@ -16,11 +13,11 @@ TEST_CASE("saved text escapes what it holds, and reads back the same")
{
auto model = model_load(file_load(CORPUS_DIR / "11_unknown.anm2"));
model.extras.children.emplace_back(
0, model::XmlNode{.tag = "Note",
0, XmlNode{.tag = "Note",
.attributes = {{"Quote", "it's \"x\" & <y>"}},
.text = "a & b > c 'd' \"e\"",
.children = {{.tag = "Inner", .children = {{.tag = "Leaf", .attributes = {{"A", "1"}}}}}}});
auto text = model::model_to_string(model);
auto text = model_to_string(model);
CHECK(text.find("Quote=\"it&apos;s &quot;x&quot; &amp; &lt;y&gt;\"") != std::string::npos);
CHECK(text.find(">a &amp; b &gt; c 'd' \"e\"<") != std::string::npos);
auto reloaded = model_load(text).extras.children;
@@ -35,10 +32,10 @@ TEST_CASE("saves leave out default colors, which read back the same")
{
INFO("fixture: ", fixture.string());
auto model = model_load(file_load(fixture));
auto text = model::model_to_string(model);
auto text = model_to_string(model);
CHECK(text.find("Tint=\"255\"") == std::string::npos);
CHECK(text.find("Offset=\"0\"") == std::string::npos);
CHECK(model::model_hash(model_load(text)) == model::model_hash(model));
CHECK(model_hash(model_load(text)) == model_hash(model));
}
}
@@ -48,14 +45,14 @@ TEST_CASE("JSON documents read back as the editor's copy")
{
INFO("fixture: ", fixture.string());
auto model = model_load(file_load(fixture));
auto text = model::model_to_json(model);
auto text = model_to_json(model);
auto reloaded = model_load(text);
CHECK(model::model_to_json(reloaded) == text);
CHECK(model_to_json(reloaded) == text);
// Unknown XML, element order, dangling references and repeated animation names are not kept.
auto isQuirky = fixture.filename() == "09_legacy_quirks.anm2" || fixture.filename() == "11_unknown.anm2";
if (!isQuirky)
CHECK(model::model_serialize(reloaded, SERIALIZE_ANM2ED_DEFAULT) ==
model::model_serialize(model, SERIALIZE_ANM2ED_DEFAULT));
CHECK(model_serialize(reloaded, SERIALIZE_ANM2ED_DEFAULT) ==
model_serialize(model, SERIALIZE_ANM2ED_DEFAULT));
}
}
@@ -63,7 +60,7 @@ TEST_CASE("JSON animations get unique names")
{
auto model = model_load(file_load(CORPUS_DIR / "01_minimal.anm2"));
model.animations.entries.push_back(model.animations.entries.front());
auto reloaded = model_load(model::model_to_json(model));
auto reloaded = model_load(model_to_json(model));
REQUIRE(reloaded.animations_count_get() == 2);
CHECK(reloaded.animation_get(1)->name == reloaded.animation_get(0)->name + " 2");
}
@@ -71,9 +68,9 @@ TEST_CASE("JSON animations get unique names")
TEST_CASE("JSON groups don't renumber an animation sharing their name")
{
auto model = model_load(file_load(CORPUS_DIR / "01_minimal.anm2"));
auto animation = std::get<std::shared_ptr<const model::Animation>>(model.animations.entries.front());
model.animations.entries = {model::AnimationGroup{.name = animation->name, .animations = {animation}}};
auto reloaded = model_load(model::model_to_json(model));
auto animation = std::get<std::shared_ptr<const Animation>>(model.animations.entries.front());
model.animations.entries = {AnimationGroup{.name = animation->name, .animations = {animation}}};
auto reloaded = model_load(model_to_json(model));
REQUIRE(reloaded.animations_count_get() == 1);
CHECK(reloaded.animation_get(0)->name == animation->name);
}
@@ -81,7 +78,7 @@ TEST_CASE("JSON groups don't renumber an animation sharing their name")
TEST_CASE("JSON markers are a pair, and a frame's child an object only when it has one")
{
auto model = model_load(file_load(CORPUS_DIR / "01_minimal.anm2"));
auto text = model::model_to_json(model);
auto text = model_to_json(model);
CHECK(text.find("\"child\"") == std::string::npos);
model.animation_edit(0)->startMarker = 2;
model.animation_edit(0)->endMarker = 5;
@@ -89,12 +86,12 @@ TEST_CASE("JSON markers are a pair, and a frame's child an object only when it h
REQUIRE(frame);
frame->childMode = ChildMode::HOLD;
frame->childStart = 3;
text = model::model_to_json(model);
text = model_to_json(model);
CHECK(text.find("\"markers\": [2, 5]") != std::string::npos);
CHECK(text.find("\"child\": {\"mode\": \"hold\", \"start\": 3}") != std::string::npos);
auto reloaded = model_load(text);
CHECK(reloaded.animation_get(0)->endMarker == 5);
CHECK(model::model_to_json(reloaded) == text);
CHECK(model_to_json(reloaded) == text);
}
TEST_CASE("JSON dates are ISO 8601")
@@ -105,7 +102,7 @@ TEST_CASE("JSON dates are ISO 8601")
{"12/31/2024 11:59:59 PM", "2024-12-31T23:59:59"}})
{
model.info.createdOn = date;
auto text = model::model_to_json(model);
auto text = model_to_json(model);
CHECK(text.find(iso) != std::string::npos);
CHECK(model_load(text).info.createdOn == date);
}
@@ -116,9 +113,9 @@ TEST_CASE("JSON that can't be read is reported by where it is, and the rest stil
auto text = R"({ "colour": 1, "info": { "fps": "fast" },
"layers": [ { "name": "a", "sheet": 4 } ],
"animations": { "Idle": { "length": 3, "root": [ { "pos": [1], "ease": "bouncy", "delay": 3 } ] } } })";
model::Model model{};
Model model{};
std::string issues{};
REQUIRE(model::model_load_string(model, text, &issues));
REQUIRE(model_load_string(model, text, &issues));
CHECK(model.animation_get(0)->frameNum == 3);
CHECK(model.animation_get(0)->root.frames[0].duration == 3);
for (auto issue : {"colour: unknown key", "info.fps: expected a number", "layers[0].sheet: no item at index 4",
@@ -126,16 +123,16 @@ TEST_CASE("JSON that can't be read is reported by where it is, and the rest stil
"animations.Idle.root[0].ease: unknown value \"bouncy\""})
CHECK_MESSAGE(issues.find(issue) != std::string::npos, issue);
CHECK_FALSE(model::model_load_string(model, "{ \"fps\": 30, ", &issues));
CHECK_FALSE(model_load_string(model, "{ \"fps\": 30, ", &issues));
}
TEST_CASE("JSON layers can name a library listed after them in the file")
{
auto text = R"({ "layers": [ { "name": "shadow", "library": 0, "animation": "Small" } ],
"libraries": [ "shared/shadows.anm2" ] })";
model::Model model{};
Model model{};
std::string issues{};
REQUIRE(model::model_load_string(model, text, &issues));
REQUIRE(model_load_string(model, text, &issues));
CHECK(issues.empty());
CHECK(model.content.layers[0].libraryId == 0);
}
+117 -120
View File
@@ -4,29 +4,26 @@
#include "playback.h"
#include "pack.h"
using namespace anm2ed;
using namespace anm2ed::test;
static Model fixture_load(const char* name) { return model_load(file_load(CORPUS_DIR / name)); }
model::Model fixture_load(const char* name) { return model_load(file_load(CORPUS_DIR / name)); }
void operation_check(const char* name, const model::Model& model)
static void operation_check(const char* name, const Model& model)
{
golden_check(GOLDEN_DIR / std::format("operation.{}.xml", name),
model::model_serialize(model, SERIALIZE_ANM2ED_DEFAULT));
model_serialize(model, SERIALIZE_ANM2ED_DEFAULT));
}
model::Track& layer_track_get(model::Model& model, int animationIndex, int layerId)
static Track& layer_track_get(Model& model, int animationIndex, int layerId)
{
auto track = model.track_edit({animationIndex, LAYER, layerId});
REQUIRE(track);
return *track;
}
int groups_named_count(const model::Animation& animation, std::string_view name)
static int groups_named_count(const Animation& animation, std::string_view name)
{
int count{};
for (const auto* entries : {&animation.layers, &animation.nulls})
model::groups_each(*entries, [&](const model::TrackGroup& group) { count += group.name == name; });
groups_each(*entries, [&](const TrackGroup& group) { count += group.name == name; });
return count;
}
@@ -36,7 +33,7 @@ TEST_CASE("animations_merge keeps one group per name")
{
INFO("merge: ", std::string(name));
auto model = fixture_load("03a_groups_nested.anm2");
edit::animations_merge(model, 0, {0, 1}, type, false, {});
animations_merge(model, 0, {0, 1}, type, false, {});
CHECK(groups_named_count(*model.animation_get(0), "Main") == 1);
}
}
@@ -50,7 +47,7 @@ TEST_CASE("file_merge presets")
for (auto [name, preset] : PRESETS_FILE_MERGE)
{
auto model = fixture_load("02_items.anm2");
CHECK(edit::file_merge(model, CORPUS_DIR / "05_regions.anm2", CORPUS_DIR, preset));
CHECK(file_merge(model, CORPUS_DIR / "05_regions.anm2", CORPUS_DIR, preset));
operation_check(name, model);
}
}
@@ -58,23 +55,23 @@ TEST_CASE("file_merge presets")
TEST_CASE("item_add")
{
auto model = fixture_load("02_items.anm2");
CHECK(edit::item_add(model, ItemType::LAYER, 0, -1, {.name = "added", .spritesheetId = 1}, {}, 1,
types::destination::ALL) == 2);
CHECK(edit::item_add(model, ItemType::NULL_, 1, -1, {}, {.name = "thisOnly"}, -1, types::destination::THIS) == 2);
CHECK(item_add(model, ItemType::LAYER, 0, -1, {.name = "added", .spritesheetId = 1}, {}, 1,
DESTINATION_ALL) == 2);
CHECK(item_add(model, ItemType::NULL_, 1, -1, {}, {.name = "thisOnly"}, -1, DESTINATION_THIS) == 2);
operation_check("item_add", model);
}
TEST_CASE("regions_generate and regions_scan")
{
auto model = fixture_load("02_items.anm2");
edit::regions_generate(model, {0}, {}, "Region {}", RegionFrameMapping::SET);
regions_generate(model, {0}, {}, "Region {}", RegionFrameMapping::SET);
auto generated = model;
edit::regions_generate(model, {0}, {}, "Region {}", RegionFrameMapping::SET);
CHECK(model::is_model_equal(model, generated));
regions_generate(model, {0}, {}, "Region {}", RegionFrameMapping::SET);
CHECK(is_model_equal(model, generated));
operation_check("regions_generate", model);
auto scanned = fixture_load("05_regions.anm2");
edit::regions_scan(scanned);
regions_scan(scanned);
operation_check("regions_scan", scanned);
}
@@ -92,7 +89,7 @@ TEST_CASE("frames_change")
change.duration = 3;
change.interpolation = Interpolation::EASE_IN;
change.isFlipX = true;
model::frames_change(layer_track_get(model, 0, 0), change, ItemType::LAYER, type, {0, 1});
frames_change(layer_track_get(model, 0, 0), change, ItemType::LAYER, type, {0, 1});
operation_check(std::format("frames_change_{}", name).c_str(), model);
}
}
@@ -100,17 +97,17 @@ TEST_CASE("frames_change")
TEST_CASE("frame_bake and frame_mix")
{
auto model = fixture_load("07_special_interpolation.anm2");
model::frame_bake(layer_track_get(model, 0, 0), 0, 2, true, false);
model::frame_bake(layer_track_get(model, 0, 0), 3, 1, false, true);
frame_bake(layer_track_get(model, 0, 0), 0, 2, true, false);
frame_bake(layer_track_get(model, 0, 0), 3, 1, false, true);
operation_check("frame_bake", model);
model::Frame from{};
model::Frame to{};
Frame from{};
Frame to{};
to.position = {10.0f, -20.0f};
to.skew = {30.0f, 0.0f};
to.rotation = 90.0f;
to.tint = {0.0f, 1.0f, 1.0f, 0.5f};
model::frame_mix(from, to, 0.5f);
frame_mix(from, to, 0.5f);
CHECK(from.position == glm::vec2(5.0f, -10.0f));
CHECK(from.skew == glm::vec2(15.0f, 0.0f));
CHECK(from.rotation == doctest::Approx(45.0f));
@@ -122,7 +119,7 @@ TEST_CASE("rects_pack fits every rect without overlap")
std::vector<glm::ivec2> sizes{{16, 16}, {32, 8}, {8, 32}, {5, 7}, {64, 1}, {1, 1}};
glm::ivec2 size{};
std::vector<glm::ivec2> positions{};
REQUIRE(util::pack::rects_pack(sizes, size, positions));
REQUIRE(rects_pack(sizes, size, positions));
REQUIRE(positions.size() == sizes.size());
for (std::size_t i = 0; i < sizes.size(); ++i)
{
@@ -144,90 +141,90 @@ TEST_CASE("edit layer operations")
{
const char* name;
const char* fixture;
std::function<edit::Uids(model::Model&)> operation;
std::function<Uids(Model&)> operation;
std::size_t uidCount;
};
const Reference head{0, LAYER, 0};
const Case CASES[] = {
{"frame_insert", "02_items.anm2",
[&](model::Model& anm2) { return edit::frame_insert(anm2, {0, LAYER, 0, 0}, 0); }, 1},
[&](Model& anm2) { return frame_insert(anm2, {0, LAYER, 0, 0}, 0); }, 1},
{"trigger_insert", "02_items.anm2",
[&](model::Model& anm2) { return edit::frame_insert(anm2, {0, TRIGGER, -1, 0}, 2); }, 1},
[&](Model& anm2) { return frame_insert(anm2, {0, TRIGGER, -1, 0}, 2); }, 1},
{"frames_delete", "02_items.anm2",
[&](model::Model& anm2) { return edit::frames_delete(anm2, {{0, LAYER, 0, 0}, {0, ROOT, -1, 1}}); }, 0},
{"frames_duplicate", "02_items.anm2", [&](model::Model& anm2)
{ return edit::frames_duplicate(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, {0, LAYER, 0, 1}); }, 2},
[&](Model& anm2) { return frames_delete(anm2, {{0, LAYER, 0, 0}, {0, ROOT, -1, 1}}); }, 0},
{"frames_duplicate", "02_items.anm2", [&](Model& anm2)
{ return frames_duplicate(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, {0, LAYER, 0, 1}); }, 2},
{"frames_reverse", "07_special_interpolation.anm2",
[&](model::Model& anm2) { return edit::frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 2}}); }, 0},
{"frames_bake", "07_special_interpolation.anm2", [&](model::Model& anm2)
{ return edit::frames_bake(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, 2, true, false); }, 5},
{"frame_split", "02_items.anm2", [&](model::Model& anm2) { return edit::frame_split(anm2, {0, LAYER, 1, 0}, 2); },
[&](Model& anm2) { return frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 2}}); }, 0},
{"frames_bake", "07_special_interpolation.anm2", [&](Model& anm2)
{ return frames_bake(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}, 2, true, false); }, 5},
{"frame_split", "02_items.anm2", [&](Model& anm2) { return frame_split(anm2, {0, LAYER, 1, 0}, 2); },
1},
{"frames_move", "02_items.anm2",
[&](model::Model& anm2) { return edit::frames_move(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 1, 0}}, head, 2); }, 2},
[&](Model& anm2) { return frames_move(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 1, 0}}, head, 2); }, 2},
{"frames_paste", "02_items.anm2",
[&](model::Model& anm2)
[&](Model& anm2)
{
auto frame = anm2.frame_get(Reference{0, LAYER, 1, 0});
auto text = model::frame_to_string(*frame, ItemType::LAYER) + model::frame_to_string(*frame, ItemType::LAYER);
return edit::frames_paste(anm2, {0, LAYER, 0, 0}, {}, text, 0, nullptr);
auto text = frame_to_string(*frame, ItemType::LAYER) + frame_to_string(*frame, ItemType::LAYER);
return frames_paste(anm2, {0, LAYER, 0, 0}, {}, text, 0, nullptr);
},
2},
{"root_bake_into", "04_group_root_transform.anm2",
[&](model::Model& anm2)
[&](Model& anm2)
{
return edit::root_bake_into(anm2, {{0, ROOT, -1, 0}}, {{0, LAYER, 2}},
return root_bake_into(anm2, {{0, ROOT, -1, 0}}, {{0, LAYER, 2}},
{.isRoundScale = true, .isMatchRootInterpolation = true});
},
1},
{"items_group", "03b_groups_legacy_flat.anm2",
[&](model::Model& anm2) { return edit::items_group(anm2, 0, LAYER, {2}, "New"); }, 1},
{"items_move", "03b_groups_legacy_flat.anm2", [&](model::Model& anm2)
{ return edit::items_move(anm2, 0, LAYER, {2}, {}, {true, LAYER, 0, -1}, false, true); }, 0},
[&](Model& anm2) { return items_group(anm2, 0, LAYER, {2}, "New"); }, 1},
{"items_move", "03b_groups_legacy_flat.anm2", [&](Model& anm2)
{ return items_move(anm2, 0, LAYER, {2}, {}, {true, LAYER, 0, -1}, false, true); }, 0},
{"items_remove", "03b_groups_legacy_flat.anm2",
[&](model::Model& anm2) { return edit::items_remove(anm2, 0, {{LAYER, {2}}}, {{NULL_, {3}}}); }, 0},
[&](Model& anm2) { return items_remove(anm2, 0, {{LAYER, {2}}}, {{NULL_, {3}}}); }, 0},
{"animation_add", "03a_groups_nested.anm2",
[&](model::Model& anm2) { return edit::animation_add(anm2, 1, 0, 0, "Added"); }, 1},
[&](Model& anm2) { return animation_add(anm2, 1, 0, 0, "Added"); }, 1},
{"animations_remove", "03a_groups_nested.anm2",
[&](model::Model& anm2) { return edit::animations_remove(anm2, {1}, {0}); }, 0},
{"animations_move", "02_items.anm2", [&](model::Model& anm2)
{ return edit::animations_move(anm2, {0}, {}, {.animationIndex = 1, .isAfter = true}); }, 1},
[&](Model& anm2) { return animations_remove(anm2, {1}, {0}); }, 0},
{"animations_move", "02_items.anm2", [&](Model& anm2)
{ return animations_move(anm2, {0}, {}, {.animationIndex = 1, .isAfter = true}); }, 1},
{"animations_group", "02_items.anm2",
[&](model::Model& anm2) { return edit::animations_group(anm2, {0, 1}, "Group"); }, 0},
[&](Model& anm2) { return animations_group(anm2, {0, 1}, "Group"); }, 0},
{"animations_paste", "02_items.anm2",
[&](model::Model& anm2)
[&](Model& anm2)
{
std::set<int> groupIds{};
auto text = model::animation_to_string(*anm2.animation_get(1));
return edit::animations_paste(anm2, text, 0, -1, groupIds, nullptr);
auto text = animation_to_string(*anm2.animation_get(1));
return animations_paste(anm2, text, 0, -1, groupIds, nullptr);
},
1},
{"regions_paste", "05_regions.anm2",
[&](model::Model& anm2)
[&](Model& anm2)
{
auto region = model::item_get(model::item_get(anm2.content.spritesheets, 0)->regions, 1);
return edit::regions_paste(anm2, 0, model::item_to_string(*region), 1, nullptr);
auto region = item_get(item_get(anm2.content.spritesheets, 0)->regions, 1);
return regions_paste(anm2, 0, item_to_string(*region), 1, nullptr);
},
0},
{"animation_grid_generate", "05_regions.anm2",
[&](model::Model& anm2)
[&](Model& anm2)
{
return edit::animation_grid_generate(anm2, {{0, LAYER, 0}},
return animation_grid_generate(anm2, {{0, LAYER, 0}},
{{0, 0}, {8, 8}, {4, 4}, 2, 3, 2, true, "Grid {}"});
},
3},
{"frames_change_apply", "02_items.anm2",
[&](model::Model& anm2)
[&](Model& anm2)
{
FrameChange change{.positionX = 3.0f, .rotation = 10.0f};
return edit::frames_change_apply(
return frames_change_apply(
anm2, {.references = {{0, LAYER, 0, 1}, {0, NULL_, 0}}, .animations = {1}, .isRoot = true}, change,
ChangeType::ADD);
},
0},
{"animations_merge", "03a_groups_nested.anm2",
[&](model::Model& anm2) { return edit::animations_merge(anm2, 0, {1}, types::merge::APPEND, true, {0}); }, 1},
[&](Model& anm2) { return animations_merge(anm2, 0, {1}, MERGE_APPEND, true, {0}); }, 1},
};
for (const auto& entry : CASES)
@@ -237,7 +234,7 @@ TEST_CASE("edit layer operations")
auto uids = entry.operation(anm2);
CHECK(uids.size() == entry.uidCount);
anm2.uids_repair();
model::UidIndex index(anm2);
UidIndex index(anm2);
for (auto uid : uids)
CHECK(index.reference_get(uid));
operation_check(entry.name, anm2);
@@ -248,11 +245,11 @@ TEST_CASE("spritesheet pixel edits")
{
auto image_make = [](glm::ivec2 size, std::vector<glm::ivec2> opaque = {})
{
std::vector<uint8_t> pixels((std::size_t)size.x * size.y * resource::image::CHANNELS, 0);
std::vector<uint8_t> pixels((std::size_t)size.x * size.y * IMAGE_CHANNELS, 0);
for (auto point : opaque)
std::fill_n(pixels.begin() + ((std::size_t)point.y * size.x + point.x) * resource::image::CHANNELS,
resource::image::CHANNELS, 255);
return resource::Image(pixels.data(), size);
std::fill_n(pixels.begin() + ((std::size_t)point.y * size.x + point.x) * IMAGE_CHANNELS,
IMAGE_CHANNELS, 255);
return Image(pixels.data(), size);
};
auto sheet = image_make({64, 64}, {{4, 6}});
auto other = image_make({16, 16});
@@ -260,8 +257,8 @@ TEST_CASE("spritesheet pixel edits")
SUBCASE("regions_trim shrinks to content and keeps the custom pivot in place")
{
auto model = fixture_load("05_regions.anm2");
CHECK(edit::regions_trim(model, 0, {0, 1}, sheet));
auto region = model::item_get(model.content.spritesheets[0].regions, 0);
CHECK(regions_trim(model, 0, {0, 1}, sheet));
auto region = item_get(model.content.spritesheets[0].regions, 0);
CHECK(region->crop == glm::vec2(4, 6));
CHECK(region->size == glm::vec2(1, 1));
CHECK(region->pivot == glm::vec2(-1, -1));
@@ -270,7 +267,7 @@ TEST_CASE("spritesheet pixel edits")
SUBCASE("spritesheet_pack returns an image holding every region")
{
auto model = fixture_load("05_regions.anm2");
auto packed = edit::spritesheet_pack(model, 0, sheet, 1);
auto packed = spritesheet_pack(model, 0, sheet, 1);
REQUIRE(packed);
CHECK(packed->is_valid());
for (auto& region : model.content.spritesheets[0].regions)
@@ -281,11 +278,11 @@ TEST_CASE("spritesheet pixel edits")
{
auto model = fixture_load("05_regions.anm2");
auto image_get = [&](int id) { return id == 0 ? &sheet : &other; };
auto merged = edit::spritesheets_merge(model, {0, 1}, image_get, true, true, false, Origin::TOP_LEFT);
auto merged = spritesheets_merge(model, {0, 1}, image_get, true, true, false, Origin::TOP_LEFT);
REQUIRE(merged);
CHECK(merged->size == glm::ivec2(80, 64));
CHECK(model.content.spritesheets.size() == 1);
CHECK(model::item_get(model.content.layers, 1)->spritesheetId == 0);
CHECK(item_get(model.content.layers, 1)->spritesheetId == 0);
operation_check("spritesheets_merge", model);
}
}
@@ -298,9 +295,9 @@ TEST_CASE("markers stay in the editor's copy and bound playback")
animation->frameNum = 10;
animation->startMarker = 2;
animation->endMarker = 5;
CHECK(model::animation_play_range_get(*animation) == glm::ivec2(2, 6));
CHECK(animation_play_range_get(*animation) == glm::ivec2(2, 6));
auto text = model::model_to_string(model);
auto text = model_to_string(model);
CHECK(game_document_get(text).find("Marker") == std::string::npos);
auto reloaded = model_load(text);
CHECK(reloaded.animation_get(0)->startMarker == 2);
@@ -316,36 +313,36 @@ TEST_CASE("markers stay in the editor's copy and bound playback")
TEST_CASE("isaac_issues_get counts what the game's copy changes")
{
CHECK(model::isaac_issues_get(fixture_load("01_minimal.anm2")) == model::IsaacIssues{});
CHECK(model::isaac_issues_get(fixture_load("07_special_interpolation.anm2")).easedFrames == 5);
CHECK(model::isaac_issues_get(fixture_load("04_group_root_transform.anm2")).groupTransforms > 0);
auto shaders = model::isaac_issues_get(fixture_load("06_shaders.anm2"));
CHECK(isaac_issues_get(fixture_load("01_minimal.anm2")) == IsaacIssues{});
CHECK(isaac_issues_get(fixture_load("07_special_interpolation.anm2")).easedFrames == 5);
CHECK(isaac_issues_get(fixture_load("04_group_root_transform.anm2")).groupTransforms > 0);
auto shaders = isaac_issues_get(fixture_load("06_shaders.anm2"));
CHECK(shaders.shaderFrames > 0);
CHECK(shaders.skewFrames > 0);
CHECK(model::isaac_issues_get(fixture_load("02_items.anm2")).triggerSounds > 0);
CHECK(isaac_issues_get(fixture_load("02_items.anm2")).triggerSounds > 0);
}
TEST_CASE("frames_property_set copies one property to the frames that have it")
{
auto model = fixture_load("02_items.anm2");
model::Frame source{.crop = {7, 8}, .position = {3, 4}};
Frame source{.crop = {7, 8}, .position = {3, 4}};
Reference layer{0, LAYER, 0, 0};
Reference null{0, NULL_, 0, 0};
REQUIRE(model.frame_get(layer));
REQUIRE(model.frame_get(null));
auto nullCrop = model.frame_get(null)->crop;
edit::frames_property_set(model, {layer, null}, edit::FrameProperty::POSITION, source);
frames_property_set(model, {layer, null}, FrameProperty::POSITION, source);
CHECK(model.frame_get(layer)->position == glm::vec2(3, 4));
CHECK(model.frame_get(null)->position == glm::vec2(3, 4));
edit::frames_property_set(model, {layer, null}, edit::FrameProperty::CROP, source);
frames_property_set(model, {layer, null}, FrameProperty::CROP, source);
CHECK(model.frame_get(layer)->crop == glm::vec2(7, 8));
CHECK(model.frame_get(null)->crop == nullCrop);
CHECK_FALSE(edit::is_frame_property_valid(edit::FrameProperty::EVENT, ItemType::LAYER));
CHECK(edit::is_frame_property_valid(edit::FrameProperty::SOUNDS, ItemType::TRIGGER));
CHECK_FALSE(edit::is_frame_property_valid(edit::FrameProperty::POSITION, ItemType::TRIGGER));
CHECK_FALSE(is_frame_property_valid(FrameProperty::EVENT, ItemType::LAYER));
CHECK(is_frame_property_valid(FrameProperty::SOUNDS, ItemType::TRIGGER));
CHECK_FALSE(is_frame_property_valid(FrameProperty::POSITION, ItemType::TRIGGER));
}
TEST_CASE("frames_split splits every selected frame that spans the time")
@@ -360,7 +357,7 @@ TEST_CASE("frames_split splits every selected frame that spans the time")
REQUIRE(layerTrack->frames[0].duration > 1);
REQUIRE(nullTrack->frames[0].duration > 1);
auto uids = edit::frames_split(model, {{0, LAYER, 0, 0}, {0, NULL_, 0, 0}}, 0);
auto uids = frames_split(model, {{0, LAYER, 0, 0}, {0, NULL_, 0, 0}}, 0);
CHECK(uids.size() == 2);
CHECK(model.track_get({0, LAYER, 0})->frames.size() == layerCount + 1);
CHECK(model.track_get({0, NULL_, 0})->frames.size() == nullCount + 1);
@@ -372,8 +369,8 @@ TEST_CASE("change all frame properties changes layer depth only")
auto model = fixture_load("02_items.anm2");
Reference layer{0, LAYER, 0, 0};
Reference null{0, NULL_, 0, 0};
edit::frames_change_apply(model, {.references = {layer, null}}, {.depth = 2}, ChangeType::ADD);
edit::frames_change_apply(model, {.references = {layer, null}}, {.depth = 1}, ChangeType::SUBTRACT);
frames_change_apply(model, {.references = {layer, null}}, {.depth = 2}, ChangeType::ADD);
frames_change_apply(model, {.references = {layer, null}}, {.depth = 1}, ChangeType::SUBTRACT);
CHECK(model.frame_get(layer)->depth == 1);
CHECK(model.frame_get(null)->depth == 0);
}
@@ -384,7 +381,7 @@ TEST_CASE("change all frame properties blurs layers and changes only animation l
Reference animationLayer{0, LAYER, 1, 0};
Reference spritesheetLayer{1, LAYER, 2, 0};
REQUIRE(model.frame_get(spritesheetLayer));
edit::frames_change_apply(model, {.references = {animationLayer, spritesheetLayer}},
frames_change_apply(model, {.references = {animationLayer, spritesheetLayer}},
{.blur = 8.0f, .childAnimation = "Spin", .childMode = ChildMode::ONCE, .childStart = 3},
ChangeType::ADJUST);
auto child = *model.frame_get(animationLayer);
@@ -398,7 +395,7 @@ TEST_CASE("change all frame properties blurs layers and changes only animation l
CHECK(sprite.childMode == ChildMode::LOOP);
CHECK(sprite.childStart == 0);
edit::frames_change_apply(model, {.references = {animationLayer}}, {.blur = 1000.0f}, ChangeType::ADD);
frames_change_apply(model, {.references = {animationLayer}}, {.blur = 1000.0f}, ChangeType::ADD);
CHECK(model.frame_get(animationLayer)->blur == FRAME_BLUR_MAX);
}
@@ -410,26 +407,26 @@ TEST_CASE("animations_merge moves triggers past the animation placed before them
auto first = *model.animation_get(0);
first.frameNum = 10;
auto second = first;
second.uid = util::uid_next();
second.triggers.frames = {model::Frame{.atFrame = 5}};
model.animations.entries = {std::make_shared<const model::Animation>(first),
std::make_shared<const model::Animation>(second)};
second.uid = uid_next();
second.triggers.frames = {Frame{.atFrame = 5}};
model.animations.entries = {std::make_shared<const Animation>(first),
std::make_shared<const Animation>(second)};
return model;
};
auto appended = model_make();
edit::animations_merge(appended, 0, {0, 1}, types::merge::APPEND, false, {});
animations_merge(appended, 0, {0, 1}, MERGE_APPEND, false, {});
CHECK(appended.animation_get(0)->triggers.frames.at(0).atFrame == 15);
auto prepended = model_make();
edit::animations_merge(prepended, 1, {0, 1}, types::merge::PREPEND, false, {});
animations_merge(prepended, 1, {0, 1}, MERGE_PREPEND, false, {});
CHECK(prepended.animation_get(1)->triggers.frames.at(0).atFrame == 15);
}
TEST_CASE("skew reads from the old Shear attributes too")
{
auto frames =
model::frames_from_string(R"(<Frame ShearX="5" ShearY="6"/><Frame SkewX="7" ShearX="9"/>)", ItemType::NULL_);
frames_from_string(R"(<Frame ShearX="5" ShearY="6"/><Frame SkewX="7" ShearX="9"/>)", ItemType::NULL_);
REQUIRE(frames.size() == 2);
CHECK(frames[0].skew == glm::vec2(5, 6));
CHECK(frames[1].skew.x == 7);
@@ -440,38 +437,38 @@ TEST_CASE("a trigger fires several events; the game's copy keeps the first")
{
auto model = fixture_load("02_items.anm2");
model.frame_edit({0, TRIGGER, -1, 0})->eventIds = {1, 0};
auto text = model::model_to_string(model);
auto text = model_to_string(model);
CHECK(model_load(text).frame_get({0, TRIGGER, -1, 0})->eventIds == std::vector{1, 0});
CHECK(model_load(game_document_get(text)).frame_get({0, TRIGGER, -1, 0})->eventIds == std::vector{1});
CHECK(model::isaac_issues_get(model).extraEvents == 1);
CHECK(isaac_issues_get(model).extraEvents == 1);
}
TEST_CASE("SVG spritesheets pack and merge into SVGs")
{
auto model = model_load(file_load(TEST_DIR / "render" / "vector.anm2"));
resource::Image vector(TEST_DIR / "render" / "vector.svg");
Image vector(TEST_DIR / "render" / "vector.svg");
auto& regions = model.content.spritesheets.front().regions;
regions.push_back({.id = 0, .name = "circle", .crop = {0, 0}, .size = {16, 16}});
regions.push_back({.id = 1, .name = "square", .crop = {16, 0}, .size = {16, 16}});
// Packing keeps the drawing: each region shows the same pixels where it now sits.
auto packed = edit::spritesheet_pack(model, 0, vector, 1);
auto packed = spritesheet_pack(model, 0, vector, 1);
REQUIRE(packed);
CHECK(packed->is_vector());
for (auto [id, original] : {std::pair{0, glm::vec2(8, 8)}, std::pair{1, glm::vec2(24, 8)}})
{
auto region = model::item_get(model.content.spritesheets.front().regions, id);
auto region = item_get(model.content.spritesheets.front().regions, id);
CHECK(packed->pixel_read(region->crop + glm::vec2(8, 8)) == vector.pixel_read(original));
}
// A PNG sheet merged after an SVG one: the result is an SVG holding the PNG; a PNG first takes the .svg name.
const std::vector<uint8_t> pixels(4 * 4 * resource::image::CHANNELS, 255);
resource::Image raster(pixels.data(), {4, 4});
const std::vector<uint8_t> pixels(4 * 4 * IMAGE_CHANNELS, 255);
Image raster(pixels.data(), {4, 4});
model.content.spritesheets.push_back({.id = 1, .path = "white.png"});
auto images = std::map<int, const resource::Image*>{{0, &vector}, {1, &raster}};
auto images = std::map<int, const Image*>{{0, &vector}, {1, &raster}};
auto image_get = [&](int id) { return images.at(id); };
auto mixed = model;
auto merged = edit::spritesheets_merge(mixed, {0, 1}, image_get, true, false, false, Origin::TOP_LEFT);
auto merged = spritesheets_merge(mixed, {0, 1}, image_get, true, false, false, Origin::TOP_LEFT);
REQUIRE(merged);
CHECK(merged->is_vector());
CHECK(merged->size == glm::ivec2(36, 16));
@@ -480,21 +477,21 @@ TEST_CASE("SVG spritesheets pack and merge into SVGs")
std::swap(model.content.spritesheets.front().id, model.content.spritesheets.back().id);
images = {{1, &vector}, {0, &raster}};
REQUIRE(edit::spritesheets_merge(model, {0, 1}, image_get, true, false, false, Origin::TOP_LEFT));
CHECK(model::item_get(model.content.spritesheets, 0)->path == "white.svg");
REQUIRE(spritesheets_merge(model, {0, 1}, image_get, true, false, false, Origin::TOP_LEFT));
CHECK(item_get(model.content.spritesheets, 0)->path == "white.svg");
}
TEST_CASE("content_remove takes what refers to an item with it")
{
auto path_keep = [](const model::Model&, const std::filesystem::path& path) { return path; };
auto remove = [&](model::Model& model, ElementType type, std::set<int> ids, int spritesheetId = -1,
const model::LibraryGet& library_get = {})
{ edit::content_remove(model, type, ids, spritesheetId, library_get, path_keep); };
auto tracks_count = [](const model::Model& model, int layerId)
auto path_keep = [](const Model&, const std::filesystem::path& path) { return path; };
auto remove = [&](Model& model, ElementType type, std::set<int> ids, int spritesheetId = -1,
const LibraryGet& library_get = {})
{ content_remove(model, type, ids, spritesheetId, library_get, path_keep); };
auto tracks_count = [](const Model& model, int layerId)
{
int count{};
for (auto [index, animation] : model.animations_get())
model::tracks_each(animation->layers, [&](const model::Track& track, auto*) { count += track.id == layerId; });
tracks_each(animation->layers, [&](const Track& track, auto*) { count += track.id == layerId; });
return count;
};
@@ -503,7 +500,7 @@ TEST_CASE("content_remove takes what refers to an item with it")
auto model = fixture_load("02_items.anm2");
CHECK(model.uses_count(ElementType::LAYER_ELEMENT, {0}) == tracks_count(model, 0));
remove(model, ElementType::LAYER_ELEMENT, {0});
CHECK_FALSE(model::item_get(model.content.layers, 0));
CHECK_FALSE(item_get(model.content.layers, 0));
CHECK(tracks_count(model, 0) == 0);
CHECK(tracks_count(model, 1) > 0);
@@ -546,7 +543,7 @@ TEST_CASE("content_remove takes what refers to an item with it")
CHECK(after.crop == before.crop);
CHECK(after.size == before.size);
CHECK(after.pivot == before.pivot);
CHECK_FALSE(model::item_get(regions.content.spritesheets.front().regions, 0));
CHECK_FALSE(item_get(regions.content.spritesheets.front().regions, 0));
}
SUBCASE("a library's layers, flattened")
@@ -555,13 +552,13 @@ TEST_CASE("content_remove takes what refers to an item with it")
auto spin = model_load(file_load(TEST_DIR / "render" / "spin.anm2"));
CHECK(model.uses_count(ElementType::LIBRARY, {0}) == 1);
remove(model, ElementType::LIBRARY, {0}, -1,
[&](const model::Model&, int id) { return id == 0 ? &spin : nullptr; });
[&](const Model&, int id) { return id == 0 ? &spin : nullptr; });
CHECK(model.content.libraries.empty());
CHECK_FALSE(model::item_get(model.content.layers, 0));
CHECK_FALSE(item_get(model.content.layers, 0));
CHECK(
std::ranges::any_of(model.content.layers, [](const model::Layer& layer) { return layer.name == "spin/red"; }));
std::ranges::any_of(model.content.layers, [](const Layer& layer) { return layer.name == "spin/red"; }));
// Layers playing this file's animations stay as they are.
CHECK(model::item_get(model.content.layers, 1)->is_animation());
CHECK(model::item_get(model.content.layers, 3)->is_animation());
CHECK(item_get(model.content.layers, 1)->is_animation());
CHECK(item_get(model.content.layers, 3)->is_animation());
}
}
+25 -32
View File
@@ -13,10 +13,6 @@
#include "image.h"
#include "render.h"
using namespace anm2ed;
namespace anm2ed::test
{
const std::filesystem::path RENDER_TEST_DIR = std::filesystem::path(ANM2ED_TEST_DIR) / "render";
const std::filesystem::path GOLDEN_DIR = RENDER_TEST_DIR / "golden";
const std::filesystem::path OUTPUT_DIR = ANM2ED_RENDER_DIR;
@@ -24,12 +20,9 @@ namespace anm2ed::test
constexpr double MISMATCH_RATIO_MAX = 0.01;
constexpr float SCALE = 3.0f;
constexpr glm::vec4 BACKGROUND = {0.2f, 0.2f, 0.2f, 1.0f};
}
using namespace anm2ed::test;
// Different OpenGL implementations round a little differently: a few pixels may differ, each only slightly.
void golden_image_check(const std::string& name)
static void golden_image_check(const std::string& name)
{
auto actualPath = OUTPUT_DIR / name;
auto goldenPath = GOLDEN_DIR / name;
@@ -39,8 +32,8 @@ void golden_image_check(const std::string& name)
std::filesystem::copy_file(actualPath, goldenPath, std::filesystem::copy_options::overwrite_existing);
return;
}
resource::Image actual(actualPath);
resource::Image golden(goldenPath);
Image actual(actualPath);
Image golden(goldenPath);
REQUIRE(golden.is_valid());
REQUIRE(actual.size == golden.size);
int mismatches{};
@@ -51,11 +44,11 @@ void golden_image_check(const std::string& name)
struct RenderFixture
{
render::HeadlessGL gl{};
HeadlessGL gl{};
std::optional<Document> document{};
std::optional<resource::Shader> shader{};
std::optional<ShaderProgram> shader{};
std::optional<Canvas> canvas{};
const model::Animation* animation{};
const Animation* animation{};
glm::vec4 bounds{};
explicit RenderFixture(const char* file)
@@ -63,18 +56,18 @@ struct RenderFixture
if (!gl.is_valid()) return;
std::filesystem::create_directories(OUTPUT_DIR);
document.emplace(RENDER_TEST_DIR / file, false);
auto& info = resource::shader::SHADERS[resource::shader::TEXTURE];
auto& info = SHADERS[SHADER_TEXTURE];
shader.emplace(info.vertex, info.fragment);
canvas.emplace(glm::vec2(1.0f));
animation = document->model.animation_get(0);
bounds = render::animation_bounds_get(*document, *animation, true);
bounds = animation_bounds_get(*document, *animation, true);
}
std::vector<uint8_t> frame_get(float time)
{
auto pixels =
render::animation_frame_render(*canvas, *shader, *document, *animation, time, bounds, SCALE, BACKGROUND, true);
render::pixels_unpremultiply(pixels);
animation_frame_render(*canvas, *shader, *document, *animation, time, bounds, SCALE, BACKGROUND, true);
pixels_unpremultiply(pixels);
return pixels;
}
@@ -108,7 +101,7 @@ TEST_CASE("frames draw where the animation puts them")
CHECK(blue.b > blue.r + 40);
auto framePath = OUTPUT_DIR / "spin_frame_0.png";
REQUIRE(resource::Image::write_pixels_png(framePath, glm::ivec2(fixture.canvas->size), pixels.data()));
REQUIRE(Image::write_pixels_png(framePath, glm::ivec2(fixture.canvas->size), pixels.data()));
golden_image_check("spin_frame_0.png");
}
@@ -121,16 +114,16 @@ TEST_CASE("a recorded spritesheet holds every frame")
return;
}
render::RecorderOptions options{.type = render::SPRITESHEET,
RecorderOptions options{.type = RENDER_SPRITESHEET,
.path = OUTPUT_DIR / "spin_sheet.png",
.start = 0,
.end = fixture.animation->frameNum - 1,
.animationFps = fixture.document->model.info.fps,
.fps = fixture.document->model.info.fps};
REQUIRE(render::animation_record(options, *fixture.canvas, *fixture.shader, *fixture.document, *fixture.animation,
REQUIRE(animation_record(options, *fixture.canvas, *fixture.shader, *fixture.document, *fixture.animation,
fixture.bounds, SCALE, BACKGROUND, true));
resource::Image sheet(options.path);
auto layout = render::spritesheet_layout_get(0, 0, fixture.animation->frameNum);
Image sheet(options.path);
auto layout = spritesheet_layout_get(0, 0, fixture.animation->frameNum);
CHECK(sheet.size == glm::ivec2(fixture.canvas->size) * glm::ivec2(layout.columns, layout.rows));
golden_image_check("spin_sheet.png");
}
@@ -155,7 +148,7 @@ TEST_CASE("animation layers draw their child animations")
CHECK(red.g < 80);
}
REQUIRE(resource::Image::write_pixels_png(OUTPUT_DIR / "nested_frame_0.png", glm::ivec2(fixture.canvas->size),
REQUIRE(Image::write_pixels_png(OUTPUT_DIR / "nested_frame_0.png", glm::ivec2(fixture.canvas->size),
pixels.data()));
golden_image_check("nested_frame_0.png");
}
@@ -180,7 +173,7 @@ TEST_CASE("a blurred layer spreads past its crop like Flash's blur filter")
CHECK(fixture.pixel_get(blurred, OUTSIDE).r > fixture.pixel_get(sharp, OUTSIDE).r + 40);
CHECK(fixture.pixel_get(blurred, INSIDE).r + 40 < fixture.pixel_get(sharp, INSIDE).r);
REQUIRE(resource::Image::write_pixels_png(OUTPUT_DIR / "spin_blur_frame_0.png", glm::ivec2(fixture.canvas->size),
REQUIRE(Image::write_pixels_png(OUTPUT_DIR / "spin_blur_frame_0.png", glm::ivec2(fixture.canvas->size),
blurred.data()));
}
@@ -202,7 +195,7 @@ TEST_CASE("SVG spritesheets draw sharp at any scale")
CHECK(inside.r > 200);
CHECK(outside.r < 80);
REQUIRE(resource::Image::write_pixels_png(OUTPUT_DIR / "vector_frame_0.png", glm::ivec2(fixture.canvas->size),
REQUIRE(Image::write_pixels_png(OUTPUT_DIR / "vector_frame_0.png", glm::ivec2(fixture.canvas->size),
pixels.data()));
golden_image_check("vector_frame_0.png");
}
@@ -222,7 +215,7 @@ TEST_CASE("converting SVG spritesheets writes PNGs beside them")
CHECK(std::filesystem::exists(directory / "vector.png"));
REQUIRE(document.texture_get(0));
CHECK_FALSE(document.texture_get(0)->is_vector());
CHECK(model::spritesheets_vector_used_get(document.model).empty());
CHECK(spritesheets_vector_used_get(document.model).empty());
document.undo();
CHECK(document.model.content.spritesheets.front().path == "vector.svg");
@@ -262,7 +255,7 @@ TEST_CASE("a Rebirth save flattens animation layers for the game")
// The game's copy draws what the editor does: skew, blur and a skewed root baked into a spritesheet beside the save.
TEST_CASE("the game's copy bakes skew and blur into a spritesheet that draws the same")
{
render::HeadlessGL gl{};
HeadlessGL gl{};
if (!gl.is_valid()) return;
auto directory = std::filesystem::temp_directory_path() / "anm2ed_bake";
std::filesystem::remove_all(directory);
@@ -297,8 +290,8 @@ TEST_CASE("the game's copy bakes skew and blur into a spritesheet that draws the
Document gameDocument(directory / "game.anm2", false);
REQUIRE(gameDocument.is_valid());
auto& info = resource::shader::SHADERS[resource::shader::TEXTURE];
resource::Shader shader(info.vertex, info.fragment);
auto& info = SHADERS[SHADER_TEXTURE];
ShaderProgram shader(info.vertex, info.fragment);
Canvas canvas(glm::vec2(1.0f));
for (int index = 0; index < document.model.animations_count_get(); ++index)
{
@@ -306,14 +299,14 @@ TEST_CASE("the game's copy bakes skew and blur into a spritesheet that draws the
auto& gameAnimation = *gameDocument.model.animation_get(index);
INFO("animation: ", animation.name);
// On whole pixels, so the baked images land on the pixels they were drawn on.
auto bounds = render::animation_bounds_get(document, animation, true);
auto bounds = animation_bounds_get(document, animation, true);
bounds = glm::vec4(glm::floor(glm::vec2(bounds)) - 8.0f, glm::ceil(glm::vec2(bounds.z, bounds.w)) + 16.0f);
for (int time = 0; time < animation.frameNum; ++time)
{
INFO("time: ", time);
auto expected = render::animation_frame_render(canvas, shader, document, animation, (float)time, bounds, 1.0f,
auto expected = animation_frame_render(canvas, shader, document, animation, (float)time, bounds, 1.0f,
glm::vec4(0.0f), true);
auto actual = render::animation_frame_render(canvas, shader, gameDocument, gameAnimation, (float)time, bounds,
auto actual = animation_frame_render(canvas, shader, gameDocument, gameAnimation, (float)time, bounds,
1.0f, glm::vec4(0.0f), true);
REQUIRE(actual.size() == expected.size());
CHECK(std::ranges::any_of(expected, [](uint8_t channel) { return channel != 0; }));
-3
View File
@@ -1,8 +1,5 @@
#include "common.h"
using namespace anm2ed;
using namespace anm2ed::test;
TEST_CASE("fixtures match goldens")
{
auto fixtures = fixtures_get();
+21 -24
View File
@@ -3,18 +3,15 @@
#include "edit/edit.h"
#include "selection.h"
using namespace anm2ed;
using namespace anm2ed::test;
model::Model fixture_load_with_uids(const char* name) { return model_load(file_load(CORPUS_DIR / name)); }
static Model fixture_load_with_uids(const char* name) { return model_load(file_load(CORPUS_DIR / name)); }
// Applies an edit and follows the selection through it, the way a commit does.
void selection_edit(model::Model& anm2, Selection& selection, auto&& edit)
void selection_edit(Model& anm2, Selection& selection, auto&& edit)
{
auto before = selection_focus_get(selection, model::UidIndex(anm2));
auto before = selection_focus_get(selection, UidIndex(anm2));
edit();
anm2.uids_repair();
selection_follow(selection, anm2, model::UidIndex(anm2), before);
selection_follow(selection, anm2, UidIndex(anm2), before);
}
TEST_CASE("uids are unique and non-zero after load")
@@ -25,8 +22,8 @@ TEST_CASE("uids are unique and non-zero after load")
std::set<std::uint64_t> uids{};
bool isUnique{true};
for (auto [index, animation] : anm2.animations_get())
model::animation_tracks_each(*animation,
[&](const model::Track& track)
animation_tracks_each(*animation,
[&](const Track& track)
{
isUnique &= uids.insert(track.uid).second;
for (const auto& frame : track.frames)
@@ -44,10 +41,10 @@ TEST_CASE("selected frames and focus follow inserts and moves")
selection_references_set(selection, anm2, SelectionKind::FRAMES, {{0, LAYER, 0, 1}, {0, LAYER, 1, 0}});
selection_focus_set(selection, anm2, {0, LAYER, 0, 1});
selection_edit(anm2, selection, [&] { edit::frame_insert(anm2, {0, LAYER, 0, -1}, 0); });
selection_edit(anm2, selection, [&] { edit::frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}); });
selection_edit(anm2, selection, [&] { frame_insert(anm2, {0, LAYER, 0, -1}, 0); });
selection_edit(anm2, selection, [&] { frames_reverse(anm2, {{0, LAYER, 0, 0}, {0, LAYER, 0, 1}}); });
model::UidIndex index(anm2);
UidIndex index(anm2);
CHECK(selection_focus_get(selection, index) == Reference{0, LAYER, 0, 0});
CHECK(selection_references_get(selection, index, SelectionKind::FRAMES) ==
std::set<Reference>{{0, LAYER, 0, 0}, {0, LAYER, 1, 0}});
@@ -60,23 +57,23 @@ TEST_CASE("a removed focus falls back to its nearest survivor")
selection_focus_set(selection, anm2, {0, LAYER, 0, 1});
selection_references_set(selection, anm2, SelectionKind::FRAMES, {{0, LAYER, 0, 1}});
selection_edit(anm2, selection, [&] { edit::frames_delete(anm2, {{0, LAYER, 0, 1}}); });
CHECK(selection_focus_get(selection, model::UidIndex(anm2)) == Reference{0, LAYER, 0, 0});
selection_edit(anm2, selection, [&] { frames_delete(anm2, {{0, LAYER, 0, 1}}); });
CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, LAYER, 0, 0});
CHECK(selection.uids[SelectionKind::FRAMES].empty());
selection_edit(anm2, selection, [&] { edit::items_remove(anm2, 0, {{LAYER, {0}}}, {}); });
CHECK(selection_focus_get(selection, model::UidIndex(anm2)) == Reference{0, ROOT});
selection_edit(anm2, selection, [&] { items_remove(anm2, 0, {{LAYER, {0}}}, {}); });
CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, ROOT});
selection_focus_set(selection, anm2, {1, ROOT, -1, 0});
selection_edit(anm2, selection, [&] { edit::animations_remove(anm2, {1}, {}); });
CHECK(selection_focus_get(selection, model::UidIndex(anm2)) == Reference{0, ROOT});
selection_edit(anm2, selection, [&] { animations_remove(anm2, {1}, {}); });
CHECK(selection_focus_get(selection, UidIndex(anm2)) == Reference{0, ROOT});
}
TEST_CASE("track and group selection follow regrouping")
{
auto anm2 = fixture_load_with_uids("03a_groups_nested.anm2");
const model::TrackGroup* group{};
model::groups_each(anm2.animation_get(0)->layers, [&](const model::TrackGroup& candidate) { group = &candidate; });
const TrackGroup* group{};
groups_each(anm2.animation_get(0)->layers, [&](const TrackGroup& candidate) { group = &candidate; });
REQUIRE(group);
auto groupId = group->id;
Selection selection{};
@@ -84,9 +81,9 @@ TEST_CASE("track and group selection follow regrouping")
selection_references_set(selection, anm2, SelectionKind::GROUPS, {{0, LAYER, groupId}});
selection_edit(anm2, selection,
[&] { edit::items_move(anm2, 0, LAYER, {0}, {}, {false, LAYER, 2, -1}, false, false); });
[&] { items_move(anm2, 0, LAYER, {0}, {}, {false, LAYER, 2, -1}, false, false); });
model::UidIndex index(anm2);
UidIndex index(anm2);
CHECK(selection_references_get(selection, index, SelectionKind::TRACKS) == std::set<Reference>{{0, LAYER, 0}});
CHECK(selection_references_get(selection, index, SelectionKind::GROUPS) == std::set<Reference>{{0, LAYER, groupId}});
}
@@ -95,7 +92,7 @@ TEST_CASE("content selection follows items, and regions follow the focused sprit
{
auto anm2 = fixture_load_with_uids("05_regions.anm2");
Selection selection{};
auto& regions = model::item_get(anm2.content.spritesheets, 0)->regions;
auto& regions = item_get(anm2.content.spritesheets, 0)->regions;
REQUIRE(regions.size() > 1);
auto regionId = regions[1].id;
@@ -109,7 +106,7 @@ TEST_CASE("content selection follows items, and regions follow the focused sprit
CHECK(selection_ids_get(selection, anm2, SelectionKind::REGIONS) == std::set<int>{regionId});
CHECK(selection_focus_id_get(selection, anm2, SelectionKind::REGIONS) == regionId);
std::erase_if(regions, [&](const model::Region& region) { return region.id == regionId; });
std::erase_if(regions, [&](const Region& region) { return region.id == regionId; });
CHECK(selection_ids_get(selection, anm2, SelectionKind::REGIONS).empty());
CHECK(selection_focus_id_get(selection, anm2, SelectionKind::REGIONS) == -1);
}
+21 -24
View File
@@ -5,16 +5,13 @@
#include "edit/edit.h"
#include "snapshots.h"
using namespace anm2ed;
using namespace anm2ed::test;
constexpr int WALK_STEPS = 150;
constexpr int WALK_SEED = 1234;
constexpr int UNDO_LIMIT = 1000;
std::vector<Reference> references_all_get(const model::Model& anm2, bool isFrames)
static std::vector<Reference> references_all_get(const Model& anm2, bool isFrames)
{
model::UidIndex index(anm2);
UidIndex index(anm2);
std::vector<Reference> result{};
auto add = [&](std::uint64_t uid)
{
@@ -23,8 +20,8 @@ std::vector<Reference> references_all_get(const model::Model& anm2, bool isFrame
if (isFrames == (reference->frameIndex >= 0)) result.push_back(*reference);
};
for (auto [i, animation] : anm2.animations_get())
model::animation_tracks_each(*animation,
[&](const model::Track& track)
animation_tracks_each(*animation,
[&](const Track& track)
{
add(track.uid);
for (const auto& frame : track.frames)
@@ -34,7 +31,7 @@ std::vector<Reference> references_all_get(const model::Model& anm2, bool isFrame
}
// One random edit on randomly chosen live elements; returns false when nothing applicable exists.
bool edit_random_apply(model::Model& anm2, std::mt19937& random)
static bool edit_random_apply(Model& anm2, std::mt19937& random)
{
auto frames = references_all_get(anm2, true);
auto tracks = references_all_get(anm2, false);
@@ -47,31 +44,31 @@ bool edit_random_apply(model::Model& anm2, std::mt19937& random)
switch (std::uniform_int_distribution<int>(0, 8)(random))
{
case 0:
edit::frame_insert(anm2, frame, 0);
frame_insert(anm2, frame, 0);
break;
case 1:
if (frames.size() > 4) edit::frames_delete(anm2, {frame, other});
if (frames.size() > 4) frames_delete(anm2, {frame, other});
break;
case 2:
edit::frames_duplicate(anm2, {frame, other}, frame);
frames_duplicate(anm2, {frame, other}, frame);
break;
case 3:
edit::frames_reverse(anm2, {frame, other});
frames_reverse(anm2, {frame, other});
break;
case 4:
edit::frames_bake(anm2, {frame}, 1, false, false);
frames_bake(anm2, {frame}, 1, false, false);
break;
case 5:
edit::frame_split(anm2, frame, 1);
frame_split(anm2, frame, 1);
break;
case 6:
if (frame.itemType != ROOT) edit::frames_move(anm2, {frame}, pick(tracks), 0);
if (frame.itemType != ROOT) frames_move(anm2, {frame}, pick(tracks), 0);
break;
case 7:
edit::animation_add(anm2, 0, -1, frame.animationIndex, "Walk");
animation_add(anm2, 0, -1, frame.animationIndex, "Walk");
break;
default:
edit::animations_move(anm2, {frame.animationIndex}, {}, {.animationIndex = 0});
animations_move(anm2, {frame.animationIndex}, {}, {.animationIndex = 0});
break;
}
return true;
@@ -88,28 +85,28 @@ TEST_CASE("undo and redo restore every state of a random edit walk")
snapshots.current.model = model_load(file_load(CORPUS_DIR / name));
std::mt19937 random(WALK_SEED);
std::vector<std::uint64_t> hashes{model::model_hash(snapshots.current.model)};
std::vector<std::uint64_t> hashes{model_hash(snapshots.current.model)};
for (int step = 0; step < WALK_STEPS; ++step)
{
snapshots.push("Edit");
if (!edit_random_apply(snapshots.current.model, random)) break;
auto count = snapshots.undoStack.size();
snapshots.commit();
if (snapshots.undoStack.size() != count) hashes.push_back(model::model_hash(snapshots.current.model));
if (snapshots.undoStack.size() != count) hashes.push_back(model_hash(snapshots.current.model));
}
REQUIRE(hashes.size() > 1);
for (auto it = hashes.rbegin() + 1; it != hashes.rend(); ++it)
{
REQUIRE(snapshots.undo());
CHECK(model::model_hash(snapshots.current.model) == *it);
CHECK(model_hash(snapshots.current.model) == *it);
}
CHECK_FALSE(snapshots.undo());
for (auto it = hashes.begin() + 1; it != hashes.end(); ++it)
{
REQUIRE(snapshots.redo());
CHECK(model::model_hash(snapshots.current.model) == *it);
CHECK(model_hash(snapshots.current.model) == *it);
}
}
}
@@ -123,15 +120,15 @@ TEST_CASE("selection follows its frame through an edit and its undo")
selection_references_set(current.selection, current.model, SelectionKind::FRAMES, {{0, LAYER, 0, 1}});
snapshots.push("Insert");
edit::frame_insert(current.model, {0, LAYER, 0, -1}, 0);
frame_insert(current.model, {0, LAYER, 0, -1}, 0);
auto& track = *current.model.track_edit(Reference{0, LAYER, 0});
std::rotate(track.frames.begin(), track.frames.end() - 1, track.frames.end());
snapshots.commit();
model::UidIndex index(current.model);
UidIndex index(current.model);
CHECK(selection_focus_get(current.selection, index) == Reference{0, LAYER, 0, 2});
CHECK(selection_references_get(current.selection, index, SelectionKind::FRAMES) ==
std::set<Reference>{{0, LAYER, 0, 2}});
REQUIRE(snapshots.undo());
CHECK(selection_focus_get(current.selection, model::UidIndex(current.model)) == Reference{0, LAYER, 0, 1});
CHECK(selection_focus_get(current.selection, UidIndex(current.model)) == Reference{0, LAYER, 0, 1});
}