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 dictionaryOfRegions = new Dictionary(); /// /// Should be called after serialization to prepare the data. /// More specifically, loads the texture map, as well as load the regions into a dictionary. /// the texture for the atlas. /// 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); } } }