mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
Allow RuntimeTexturePacker to automatically dispose submitted textures when packing
This commit is contained in:
parent
149669df99
commit
115b05beaa
3 changed files with 31 additions and 7 deletions
|
@ -36,6 +36,9 @@ Fixes
|
||||||
- Fixed text not being pasted into a text field at all if it contains characters that don't match the input rule
|
- Fixed text not being pasted into a text field at all if it contains characters that don't match the input rule
|
||||||
|
|
||||||
### MLEM.Data
|
### MLEM.Data
|
||||||
|
Additions
|
||||||
|
- Allow RuntimeTexturePacker to automatically dispose submitted textures when packing
|
||||||
|
|
||||||
Improvements
|
Improvements
|
||||||
- Use TitleContainer for opening streams where possible
|
- Use TitleContainer for opening streams where possible
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,6 @@ namespace MLEM.Data {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class RuntimeTexturePacker : IDisposable {
|
public class RuntimeTexturePacker : IDisposable {
|
||||||
|
|
||||||
private readonly List<Request> textures = new List<Request>();
|
|
||||||
private readonly bool autoIncreaseMaxWidth;
|
|
||||||
private readonly bool forcePowerOfTwo;
|
|
||||||
private readonly bool forceSquare;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The generated packed texture.
|
/// The generated packed texture.
|
||||||
/// This value is null before <see cref="Pack"/> is called.
|
/// This value is null before <see cref="Pack"/> is called.
|
||||||
|
@ -37,6 +32,13 @@ namespace MLEM.Data {
|
||||||
/// The time that <see cref="Pack"/> took the last time it was called
|
/// The time that <see cref="Pack"/> took the last time it was called
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TimeSpan LastTotalTime => this.LastCalculationTime + this.LastPackTime;
|
public TimeSpan LastTotalTime => this.LastCalculationTime + this.LastPackTime;
|
||||||
|
|
||||||
|
private readonly List<Request> textures = new List<Request>();
|
||||||
|
private readonly bool autoIncreaseMaxWidth;
|
||||||
|
private readonly bool forcePowerOfTwo;
|
||||||
|
private readonly bool forceSquare;
|
||||||
|
private readonly bool disposeTextures;
|
||||||
|
|
||||||
private int maxWidth;
|
private int maxWidth;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -46,11 +48,13 @@ namespace MLEM.Data {
|
||||||
/// <param name="autoIncreaseMaxWidth">Whether the maximum width should be increased if there is a texture to be packed that is wider than <see cref="maxWidth"/>. Defaults to false.</param>
|
/// <param name="autoIncreaseMaxWidth">Whether the maximum width should be increased if there is a texture to be packed that is wider than <see cref="maxWidth"/>. Defaults to false.</param>
|
||||||
/// <param name="forcePowerOfTwo">Whether the resulting <see cref="PackedTexture"/> should have a width and height that is a power of two</param>
|
/// <param name="forcePowerOfTwo">Whether the resulting <see cref="PackedTexture"/> should have a width and height that is a power of two</param>
|
||||||
/// <param name="forceSquare">Whether the resulting <see cref="PackedTexture"/> should be square regardless of required size</param>
|
/// <param name="forceSquare">Whether the resulting <see cref="PackedTexture"/> should be square regardless of required size</param>
|
||||||
public RuntimeTexturePacker(int maxWidth = 2048, bool autoIncreaseMaxWidth = false, bool forcePowerOfTwo = false, bool forceSquare = false) {
|
/// <param name="disposeTextures">Whether the original textures submitted to this texture packer should be disposed after packing</param>
|
||||||
|
public RuntimeTexturePacker(int maxWidth = 2048, bool autoIncreaseMaxWidth = false, bool forcePowerOfTwo = false, bool forceSquare = false, bool disposeTextures = false) {
|
||||||
this.maxWidth = maxWidth;
|
this.maxWidth = maxWidth;
|
||||||
this.autoIncreaseMaxWidth = autoIncreaseMaxWidth;
|
this.autoIncreaseMaxWidth = autoIncreaseMaxWidth;
|
||||||
this.forcePowerOfTwo = forcePowerOfTwo;
|
this.forcePowerOfTwo = forcePowerOfTwo;
|
||||||
this.forceSquare = forceSquare;
|
this.forceSquare = forceSquare;
|
||||||
|
this.disposeTextures = disposeTextures;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -124,8 +128,12 @@ namespace MLEM.Data {
|
||||||
this.LastPackTime = stopwatch.Elapsed;
|
this.LastPackTime = stopwatch.Elapsed;
|
||||||
|
|
||||||
// invoke callbacks
|
// invoke callbacks
|
||||||
foreach (var request in this.textures)
|
foreach (var request in this.textures) {
|
||||||
request.Result.Invoke(new TextureRegion(this.PackedTexture, request.PackedArea));
|
request.Result.Invoke(new TextureRegion(this.PackedTexture, request.PackedArea));
|
||||||
|
if (this.disposeTextures)
|
||||||
|
request.Texture.Texture.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
this.textures.Clear();
|
this.textures.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,18 +8,21 @@ namespace Tests {
|
||||||
public class TexturePackerTests {
|
public class TexturePackerTests {
|
||||||
|
|
||||||
private Texture2D testTexture;
|
private Texture2D testTexture;
|
||||||
|
private Texture2D disposedTestTexture;
|
||||||
private TestGame game;
|
private TestGame game;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void SetUp() {
|
public void SetUp() {
|
||||||
this.game = TestGame.Create();
|
this.game = TestGame.Create();
|
||||||
this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256);
|
this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256);
|
||||||
|
this.disposedTestTexture = new Texture2D(this.game.GraphicsDevice, 16, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TearDown]
|
[TearDown]
|
||||||
public void TearDown() {
|
public void TearDown() {
|
||||||
this.game?.Dispose();
|
this.game?.Dispose();
|
||||||
this.testTexture?.Dispose();
|
this.testTexture?.Dispose();
|
||||||
|
this.disposedTestTexture?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -37,6 +40,16 @@ namespace Tests {
|
||||||
Assert.AreEqual(packer.PackedTexture.Height, 64);
|
Assert.AreEqual(packer.PackedTexture.Height, 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestDisposal() {
|
||||||
|
using var packer = new RuntimeTexturePacker(128, disposeTextures: true);
|
||||||
|
packer.Add(new TextureRegion(this.disposedTestTexture), StubResult);
|
||||||
|
packer.Add(new TextureRegion(this.disposedTestTexture, 0, 0, 8, 8), StubResult);
|
||||||
|
packer.Pack(this.game.GraphicsDevice);
|
||||||
|
Assert.True(this.disposedTestTexture.IsDisposed);
|
||||||
|
Assert.False(packer.PackedTexture.IsDisposed);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestBounds() {
|
public void TestBounds() {
|
||||||
// test forced max width
|
// test forced max width
|
||||||
|
|
Loading…
Reference in a new issue