add support for loading a texture atlas from a JSON file
This commit is contained in:
parent
46e0c96757
commit
2eaea03dba
|
@ -189,6 +189,9 @@
|
||||||
<Compile Include="Graphics\Atlas\AutoGridTextureAtlas.cs" />
|
<Compile Include="Graphics\Atlas\AutoGridTextureAtlas.cs" />
|
||||||
<Compile Include="Graphics\Atlas\TextureAtlasAnimator.cs" />
|
<Compile Include="Graphics\Atlas\TextureAtlasAnimator.cs" />
|
||||||
<Compile Include="Graphics\Atlas\TextureAtlasTileAnimation.cs" />
|
<Compile Include="Graphics\Atlas\TextureAtlasTileAnimation.cs" />
|
||||||
|
<Compile Include="Graphics\Atlas\JsonTextureAtlasDefinition.cs" />
|
||||||
|
<Compile Include="Graphics\Atlas\JsonTextureAtlasTile.cs" />
|
||||||
|
<Compile Include="Graphics\Atlas\TextureAtlasLoader.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Blarg.GameFramework.Graphics.Atlas
|
||||||
|
{
|
||||||
|
public class JsonTextureAtlasDefinition
|
||||||
|
{
|
||||||
|
public string Texture;
|
||||||
|
public List<JsonTextureAtlasTile> Tiles;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
19
Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasTile.cs
Normal file
19
Blarg.GameFramework/Graphics/Atlas/JsonTextureAtlasTile.cs
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
49
Blarg.GameFramework/Graphics/Atlas/TextureAtlasLoader.cs
Normal file
49
Blarg.GameFramework/Graphics/Atlas/TextureAtlasLoader.cs
Normal file
|
@ -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<JsonTextureAtlasDefinition>(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<ContentManager>();
|
||||||
|
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<Texture>(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in a new issue