2021-04-04 18:31:51 +02:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using MLEM.Data;
|
|
|
|
|
using MLEM.Textures;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Tests {
|
|
|
|
|
public class TexturePackerTests {
|
|
|
|
|
|
|
|
|
|
private Texture2D testTexture;
|
2021-11-01 16:00:13 +01:00
|
|
|
|
private Texture2D disposedTestTexture;
|
2021-04-04 18:31:51 +02:00
|
|
|
|
private TestGame game;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp() {
|
|
|
|
|
this.game = TestGame.Create();
|
|
|
|
|
this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256);
|
2021-11-01 16:00:13 +01:00
|
|
|
|
this.disposedTestTexture = new Texture2D(this.game.GraphicsDevice, 16, 16);
|
2021-04-04 18:31:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
public void TearDown() {
|
|
|
|
|
this.game?.Dispose();
|
|
|
|
|
this.testTexture?.Dispose();
|
2021-11-01 16:00:13 +01:00
|
|
|
|
this.disposedTestTexture?.Dispose();
|
2021-04-04 18:31:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestPacking() {
|
|
|
|
|
using var packer = new RuntimeTexturePacker();
|
|
|
|
|
for (var i = 0; i < 5; i++) {
|
|
|
|
|
var width = 16 * (i + 1);
|
|
|
|
|
packer.Add(new TextureRegion(this.testTexture, 0, 0, width, 64), r => {
|
|
|
|
|
Assert.AreEqual(r.Width, width);
|
|
|
|
|
Assert.AreEqual(r.Height, 64);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
packer.Pack(this.game.GraphicsDevice);
|
|
|
|
|
Assert.AreEqual(packer.PackedTexture.Width, 16 + 32 + 48 + 64 + 80);
|
|
|
|
|
Assert.AreEqual(packer.PackedTexture.Height, 64);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-01 16:00:13 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestDisposal() {
|
|
|
|
|
using var packer = new RuntimeTexturePacker(128, disposeTextures: true);
|
2022-06-15 11:38:11 +02:00
|
|
|
|
packer.Add(new TextureRegion(this.disposedTestTexture), TexturePackerTests.StubResult);
|
|
|
|
|
packer.Add(new TextureRegion(this.disposedTestTexture, 0, 0, 8, 8), TexturePackerTests.StubResult);
|
2021-11-01 16:00:13 +01:00
|
|
|
|
packer.Pack(this.game.GraphicsDevice);
|
|
|
|
|
Assert.True(this.disposedTestTexture.IsDisposed);
|
|
|
|
|
Assert.False(packer.PackedTexture.IsDisposed);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 18:31:51 +02:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestBounds() {
|
|
|
|
|
// test forced max width
|
|
|
|
|
using var packer = new RuntimeTexturePacker(128);
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() => {
|
2022-06-15 11:38:11 +02:00
|
|
|
|
packer.Add(new TextureRegion(this.testTexture, 0, 0, 256, 128), TexturePackerTests.StubResult);
|
2021-04-04 18:31:51 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// test auto-expanding width
|
|
|
|
|
using var packer2 = new RuntimeTexturePacker(128, true);
|
|
|
|
|
Assert.DoesNotThrow(() => {
|
2022-06-15 11:38:11 +02:00
|
|
|
|
packer2.Add(new TextureRegion(this.testTexture, 0, 0, 256, 128), TexturePackerTests.StubResult);
|
2021-04-04 18:31:51 +02:00
|
|
|
|
});
|
|
|
|
|
packer2.Pack(this.game.GraphicsDevice);
|
|
|
|
|
|
|
|
|
|
// test power of two forcing
|
|
|
|
|
using var packer3 = new RuntimeTexturePacker(128, forcePowerOfTwo: true);
|
2022-06-15 11:38:11 +02:00
|
|
|
|
packer3.Add(new TextureRegion(this.testTexture, 0, 0, 37, 170), TexturePackerTests.StubResult);
|
2021-04-04 18:31:51 +02:00
|
|
|
|
packer3.Pack(this.game.GraphicsDevice);
|
|
|
|
|
Assert.AreEqual(64, packer3.PackedTexture.Width);
|
|
|
|
|
Assert.AreEqual(256, packer3.PackedTexture.Height);
|
|
|
|
|
|
|
|
|
|
// test square forcing
|
|
|
|
|
using var packer4 = new RuntimeTexturePacker(128, forceSquare: true);
|
2022-06-15 11:38:11 +02:00
|
|
|
|
packer4.Add(new TextureRegion(this.testTexture, 0, 0, 37, 170), TexturePackerTests.StubResult);
|
2021-04-04 18:31:51 +02:00
|
|
|
|
packer4.Pack(this.game.GraphicsDevice);
|
|
|
|
|
Assert.AreEqual(170, packer4.PackedTexture.Width);
|
|
|
|
|
Assert.AreEqual(170, packer4.PackedTexture.Height);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-20 23:59:42 +02:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestPackMultipleTimes() {
|
|
|
|
|
using var packer = new RuntimeTexturePacker(1024);
|
|
|
|
|
|
|
|
|
|
// pack the first time
|
|
|
|
|
var results = 0;
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
packer.Add(new TextureRegion(this.testTexture, 0, 0, 64, 64), _ => results++);
|
|
|
|
|
packer.Pack(this.game.GraphicsDevice);
|
|
|
|
|
Assert.AreEqual(10, results);
|
|
|
|
|
|
|
|
|
|
// pack without resizing
|
|
|
|
|
packer.Add(new TextureRegion(this.testTexture, 0, 0, 0, 0), _ => results++);
|
|
|
|
|
packer.Pack(this.game.GraphicsDevice);
|
|
|
|
|
Assert.AreEqual(11, results);
|
|
|
|
|
|
|
|
|
|
// pack and force a resize
|
|
|
|
|
packer.Add(new TextureRegion(this.testTexture, 0, 0, 64, 64), _ => results++);
|
|
|
|
|
packer.Pack(this.game.GraphicsDevice);
|
|
|
|
|
// all callbacks are called again, so we add 11 again, as well as the callback we just added
|
|
|
|
|
Assert.AreEqual(2 * 11 + 1, results);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 14:56:11 +01:00
|
|
|
|
private static void StubResult(TextureRegion region) {}
|
2021-04-04 18:31:51 +02:00
|
|
|
|
|
|
|
|
|
}
|
2022-06-17 18:23:47 +02:00
|
|
|
|
}
|