2022-04-13 03:06:14 +00:00
|
|
|
using System;
|
2023-12-15 18:46:43 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2022-04-13 03:06:14 +00:00
|
|
|
using WellspringCS;
|
|
|
|
|
|
|
|
namespace MoonWorks.Graphics.Font
|
|
|
|
{
|
2023-12-15 18:46:43 +00:00
|
|
|
public unsafe class TextBatch : GraphicsResource
|
2022-04-13 03:06:14 +00:00
|
|
|
{
|
2023-12-15 18:46:43 +00:00
|
|
|
public const int INITIAL_CHAR_COUNT = 64;
|
|
|
|
public const int INITIAL_VERTEX_COUNT = INITIAL_CHAR_COUNT * 4;
|
|
|
|
public const int INITIAL_INDEX_COUNT = INITIAL_CHAR_COUNT * 6;
|
|
|
|
|
2022-04-13 03:06:14 +00:00
|
|
|
private GraphicsDevice GraphicsDevice { get; }
|
|
|
|
public IntPtr Handle { get; }
|
|
|
|
|
|
|
|
public Buffer VertexBuffer { get; protected set; } = null;
|
|
|
|
public Buffer IndexBuffer { get; protected set; } = null;
|
|
|
|
public uint PrimitiveCount { get; protected set; }
|
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
public Font CurrentFont { get; private set; }
|
|
|
|
|
|
|
|
private byte* StringBytes;
|
|
|
|
private int StringBytesLength;
|
2022-06-30 20:25:52 +00:00
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
public TextBatch(GraphicsDevice device) : base(device)
|
2022-04-13 03:06:14 +00:00
|
|
|
{
|
2023-12-15 18:46:43 +00:00
|
|
|
GraphicsDevice = device;
|
2022-04-13 03:06:14 +00:00
|
|
|
Handle = Wellspring.Wellspring_CreateTextBatch();
|
2023-12-15 18:46:43 +00:00
|
|
|
|
|
|
|
StringBytesLength = 128;
|
|
|
|
StringBytes = (byte*) NativeMemory.Alloc((nuint) StringBytesLength);
|
|
|
|
|
|
|
|
VertexBuffer = Buffer.Create<Vertex>(GraphicsDevice, BufferUsageFlags.Vertex, INITIAL_VERTEX_COUNT);
|
|
|
|
IndexBuffer = Buffer.Create<uint>(GraphicsDevice, BufferUsageFlags.Index, INITIAL_INDEX_COUNT);
|
2022-04-13 03:06:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
// Call this to initialize or reset the batch.
|
|
|
|
public void Start(Font font)
|
2022-04-13 03:06:14 +00:00
|
|
|
{
|
2023-12-15 18:46:43 +00:00
|
|
|
Wellspring.Wellspring_StartTextBatch(Handle, font.Handle);
|
|
|
|
CurrentFont = font;
|
2022-04-13 03:06:14 +00:00
|
|
|
PrimitiveCount = 0;
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
// Add text with size and color to the batch
|
|
|
|
public unsafe bool Add(
|
2022-04-13 22:10:23 +00:00
|
|
|
string text,
|
2023-12-15 18:46:43 +00:00
|
|
|
int pixelSize,
|
2022-04-13 22:10:23 +00:00
|
|
|
Color color,
|
|
|
|
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left,
|
|
|
|
VerticalAlignment verticalAlignment = VerticalAlignment.Baseline
|
|
|
|
) {
|
2022-06-30 20:24:51 +00:00
|
|
|
var byteCount = System.Text.Encoding.UTF8.GetByteCount(text);
|
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
if (StringBytesLength < byteCount)
|
2022-06-30 20:24:51 +00:00
|
|
|
{
|
2023-12-15 18:46:43 +00:00
|
|
|
StringBytes = (byte*) NativeMemory.Realloc(StringBytes, (nuint) byteCount);
|
2022-06-30 20:24:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 03:06:14 +00:00
|
|
|
fixed (char* chars = text)
|
|
|
|
{
|
2023-12-15 18:46:43 +00:00
|
|
|
System.Text.Encoding.UTF8.GetBytes(chars, text.Length, StringBytes, byteCount);
|
2022-04-13 03:06:14 +00:00
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
var result = Wellspring.Wellspring_AddToTextBatch(
|
2022-04-13 03:06:14 +00:00
|
|
|
Handle,
|
2023-12-15 18:46:43 +00:00
|
|
|
pixelSize,
|
2022-04-13 03:06:14 +00:00
|
|
|
new Wellspring.Color { R = color.R, G = color.G, B = color.B, A = color.A },
|
2022-04-13 22:10:23 +00:00
|
|
|
(Wellspring.HorizontalAlignment) horizontalAlignment,
|
|
|
|
(Wellspring.VerticalAlignment) verticalAlignment,
|
2023-12-15 18:46:43 +00:00
|
|
|
(IntPtr) StringBytes,
|
2022-04-13 03:06:14 +00:00
|
|
|
(uint) byteCount
|
|
|
|
);
|
|
|
|
|
|
|
|
if (result == 0)
|
|
|
|
{
|
2023-12-15 18:46:43 +00:00
|
|
|
Logger.LogWarn("Could not decode string: " + text);
|
|
|
|
return false;
|
2022-04-13 03:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-15 18:46:43 +00:00
|
|
|
|
|
|
|
return true;
|
2022-04-13 03:06:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
// Call this after you have made all the Add calls you want, but before beginning a render pass.
|
2022-04-13 03:06:14 +00:00
|
|
|
public unsafe void UploadBufferData(CommandBuffer commandBuffer)
|
|
|
|
{
|
|
|
|
Wellspring.Wellspring_GetBufferData(
|
|
|
|
Handle,
|
2022-06-30 20:25:52 +00:00
|
|
|
out uint vertexCount,
|
2022-04-13 03:06:14 +00:00
|
|
|
out IntPtr vertexDataPointer,
|
|
|
|
out uint vertexDataLengthInBytes,
|
|
|
|
out IntPtr indexDataPointer,
|
|
|
|
out uint indexDataLengthInBytes
|
|
|
|
);
|
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
if (VertexBuffer.Size < vertexDataLengthInBytes)
|
2022-04-13 03:06:14 +00:00
|
|
|
{
|
|
|
|
VertexBuffer.Dispose();
|
|
|
|
VertexBuffer = new Buffer(GraphicsDevice, BufferUsageFlags.Vertex, vertexDataLengthInBytes);
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
if (IndexBuffer.Size < indexDataLengthInBytes)
|
2022-04-13 03:06:14 +00:00
|
|
|
{
|
|
|
|
IndexBuffer.Dispose();
|
2024-01-28 01:34:26 +00:00
|
|
|
IndexBuffer = new Buffer(GraphicsDevice, BufferUsageFlags.Index, vertexDataLengthInBytes);
|
2022-04-13 03:06:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 00:22:24 +00:00
|
|
|
if (vertexDataLengthInBytes > 0 && indexDataLengthInBytes > 0)
|
|
|
|
{
|
|
|
|
commandBuffer.SetBufferData(VertexBuffer, vertexDataPointer, 0, vertexDataLengthInBytes);
|
|
|
|
commandBuffer.SetBufferData(IndexBuffer, indexDataPointer, 0, indexDataLengthInBytes);
|
|
|
|
}
|
2022-06-30 20:25:52 +00:00
|
|
|
|
2023-12-15 18:46:43 +00:00
|
|
|
PrimitiveCount = vertexCount / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call this AFTER binding your text pipeline!
|
|
|
|
public void Render(CommandBuffer commandBuffer, Math.Float.Matrix4x4 transformMatrix)
|
|
|
|
{
|
|
|
|
commandBuffer.BindFragmentSamplers(new TextureSamplerBinding(
|
|
|
|
CurrentFont.Texture,
|
|
|
|
GraphicsDevice.LinearSampler
|
|
|
|
));
|
|
|
|
commandBuffer.BindVertexBuffers(VertexBuffer);
|
|
|
|
commandBuffer.BindIndexBuffer(IndexBuffer, IndexElementSize.ThirtyTwo);
|
|
|
|
commandBuffer.DrawIndexedPrimitives(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
PrimitiveCount,
|
|
|
|
commandBuffer.PushVertexShaderUniforms(transformMatrix),
|
|
|
|
commandBuffer.PushFragmentShaderUniforms(CurrentFont.DistanceRange)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!IsDisposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
VertexBuffer.Dispose();
|
|
|
|
IndexBuffer.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
NativeMemory.Free(StringBytes);
|
|
|
|
Wellspring.Wellspring_DestroyTextBatch(Handle);
|
|
|
|
}
|
|
|
|
base.Dispose(disposing);
|
2022-04-13 03:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|