implemented proper disposal

This commit is contained in:
Harrison Deng 2019-01-14 21:06:00 -06:00
parent 2a2a7cba49
commit 30c96f1fff

View File

@ -2,16 +2,17 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace RecrownedAthenaeum.DataTypes namespace RecrownedAthenaeum.DataTypes
{ {
/// <summary> /// <summary>
/// Holds information about an image file that contains various textures in various regions in the file. /// Holds information about an image file that contains various textures in various regions in the file.
/// </summary> /// </summary>
public class TextureAtlas public class TextureAtlas : IDisposable
{ {
private Texture2D texture; private Texture2D texture;
private bool disposed;
private Dictionary<string, TextureAtlasRegion> dictionaryOfRegions = new Dictionary<string, TextureAtlasRegion>(); private Dictionary<string, TextureAtlasRegion> dictionaryOfRegions = new Dictionary<string, TextureAtlasRegion>();
/// <summary> /// <summary>
@ -71,6 +72,40 @@ namespace RecrownedAthenaeum.DataTypes
return dictionaryOfRegions[name].AsTexture2D(graphicsDevice); return dictionaryOfRegions[name].AsTexture2D(graphicsDevice);
} }
/// <summary>
/// Disposes unmanaged resources for the texture atlas.
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Overridable disposal method.
/// </summary>
/// <param name="disposing">Only true if user calls <see cref="Dispose()"/></param>
public virtual void Dispose(bool disposing)
{
disposed = true;
if (!disposed && disposing)
{
texture.Dispose();
for (int i = 0; i < dictionaryOfRegions.Count; i++)
{
dictionaryOfRegions.ElementAt(i).Value.Dispose();
}
dictionaryOfRegions.Clear();
}
}
/// <summary>
/// Destructor.
/// </summary>
~TextureAtlas()
{
Dispose(false);
}
/// <summary> /// <summary>
/// A region of a <see cref="TextureAtlas"/>. /// A region of a <see cref="TextureAtlas"/>.
/// </summary> /// </summary>
@ -80,6 +115,7 @@ namespace RecrownedAthenaeum.DataTypes
/// The name of the region. Mostly used to be refered to within the context of a <see cref="TextureAtlas"/>. /// The name of the region. Mostly used to be refered to within the context of a <see cref="TextureAtlas"/>.
/// </summary> /// </summary>
public readonly string name; public readonly string name;
/// <summary> /// <summary>
/// The location and dimensions of where the original texture resides on the texture representing the atlas. /// The location and dimensions of where the original texture resides on the texture representing the atlas.
/// </summary> /// </summary>