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; 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) public override void DrawTexture(int id, Color tint)
{ {
Raylib.DrawTextureV(_texturePool[id], _position, DaggerColorToRaylibColor(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) }; 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]; if (font.FontHandle == -1)
Raylib.DrawTextPro(font, text, _position, Vector2.Zero, _rotation, fontSize, 0.0f, DaggerColorToRaylibColor(color)); {
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) protected override int GetMonitorWidth(int monitorId)
@@ -254,6 +230,33 @@ namespace DaggerFramework.Rendering
return Raylib.GetCurrentMonitor(); 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<Texture2D> _texturePool = new();
private List<Raylib_cs.Font> _fontPool = 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> /// <returns>A texture handler on the GPU.</returns>
public abstract int LoadTexture(Texture2d texture); public abstract int LoadTexture(Texture2d texture);
public abstract int LoadFont(Font font);
/// <summary> /// <summary>
/// Sets transforms for the next draw operation. /// Sets transforms for the next draw operation.
/// </summary> /// </summary>
@@ -128,7 +126,7 @@ namespace DaggerFramework.Rendering
/// <param name="color">Color of the text.</param> /// <param name="color">Color of the text.</param>
public abstract void DrawSdfText(string text, int fontSize, Color color); 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> /// <summary>
/// Draws the texture. /// Draws the texture.

View File

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

View File

@@ -2,7 +2,11 @@ namespace DaggerFramework;
public class Font : Resource 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) public Font(string path, byte[] buffer) : base(path, buffer)
{ {
} }

View File

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

View File

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

View File

@@ -51,6 +51,11 @@ public class TestGame : Game
if (_resourceManager.TryLoad<Font>("inter_regular", "fonts/Inter-Regular.ttf")) if (_resourceManager.TryLoad<Font>("inter_regular", "fonts/Inter-Regular.ttf"))
{ {
_resourceManager.TryGetResource("inter_regular", out _font); _resourceManager.TryGetResource("inter_regular", out _font);
if (_font is not null)
{
_font.Size = 20;
}
} }
} }
@@ -59,12 +64,8 @@ public class TestGame : Game
_inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); _inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) });
_inputHandler.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) }); _inputHandler.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) });
_fontHandle = _renderer.LoadFont(_font);
_scene.AddLayer("World", _worldLayer); _scene.AddLayer("World", _worldLayer);
// _scene.AddLayer("UI", _uiLayer); _worldLayer.AddEntity(new World());
// _worldLayer.AddEntity(new World());
_worldLayer.AddEntity(new TestPlayer()); _worldLayer.AddEntity(new TestPlayer());
_scene.Start(); _scene.Start();
@@ -79,7 +80,11 @@ public class TestGame : Game
double frameTimeMs = _renderer.FrameTime * 1000f; double frameTimeMs = _renderer.FrameTime * 1000f;
_renderer.SetTransform(Vector2.One * 32f, Vector2.Zero, 0f); _renderer.SetTransform(Vector2.One * 32f, Vector2.Zero, 0f);
_renderer.DrawText(_fontHandle, $"{frameTimeMs:0.0} ms", 20, new Color(1f, 1f, 1f, 0.5f));
if (_font is not null)
{
_renderer.DrawText(_font, $"{frameTimeMs:0.0} ms", new Color(1f, 1f, 1f, 0.5f));
}
} }
} }
@@ -87,7 +92,6 @@ public class TestGame : Game
private ResourceManager _resourceManager = new(); private ResourceManager _resourceManager = new();
private Sound? _testSound; private Sound? _testSound;
private Font? _font; private Font? _font;
private int _fontHandle;
private FmodAudioBackend _audioBackend; private FmodAudioBackend _audioBackend;
private InputHandler _inputHandler; private InputHandler _inputHandler;
private Scene _scene; private Scene _scene;