diff --git a/lib/WellspringCS b/lib/WellspringCS index 38a0d66e..8852da67 160000 --- a/lib/WellspringCS +++ b/lib/WellspringCS @@ -1 +1 @@ -Subproject commit 38a0d66e78f592dad4f6e0030aa1c7aceafc6b04 +Subproject commit 8852da6765c035e8c60a1987a644979bc617cdc3 diff --git a/src/Graphics/Font/Enums.cs b/src/Graphics/Font/Enums.cs new file mode 100644 index 00000000..9137f2fa --- /dev/null +++ b/src/Graphics/Font/Enums.cs @@ -0,0 +1,17 @@ +namespace MoonWorks.Graphics.Font +{ + public enum HorizontalAlignment + { + Left, + Center, + Right + } + + public enum VerticalAlignment + { + Baseline, + Top, + Middle, + Bottom + } +} diff --git a/src/Graphics/Font/Font.cs b/src/Graphics/Font/Font.cs new file mode 100644 index 00000000..95e8780e --- /dev/null +++ b/src/Graphics/Font/Font.cs @@ -0,0 +1,44 @@ +using System; +using System.IO; +using WellspringCS; + +namespace MoonWorks.Graphics.Font +{ + public class Font : IDisposable + { + public IntPtr Handle { get; } + + private bool IsDisposed; + + public unsafe Font(string path) + { + var bytes = File.ReadAllBytes(path); + fixed (byte* pByte = &bytes[0]) + { + Handle = Wellspring.Wellspring_CreateFont((IntPtr) pByte, (uint) bytes.Length); + } + } + + protected virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + Wellspring.Wellspring_DestroyFont(Handle); + IsDisposed = true; + } + } + + ~Font() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: false); + } + + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } +} diff --git a/src/Graphics/Font/Packer.cs b/src/Graphics/Font/Packer.cs index 20052019..65ff964d 100644 --- a/src/Graphics/Font/Packer.cs +++ b/src/Graphics/Font/Packer.cs @@ -10,16 +10,14 @@ namespace MoonWorks.Graphics.Font public IntPtr Handle { get; } public Texture Texture { get; } + public Font Font { get; } + private bool IsDisposed; - public unsafe Packer(GraphicsDevice graphicsDevice, string path, uint textureWidth, uint textureHeight, uint padding = 1) + public unsafe Packer(GraphicsDevice graphicsDevice, Font font, float fontSize, uint textureWidth, uint textureHeight, uint padding = 1) { - var bytes = File.ReadAllBytes(path); - fixed (byte* pByte = &bytes[0]) - { - Handle = Wellspring.Wellspring_CreatePacker((IntPtr) pByte, (uint) bytes.Length, textureWidth, textureHeight, 0, padding); - } - + Font = font; + Handle = Wellspring.Wellspring_CreatePacker(Font.Handle, fontSize, textureWidth, textureHeight, 0, padding); Texture = Texture.CreateTexture2D(graphicsDevice, textureWidth, textureHeight, TextureFormat.R8, TextureUsageFlags.Sampler); } diff --git a/src/Graphics/Font/Structs.cs b/src/Graphics/Font/Structs.cs index 37082f12..83e7fc77 100644 --- a/src/Graphics/Font/Structs.cs +++ b/src/Graphics/Font/Structs.cs @@ -6,7 +6,6 @@ namespace MoonWorks.Graphics.Font [StructLayout(LayoutKind.Sequential)] public struct FontRange { - public uint FontSize; public uint FirstCodepoint; public uint NumChars; public byte OversampleH; diff --git a/src/Graphics/Font/TextBatch.cs b/src/Graphics/Font/TextBatch.cs index ce246cd2..1627bcf8 100644 --- a/src/Graphics/Font/TextBatch.cs +++ b/src/Graphics/Font/TextBatch.cs @@ -26,8 +26,15 @@ namespace MoonWorks.Graphics.Font PrimitiveCount = 0; } - public unsafe void Draw(float x, float y, float depth, Color color, string text) - { + public unsafe void Draw( + string text, + float x, + float y, + float depth, + Color color, + HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left, + VerticalAlignment verticalAlignment = VerticalAlignment.Baseline + ) { fixed (char* chars = text) { var byteCount = System.Text.Encoding.UTF8.GetByteCount(text); @@ -40,6 +47,8 @@ namespace MoonWorks.Graphics.Font y, depth, new Wellspring.Color { R = color.R, G = color.G, B = color.B, A = color.A }, + (Wellspring.HorizontalAlignment) horizontalAlignment, + (Wellspring.VerticalAlignment) verticalAlignment, (IntPtr) bytes, (uint) byteCount );