From 2eaea03dbaa7bbca65cea61fcc5c0b3e7739184f Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 25 Aug 2013 14:28:11 -0400 Subject: [PATCH] add support for loading a texture atlas from a JSON file --- .../Blarg.GameFramework.csproj | 3 ++ .../Atlas/JsonTextureAtlasDefinition.cs | 13 +++++ .../Graphics/Atlas/JsonTextureAtlasTile.cs | 19 +++++++ .../Graphics/Atlas/TextureAtlasLoader.cs | 49 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasDefinition.cs create mode 100644 Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasTile.cs create mode 100644 Blarg.GameFramework/Graphics/Atlas/TextureAtlasLoader.cs diff --git a/Blarg.GameFramework/Blarg.GameFramework.csproj b/Blarg.GameFramework/Blarg.GameFramework.csproj index d2fce3e..e190414 100644 --- a/Blarg.GameFramework/Blarg.GameFramework.csproj +++ b/Blarg.GameFramework/Blarg.GameFramework.csproj @@ -189,6 +189,9 @@ + + + diff --git a/Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasDefinition.cs b/Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasDefinition.cs new file mode 100644 index 0000000..ad10d0f --- /dev/null +++ b/Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasDefinition.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; + +namespace Blarg.GameFramework.Graphics.Atlas +{ + public class JsonTextureAtlasDefinition + { + public string Texture; + public List Tiles; + + } +} + diff --git a/Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasTile.cs b/Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasTile.cs new file mode 100644 index 0000000..a2c435d --- /dev/null +++ b/Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasTile.cs @@ -0,0 +1,19 @@ +using System; + +namespace Blarg.GameFramework.Graphics.Atlas +{ + public class JsonTextureAtlasTile + { + public bool Autogrid; + public int X; + public int Y; + public int Width; + public int Height; + public int NumTilesX; + public int NumTilesY; + public int TileWidth; + public int TileHeight; + public int Border; + } +} + diff --git a/Blarg.GameFramework/Graphics/Atlas/TextureAtlasLoader.cs b/Blarg.GameFramework/Graphics/Atlas/TextureAtlasLoader.cs new file mode 100644 index 0000000..cea896e --- /dev/null +++ b/Blarg.GameFramework/Graphics/Atlas/TextureAtlasLoader.cs @@ -0,0 +1,49 @@ +using System; +using System.IO; +using Newtonsoft.Json; +using Blarg.GameFramework.Content; +using Blarg.GameFramework.IO; + +namespace Blarg.GameFramework.Graphics.Atlas +{ + public static class TextureAtlasLoader + { + public static TextureAtlas Load(Stream file, string texturePath = null) + { + if (file == null) + throw new ArgumentNullException("file"); + + var reader = new StreamReader(file); + + var definition = JsonConvert.DeserializeObject(reader.ReadToEnd()); + + if (definition.Texture == null) + throw new Exception("No texture file specified."); + if (definition.Tiles == null || definition.Tiles.Count == 0) + throw new Exception("No tiles defined."); + + var contentManager = Framework.Services.Get(); + if (contentManager == null) + throw new InvalidOperationException("Cannot find a ContentManager object."); + + string textureFile = definition.Texture; + if (!String.IsNullOrEmpty(texturePath)) + textureFile = texturePath + textureFile; + + var texture = contentManager.Get(textureFile); + var atlas = new CustomTextureAtlas(texture); + + for (int i = 0; i < definition.Tiles.Count; ++i) { + var tile = definition.Tiles[i]; + // TODO: parameter value error checking + if (tile.Autogrid) + atlas.AddGrid(tile.X, tile.Y, tile.TileWidth, tile.TileHeight, tile.NumTilesX, tile.NumTilesY, tile.Border); + else + atlas.Add(tile.X, tile.Y, tile.Width, tile.Height); + } + + return atlas; + } + } +} +