added draw and texture obtaining functions to atlas and regions.

This commit is contained in:
Harrison Deng 2018-12-04 15:46:44 -06:00
parent b4ddd73f67
commit 52fa550ced
3 changed files with 82 additions and 10 deletions

View File

@ -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;
/// <summary>

View File

@ -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<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)
{
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);
}
}
}

View File

@ -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<TextureAtlasRegion>, 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();
}
}
}