mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +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
|
||||
|
||||
### MLEM.Data
|
||||
Additions
|
||||
- Allow RuntimeTexturePacker to automatically dispose submitted textures when packing
|
||||
|
||||
Improvements
|
||||
- Use TitleContainer for opening streams where possible
|
||||
|
||||
|
|
|
@ -15,11 +15,6 @@ namespace MLEM.Data {
|
|||
/// </summary>
|
||||
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>
|
||||
/// The generated packed texture.
|
||||
/// 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
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <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="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>
|
||||
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.autoIncreaseMaxWidth = autoIncreaseMaxWidth;
|
||||
this.forcePowerOfTwo = forcePowerOfTwo;
|
||||
this.forceSquare = forceSquare;
|
||||
this.disposeTextures = disposeTextures;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -124,8 +128,12 @@ namespace MLEM.Data {
|
|||
this.LastPackTime = stopwatch.Elapsed;
|
||||
|
||||
// invoke callbacks
|
||||
foreach (var request in this.textures)
|
||||
foreach (var request in this.textures) {
|
||||
request.Result.Invoke(new TextureRegion(this.PackedTexture, request.PackedArea));
|
||||
if (this.disposeTextures)
|
||||
request.Texture.Texture.Dispose();
|
||||
}
|
||||
|
||||
this.textures.Clear();
|
||||
}
|
||||
|
||||
|
|
|
@ -8,18 +8,21 @@ namespace Tests {
|
|||
public class TexturePackerTests {
|
||||
|
||||
private Texture2D testTexture;
|
||||
private Texture2D disposedTestTexture;
|
||||
private TestGame game;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp() {
|
||||
this.game = TestGame.Create();
|
||||
this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256);
|
||||
this.disposedTestTexture = new Texture2D(this.game.GraphicsDevice, 16, 16);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown() {
|
||||
this.game?.Dispose();
|
||||
this.testTexture?.Dispose();
|
||||
this.disposedTestTexture?.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -37,6 +40,16 @@ namespace Tests {
|
|||
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]
|
||||
public void TestBounds() {
|
||||
// test forced max width
|
||||
|
|
Loading…
Reference in a new issue