diff --git a/Recrowned-Athenaeum/DataTypes/NinePatch.cs b/Recrowned-Athenaeum/DataTypes/NinePatch.cs index 6331aee..0373113 100644 --- a/Recrowned-Athenaeum/DataTypes/NinePatch.cs +++ b/Recrowned-Athenaeum/DataTypes/NinePatch.cs @@ -5,13 +5,18 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Serialization; namespace RecrownedAthenaeum.DataTypes { public class NinePatch { + [XmlIgnore] public Color color; + + [XmlIgnore] readonly Texture2D texture; + readonly int a, b, c, d; /// diff --git a/Recrowned-Athenaeum/DataTypes/TextureAtlas.cs b/Recrowned-Athenaeum/DataTypes/TextureAtlas.cs index 367337d..4ad5501 100644 --- a/Recrowned-Athenaeum/DataTypes/TextureAtlas.cs +++ b/Recrowned-Athenaeum/DataTypes/TextureAtlas.cs @@ -1,22 +1,57 @@ -using Recrowned_Athenaeum.DataTypes; +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;23 +using System.Threading.Tasks; +using System.Xml.Serialization; namespace RecrownedAthenaeum.DataTypes { public class TextureAtlas { + string assetName; private TextureAtlasRegion[] textureRegions; - public TextureAtlas(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) { - this.textureRegions = textureRegions; + 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); + } } } diff --git a/Recrowned-Athenaeum/DataTypes/TextureAtlasRegion.cs b/Recrowned-Athenaeum/DataTypes/TextureAtlasRegion.cs index 521c5b1..1e6e9fe 100644 --- a/Recrowned-Athenaeum/DataTypes/TextureAtlasRegion.cs +++ b/Recrowned-Athenaeum/DataTypes/TextureAtlasRegion.cs @@ -1,19 +1,51 @@ using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using RecrownedAthenaeum.DataTypes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Serialization; namespace Recrowned_Athenaeum.DataTypes { - public struct TextureAtlasRegion + public struct TextureAtlasRegion : IComparable, IDisposable { - string name; - Rectangle bounds; + public string name; + Rectangle sourceRectangle; Vector2 origin; - bool ninePatch; - int a, b, c, d; + Color color; + float rotation; + NinePatch ninepatch; + [XmlIgnore] + Texture2D regionTexture; + public void Draw(SpriteBatch batch, Rectangle destination, Texture2D regionOfTexture) + { + batch.Draw(regionOfTexture, destination, sourceRectangle, color); + } + + public Texture2D AsTexture2D(GraphicsDevice graphicsDevice, Texture2D textureAtlas) + { + if (regionTexture == null) + { + Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height]; + regionTexture = new Texture2D(graphicsDevice, sourceRectangle.Width, sourceRectangle.Height); + textureAtlas.GetData(0, sourceRectangle, data, 0, sourceRectangle.Width * sourceRectangle.Height); + regionTexture.SetData(data); + } + return regionTexture; + } + + public int CompareTo(TextureAtlasRegion other) + { + return name.CompareTo(other); + } + + public void Dispose() + { + regionTexture?.Dispose(); + } } }