diff --git a/DaggerFramework/Source/Rendering/RaylibRenderer.cs b/DaggerFramework/Source/Rendering/RaylibRenderer.cs index 6942535..b1bc61a 100644 --- a/DaggerFramework/Source/Rendering/RaylibRenderer.cs +++ b/DaggerFramework/Source/Rendering/RaylibRenderer.cs @@ -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 _texturePool = new(); private List _fontPool = new(); diff --git a/DaggerFramework/Source/Rendering/Renderer.cs b/DaggerFramework/Source/Rendering/Renderer.cs index f07392c..efa283c 100644 --- a/DaggerFramework/Source/Rendering/Renderer.cs +++ b/DaggerFramework/Source/Rendering/Renderer.cs @@ -86,8 +86,6 @@ namespace DaggerFramework.Rendering /// A texture handler on the GPU. public abstract int LoadTexture(Texture2d texture); - public abstract int LoadFont(Font font); - /// /// Sets transforms for the next draw operation. /// @@ -128,7 +126,7 @@ namespace DaggerFramework.Rendering /// Color of the text. 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); /// /// Draws the texture. diff --git a/DaggerFramework/Source/Rendering/StandardRenderer.cs b/DaggerFramework/Source/Rendering/StandardRenderer.cs index c7126dd..bd6640b 100644 --- a/DaggerFramework/Source/Rendering/StandardRenderer.cs +++ b/DaggerFramework/Source/Rendering/StandardRenderer.cs @@ -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; diff --git a/DaggerFramework/Source/Resources/Font.cs b/DaggerFramework/Source/Resources/Font.cs index 0ffc1e7..c945183 100644 --- a/DaggerFramework/Source/Resources/Font.cs +++ b/DaggerFramework/Source/Resources/Font.cs @@ -2,7 +2,11 @@ namespace DaggerFramework; public class Font : Resource { - public int Size = 16; + /// + /// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1. + /// + internal int FontHandle { get; set; } = -1; + public int Size { get; set; } = 16; public Font(string path, byte[] buffer) : base(path, buffer) { } diff --git a/DaggerFramework/Source/SceneGraph/Entities/Text2d.cs b/DaggerFramework/Source/SceneGraph/Entities/Text2d.cs index 79ec8de..d77b077 100644 --- a/DaggerFramework/Source/SceneGraph/Entities/Text2d.cs +++ b/DaggerFramework/Source/SceneGraph/Entities/Text2d.cs @@ -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; diff --git a/DaggerFramework/Source/UI/TextLabel.cs b/DaggerFramework/Source/UI/TextLabel.cs index 5a139a7..cbc8d1a 100644 --- a/DaggerFramework/Source/UI/TextLabel.cs +++ b/DaggerFramework/Source/UI/TextLabel.cs @@ -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; } \ No newline at end of file diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index b98912c..3da0959 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -51,6 +51,11 @@ public class TestGame : Game if (_resourceManager.TryLoad("inter_regular", "fonts/Inter-Regular.ttf")) { _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("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) }); - _fontHandle = _renderer.LoadFont(_font); - _scene.AddLayer("World", _worldLayer); - // _scene.AddLayer("UI", _uiLayer); - - // _worldLayer.AddEntity(new World()); + _worldLayer.AddEntity(new World()); _worldLayer.AddEntity(new TestPlayer()); _scene.Start(); @@ -79,7 +80,11 @@ public class TestGame : Game double frameTimeMs = _renderer.FrameTime * 1000f; _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 Sound? _testSound; private Font? _font; - private int _fontHandle; private FmodAudioBackend _audioBackend; private InputHandler _inputHandler; private Scene _scene;