Font rendering update

main
cosmonaut 2022-04-13 15:10:23 -07:00
parent b49dc3720a
commit 65568ea234
6 changed files with 78 additions and 11 deletions

@ -1 +1 @@
Subproject commit 38a0d66e78f592dad4f6e0030aa1c7aceafc6b04 Subproject commit 8852da6765c035e8c60a1987a644979bc617cdc3

View File

@ -0,0 +1,17 @@
namespace MoonWorks.Graphics.Font
{
public enum HorizontalAlignment
{
Left,
Center,
Right
}
public enum VerticalAlignment
{
Baseline,
Top,
Middle,
Bottom
}
}

44
src/Graphics/Font/Font.cs Normal file
View File

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

View File

@ -10,16 +10,14 @@ namespace MoonWorks.Graphics.Font
public IntPtr Handle { get; } public IntPtr Handle { get; }
public Texture Texture { get; } public Texture Texture { get; }
public Font Font { get; }
private bool IsDisposed; 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); Font = font;
fixed (byte* pByte = &bytes[0]) Handle = Wellspring.Wellspring_CreatePacker(Font.Handle, fontSize, textureWidth, textureHeight, 0, padding);
{
Handle = Wellspring.Wellspring_CreatePacker((IntPtr) pByte, (uint) bytes.Length, textureWidth, textureHeight, 0, padding);
}
Texture = Texture.CreateTexture2D(graphicsDevice, textureWidth, textureHeight, TextureFormat.R8, TextureUsageFlags.Sampler); Texture = Texture.CreateTexture2D(graphicsDevice, textureWidth, textureHeight, TextureFormat.R8, TextureUsageFlags.Sampler);
} }

View File

@ -6,7 +6,6 @@ namespace MoonWorks.Graphics.Font
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct FontRange public struct FontRange
{ {
public uint FontSize;
public uint FirstCodepoint; public uint FirstCodepoint;
public uint NumChars; public uint NumChars;
public byte OversampleH; public byte OversampleH;

View File

@ -26,8 +26,15 @@ namespace MoonWorks.Graphics.Font
PrimitiveCount = 0; 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) fixed (char* chars = text)
{ {
var byteCount = System.Text.Encoding.UTF8.GetByteCount(text); var byteCount = System.Text.Encoding.UTF8.GetByteCount(text);
@ -40,6 +47,8 @@ namespace MoonWorks.Graphics.Font
y, y,
depth, depth,
new Wellspring.Color { R = color.R, G = color.G, B = color.B, A = color.A }, new Wellspring.Color { R = color.R, G = color.G, B = color.B, A = color.A },
(Wellspring.HorizontalAlignment) horizontalAlignment,
(Wellspring.VerticalAlignment) verticalAlignment,
(IntPtr) bytes, (IntPtr) bytes,
(uint) byteCount (uint) byteCount
); );