From c3c8b132da2389990f737d4dac2d22ebf57f4735 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 14 Sep 2022 12:19:05 +0200 Subject: [PATCH] fixed DataTextureAtlas frm instruction failing if the original texture has an offset --- MLEM.Data/DataTextureAtlas.cs | 4 +++- Tests/DataTextureAtlasTests.cs | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index 11fe3a0..fcd15c5 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -6,6 +6,7 @@ using System.Text.RegularExpressions; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; using MLEM.Misc; using MLEM.Textures; #if FNA @@ -148,7 +149,8 @@ namespace MLEM.Data { customData.Clear(); foreach (var key in fromRegion.GetDataKeys()) customData.Add(key, fromRegion.GetData(key)); - location = fromRegion.Area; + // our main texture might be a sub-region already, so we have to take that into account + location = fromRegion.Area.OffsetCopy(new Point(-texture.U, -texture.V)); pivot = fromRegion.PivotPixels; if (pivot != Vector2.Zero && !pivotRelative) pivot += location.Location.ToVector2(); diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index cf4a02a..f81c344 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -9,43 +9,44 @@ namespace Tests { public class TestDataTextureAtlas { [Test] - public void Test() { + public void Test([Values(0, 4)] int regionX, [Values(0, 4)] int regionY) { using var game = TestGame.Create(); using var texture = new Texture2D(game.GraphicsDevice, 1, 1); - var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas"); + var region = new TextureRegion(texture, regionX, regionY, 1, 1); + var atlas = DataTextureAtlas.LoadAtlasData(region, game.RawContent, "Texture.atlas"); Assert.AreEqual(12, atlas.Regions.Count()); // no pivot var plant = atlas["Plant"]; - Assert.AreEqual(plant.Area, new Rectangle(96, 0, 16, 32)); + Assert.AreEqual(plant.Area, new Rectangle(96 + regionX, 0 + regionY, 16, 32)); Assert.AreEqual(plant.PivotPixels, Vector2.Zero); // no added offset var table = atlas["LongTableUp"]; - Assert.AreEqual(table.Area, new Rectangle(0, 32, 64, 48)); + Assert.AreEqual(table.Area, new Rectangle(0 + regionX, 32 + regionY, 64, 48)); Assert.AreEqual(table.PivotPixels, new Vector2(16, 48 - 32)); // added offset var table2 = atlas["LongTableDown"]; - Assert.AreEqual(table2.Area, new Rectangle(64, 32, 64, 48)); + Assert.AreEqual(table2.Area, new Rectangle(64 + regionX, 32 + regionY, 64, 48)); Assert.AreEqual(table2.PivotPixels, new Vector2(112 - 64, 48 - 32)); // negative pivot var negativePivot = atlas["TestRegionNegativePivot"]; - Assert.AreEqual(negativePivot.Area, new Rectangle(0, 32, 16, 16)); + Assert.AreEqual(negativePivot.Area, new Rectangle(0 + regionX, 32 + regionY, 16, 16)); Assert.AreEqual(negativePivot.PivotPixels, new Vector2(-32, 46 - 32)); // cpy (pivot pixels should be identical to LongTableUp because they're region-internal) var copy1 = atlas["Copy1"]; - Assert.AreEqual(copy1.Area, new Rectangle(0 + 16, 32, 64, 48)); + Assert.AreEqual(copy1.Area, new Rectangle(0 + 16 + regionX, 32 + regionY, 64, 48)); Assert.AreEqual(copy1.PivotPixels, new Vector2(16, 48 - 32)); var copy2 = atlas["Copy2"]; - Assert.AreEqual(copy2.Area, new Rectangle(0 + 32, 32 + 4, 64, 48)); + Assert.AreEqual(copy2.Area, new Rectangle(0 + 32 + regionX, 32 + 4 + regionY, 64, 48)); Assert.AreEqual(copy2.PivotPixels, new Vector2(16, 48 - 32)); // frm var copy3 = atlas["Copy3"]; - Assert.AreEqual(copy3.Area, new Rectangle(0 + 2, 32 + 4, 64, 48)); + Assert.AreEqual(copy3.Area, new Rectangle(0 + 2 + regionX, 32 + 4 + regionY, 64, 48)); Assert.AreEqual(copy3.PivotPixels, new Vector2(16, 48 - 32)); // data