recrownedathenaeum/Recrowned-Athenaeum/DataTypes/TextureAtlas.cs

58 lines
1.8 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Recrowned_Athenaeum.DataTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace RecrownedAthenaeum.DataTypes
{
public class TextureAtlas
{
string assetName;
private TextureAtlasRegion[] textureRegions;
[XmlIgnore]
private Texture2D texture;
[XmlIgnore]
private Dictionary<string, TextureAtlasRegion> dictionaryOfRegions = new Dictionary<string, TextureAtlasRegion>();
/// <summary>
/// Should be called after serialization to prepare the data.
/// More specifically, loads the texture map, as well as load the regions into a dictionary.
/// <param name="texture">the texture for the atlas.</param>
/// </summary>
public void Initialize(Texture2D texture)
{
SetTextureRegions(texture, textureRegions);
}
public void SetTextureRegions(Texture2D texture, TextureAtlasRegion[] regions)
{
this.texture = texture;
Array.Sort(regions);
this.textureRegions = regions;
dictionaryOfRegions.Clear();
foreach (TextureAtlasRegion region in textureRegions)
{
dictionaryOfRegions.Add(region.name, region);
}
}
public void Draw(SpriteBatch batch, Rectangle destinationRectangle, string textureRegionName)
{
dictionaryOfRegions[textureRegionName].Draw(batch, destinationRectangle, );
}
public Texture2D TextureOfRegion(string name, GraphicsDevice graphicsDevice)
{
return dictionaryOfRegions[name].AsTexture2D(graphicsDevice, texture);
}
}
}