Update TestGame, load fonts from memory.

This commit is contained in:
2024-01-21 00:25:23 +01:00
parent 3e5e010527
commit fb5033f9a7
7 changed files with 66 additions and 72 deletions

View File

@@ -143,35 +143,6 @@ namespace DaggerFramework.Rendering
return _texturePool.Count - 1;
}
public unsafe override int LoadFont(Font font)
{
// TODO: screw it, let's just load the font directly from the path. I give up.
// Raylib_cs.Font fontRay;
// string ext = "ttf";
// byte[] extBytes = new byte[Encoding.Default.GetByteCount(ext) + 1];
// Encoding.Default.GetBytes(ext, extBytes);
// int fontChars = 0;
// unsafe
// {
// fixed (byte* extP = extBytes)
// {
// fixed (byte* bufferP = font.Buffer)
// {
// fontRay = Raylib.LoadFontFromMemory((sbyte*)extP, bufferP, (int)font.BufferSize, font.Size, null, 95);
// }
// }
// }
var fontRay = Raylib.LoadFont(font.Path);
Raylib.SetTextureFilter(fontRay.texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
_fontPool.Add(fontRay);
return _fontPool.Count - 1;
}
public override void DrawTexture(int id, Color tint)
{
Raylib.DrawTextureV(_texturePool[id], _position, DaggerColorToRaylibColor(tint));
@@ -233,10 +204,15 @@ namespace DaggerFramework.Rendering
return new Raylib_cs.Color { r = (byte)Math.Round(color.R * 255f), g = (byte)Math.Round(color.G * 255f), b = (byte)Math.Round(color.B * 255f), a = (byte)Math.Round(color.A * 255f) };
}
public override void DrawText(int fontId, string text, int fontSize, Color color)
public override void DrawText(Font font, string text, Color color)
{
var font = _fontPool[fontId];
Raylib.DrawTextPro(font, text, _position, Vector2.Zero, _rotation, fontSize, 0.0f, DaggerColorToRaylibColor(color));
if (font.FontHandle == -1)
{
LoadFont(font);
}
var rayFont = _fontPool[font.FontHandle];
Raylib.DrawTextPro(rayFont, text, _position, Vector2.Zero, _rotation, font.Size, 0.0f, DaggerColorToRaylibColor(color));
}
protected override int GetMonitorWidth(int monitorId)
@@ -254,6 +230,33 @@ namespace DaggerFramework.Rendering
return Raylib.GetCurrentMonitor();
}
private unsafe void LoadFont(Font font)
{
Raylib_cs.Font fontRay;
string ext = ".ttf";
byte[] extBytes = new byte[Encoding.Default.GetByteCount(ext) + 1];
Encoding.Default.GetBytes(ext, extBytes);
int fontChars = 0;
unsafe
{
fixed (byte* extP = extBytes)
{
fixed (byte* bufferP = font.Buffer)
{
fontRay = Raylib.LoadFontFromMemory((sbyte*)extP, bufferP, (int)font.BufferSize, font.Size, null, 95);
}
}
}
Raylib.SetTextureFilter(fontRay.texture, TextureFilter.TEXTURE_FILTER_TRILINEAR);
_fontPool.Add(fontRay);
font.FontHandle = _fontPool.Count - 1;
}
private List<Texture2D> _texturePool = new();
private List<Raylib_cs.Font> _fontPool = new();

View File

@@ -86,8 +86,6 @@ namespace DaggerFramework.Rendering
/// <returns>A texture handler on the GPU.</returns>
public abstract int LoadTexture(Texture2d texture);
public abstract int LoadFont(Font font);
/// <summary>
/// Sets transforms for the next draw operation.
/// </summary>
@@ -128,7 +126,7 @@ namespace DaggerFramework.Rendering
/// <param name="color">Color of the text.</param>
public abstract void DrawSdfText(string text, int fontSize, Color color);
public abstract void DrawText(int fontId, string text, int fontSize, Color color);
public abstract void DrawText(Font font, string text, Color color);
/// <summary>
/// Draws the texture.

View File

@@ -161,16 +161,6 @@ namespace DaggerFramework.Rendering
throw new NotImplementedException();
}
public override int LoadFont(Font font)
{
throw new NotImplementedException();
}
public override void DrawText(int fontId, string text, int fontSize, Color color)
{
throw new NotImplementedException();
}
public override void BeginCamera2d(Vector2 offset, Vector2 target, float rotation, float zoom)
{
throw new NotImplementedException();
@@ -201,6 +191,12 @@ namespace DaggerFramework.Rendering
throw new NotImplementedException();
}
public override void DrawText(Font font, string text, Color color)
{
throw new NotImplementedException();
}
private GL _gl;
private Glfw _glfw;
private unsafe WindowHandle* _windowHandle;

View File

@@ -2,7 +2,11 @@ namespace DaggerFramework;
public class Font : Resource
{
public int Size = 16;
/// <summary>
/// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1.
/// </summary>
internal int FontHandle { get; set; } = -1;
public int Size { get; set; } = 16;
public Font(string path, byte[] buffer) : base(path, buffer)
{
}

View File

@@ -6,7 +6,6 @@ namespace DaggerFramework.SceneGraph
public class Text2d : Drawable2d
{
public string Text { get => _text; set => _text = value; }
public int FontSize { get => _fontSize; set => _fontSize = value; }
public Color FontColor { get => _fontColor; set => _fontColor = value; }
public Font Font
{
@@ -17,26 +16,24 @@ namespace DaggerFramework.SceneGraph
}
}
public Text2d(Font font)
{
_font = font;
}
public override void OnDraw(Renderer renderer)
{
if (_isDirty)
{
_fontHandle = renderer.LoadFont(_font);
_isDirty = false;
}
if (_font == null)
{
renderer.DrawDebugText(_text, _fontSize, _fontColor);
renderer.DrawDebugText(_text, 20, _fontColor);
}
else
{
renderer.DrawText(_fontHandle, _text, _fontSize, _fontColor);
renderer.DrawText(_font, _text, _fontColor);
}
}
private string _text = string.Empty;
private int _fontSize = 16;
private Color _fontColor = Color.White;
private Font _font;
private int _fontHandle;

View File

@@ -9,7 +9,6 @@ public class TextLabel : UIElement
{
get => _font; set
{
_isDirty = true;
_font = value;
}
}
@@ -17,11 +16,6 @@ public class TextLabel : UIElement
public Color FontColor { get; set; } = Color.White;
protected override void OnRender(Renderer renderer)
{
if (_isDirty && _font != null)
{
_fontHandle = renderer.LoadFont(_font);
_isDirty = false;
}
if (_font == null)
{
@@ -29,11 +23,9 @@ public class TextLabel : UIElement
}
else
{
renderer.DrawText(_fontHandle, Text, FontSize, FontColor);
renderer.DrawText(_font, Text, FontColor);
}
}
private Font? _font;
private int _fontHandle;
private bool _isDirty;
}