diff --git a/Data/AtlasAnimation.cs b/Data/AtlasAnimation.cs new file mode 100644 index 0000000..6fc75ff --- /dev/null +++ b/Data/AtlasAnimation.cs @@ -0,0 +1,14 @@ +namespace Kav +{ + public struct AtlasAnimation + { + public UVData[] Frames { get; } + public int Framerate { get; } + + public AtlasAnimation(UVData[] frames, int framerate) + { + Frames = frames; + Framerate = framerate; + } + } +} diff --git a/Data/TextureAtlas.cs b/Data/TextureAtlas.cs new file mode 100644 index 0000000..8d9097d --- /dev/null +++ b/Data/TextureAtlas.cs @@ -0,0 +1,96 @@ +using Kav.Data; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System.Collections.Generic; +using System.IO; + +namespace Kav +{ + public struct TextureAtlasSlice + { + public int X { get; } + public int Y { get; } + public int W { get; } + public int H { get; } + + public UVData UVData { get; } + + // for use with tiling + public Vector2 TiledUVOffset { get; } + + public TextureAtlasSlice(int x, int y, int w, int h, int totalW, int totalH) + { + X = x; + Y = y; + W = w; + H = h; + + UVData = new UVData(new Vector2(x, y), new Vector2(w, h), new Vector2(totalW, totalH)); + + TiledUVOffset = new Vector2(x / (float)totalW, y / (float)totalH); + } + } + + public class TextureAtlas + { + public Texture2D Texture { get; } + protected List Names { get; } = new List(); + protected Dictionary Slices { get; } = new Dictionary(); + + public TextureAtlas(GraphicsDevice graphicsDevice, FileInfo atlasMetadataFile) + { + var atlasData = CrunchAtlasReader.ReadTextureAtlas(atlasMetadataFile); + + var textureData = atlasData.Textures[0]; + + using var stream = File.OpenRead(Path.Combine(atlasMetadataFile.DirectoryName, textureData.Name + ".png")); + Texture = Texture2D.FromStream(graphicsDevice, stream); + + foreach (var slice in textureData.Images) + { + Names.Add(slice.N); + + Slices.Add( + slice.N, + new TextureAtlasSlice( + slice.X, + slice.Y, + slice.W, + slice.H, + Texture.Width, + Texture.Height + ) + ); + } + } + + public TextureAtlasSlice Lookup(string name) + { + return Slices[name]; + } + + public string Name(int index) + { + return Names[index]; + } + + public int Count() + { + return Names.Count; + } + } + + // Assumes all subimages are the same size + public class TiledTextureAtlas : TextureAtlas + { + public int NumRows { get; } + public int NumColumns { get; } + + public TiledTextureAtlas(GraphicsDevice graphicsDevice, FileInfo atlasMetadataFile) : base(graphicsDevice, atlasMetadataFile) + { + var subImageSlice = Slices[Names[0]]; + NumRows = Texture.Height / subImageSlice.H; + NumColumns = Texture.Width / subImageSlice.W; + } + } +} diff --git a/Data/UVData.cs b/Data/UVData.cs index 0295804..6c08ce6 100644 --- a/Data/UVData.cs +++ b/Data/UVData.cs @@ -1,6 +1,6 @@ using Microsoft.Xna.Framework; -namespace Kav.Data +namespace Kav { public struct UVData { diff --git a/Kav.Core.csproj b/Kav.Core.csproj index 676e7c2..9563ccb 100644 --- a/Kav.Core.csproj +++ b/Kav.Core.csproj @@ -2,6 +2,7 @@ netstandard2.0 + 8.0 Kav Evan Hemsley Evan Hemsley 2020 diff --git a/Kav.Framework.csproj b/Kav.Framework.csproj index 2263772..50ab6ca 100644 --- a/Kav.Framework.csproj +++ b/Kav.Framework.csproj @@ -2,6 +2,7 @@ netstandard2.0 + 8.0 Kav Evan Hemsley Evan Hemsley 2020 diff --git a/Loaders/CrunchAtlasReader.cs b/Loaders/CrunchAtlasReader.cs new file mode 100644 index 0000000..3f9b616 --- /dev/null +++ b/Loaders/CrunchAtlasReader.cs @@ -0,0 +1,18 @@ +using System.IO; +using System.Text.Json; + +namespace Kav +{ + public static class CrunchAtlasReader + { + static JsonSerializerOptions options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + public static CrunchTextureAtlasData ReadTextureAtlas(FileInfo file) + { + return JsonSerializer.Deserialize(File.ReadAllText(file.FullName), options); + } + } +} diff --git a/Loaders/CrunchTextureAtlasData.cs b/Loaders/CrunchTextureAtlasData.cs new file mode 100644 index 0000000..e4d61c8 --- /dev/null +++ b/Loaders/CrunchTextureAtlasData.cs @@ -0,0 +1,22 @@ +namespace Kav +{ + public struct CrunchTextureAtlasData + { + public CrunchTextureAtlasTextureData[] Textures { get; set; } + } + + public struct CrunchTextureAtlasTextureData + { + public string Name { get; set; } + public CrunchTextureAtlasImageData[] Images { get; set; } + } + + public struct CrunchTextureAtlasImageData + { + public string N { get; set; } + public int X { get; set; } + public int Y { get; set; } + public int W { get; set; } + public int H { get; set; } + } +} diff --git a/Renderer.cs b/Renderer.cs index 1ac47b0..3bf8bd2 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -125,7 +125,7 @@ namespace Kav ) { GraphicsDevice.SetRenderTarget(renderTarget); - GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; + GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;