Spritesheet pixel edits live in src/edit/spritesheets.cpp
regions_trim, spritesheet_pack and spritesheets_merge are model functions that take the source image(s) and return the new image; Document keeps thin wrappers that store the result as an asset. The edits are now tested headless (trim, pack, merge with layer remap). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4b6e186f57
commit
c4b21fd90c
5 files changed
+308
-177
No files matched your search
@@ -0,0 +1,41 @@
|
||||
<AnimatedActor>
|
||||
<Info CreatedBy="fixture" CreatedOn="01/01/2026 12:00:00 PM" Fps="30" Version="2"/>
|
||||
<Content>
|
||||
<Spritesheets>
|
||||
<Spritesheet Id="0" Path="sheet.png">
|
||||
<Region Id="0" Name="custom" XCrop="0" YCrop="0" Width="16" Height="16" XPivot="3" YPivot="5"/>
|
||||
<Region Id="1" Name="topleft" XCrop="16" YCrop="0" Width="16" Height="32" Origin="TopLeft"/>
|
||||
<Region Id="2" Name="center" XCrop="32" YCrop="0" Width="10" Height="20" Origin="Center"/>
|
||||
<Region Id="3" Name="gap" XCrop="0" YCrop="32" Width="8" Height="8" XPivot="0" YPivot="0"/>
|
||||
<Region Id="4" Name="other" XCrop="64" YCrop="0" Width="16" Height="16" Origin="TopLeft"/>
|
||||
</Spritesheet>
|
||||
</Spritesheets>
|
||||
<Layers>
|
||||
<Layer Id="0" Name="a" SpritesheetId="0"/>
|
||||
<Layer Id="1" Name="b" SpritesheetId="0"/>
|
||||
</Layers>
|
||||
<Nulls/>
|
||||
<Events/>
|
||||
</Content>
|
||||
<Animations DefaultAnimation="A">
|
||||
<Animation Name="A" FrameNum="20" Loop="true">
|
||||
<RootAnimation>
|
||||
<Frame XPosition="0" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" ShearX="0" ShearY="0" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
</RootAnimation>
|
||||
<LayerAnimations>
|
||||
<LayerAnimation LayerId="0" Visible="true">
|
||||
<Frame RegionId="0" XPosition="0" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" ShearX="0" ShearY="0" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
<Frame RegionId="1" XPosition="0" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" ShearX="0" ShearY="0" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
<Frame RegionId="2" XPosition="0" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" ShearX="0" ShearY="0" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
<Frame XPivot="1" YPivot="1" XCrop="2" YCrop="2" Width="4" Height="4" XPosition="0" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" ShearX="0" ShearY="0" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
<Frame RegionId="3" XPosition="0" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" ShearX="0" ShearY="0" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
</LayerAnimation>
|
||||
<LayerAnimation LayerId="1" Visible="true">
|
||||
<Frame XPivot="8" YPivot="8" XCrop="0" YCrop="0" Width="16" Height="16" XPosition="0" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" ShearX="0" ShearY="0" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
</LayerAnimation>
|
||||
</LayerAnimations>
|
||||
<NullAnimations/>
|
||||
<Triggers/>
|
||||
</Animation>
|
||||
</Animations>
|
||||
</AnimatedActor>
|
||||
@@ -243,3 +243,49 @@ TEST_CASE("edit layer operations")
|
||||
operation_check(entry.name, anm2);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
};
|
||||
auto sheet = image_make({64, 64}, {{4, 6}});
|
||||
auto other = image_make({16, 16});
|
||||
|
||||
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(region->crop == glm::vec2(4, 6));
|
||||
CHECK(region->size == glm::vec2(1, 1));
|
||||
CHECK(region->pivot == glm::vec2(-1, -1));
|
||||
}
|
||||
|
||||
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);
|
||||
REQUIRE(packed);
|
||||
CHECK(packed->is_valid());
|
||||
for (auto& region : model.content.spritesheets[0].regions)
|
||||
CHECK(glm::all(glm::lessThanEqual(glm::ivec2(region.crop + region.size), packed->size)));
|
||||
}
|
||||
|
||||
SUBCASE("spritesheets_merge appends images and moves layers onto the base sheet")
|
||||
{
|
||||
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);
|
||||
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);
|
||||
operation_check("spritesheets_merge", model);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user