diff --git a/DaggerFramework/DaggerFramework.csproj b/DaggerFramework/DaggerFramework.csproj index 9783d74..20afeaa 100644 --- a/DaggerFramework/DaggerFramework.csproj +++ b/DaggerFramework/DaggerFramework.csproj @@ -11,14 +11,15 @@ + + - + \ No newline at end of file diff --git a/DaggerFramework/Source/Audio/FmodAudioBackend.cs b/DaggerFramework/Source/Audio/FmodAudioBackend.cs index daae1af..77ddd94 100644 --- a/DaggerFramework/Source/Audio/FmodAudioBackend.cs +++ b/DaggerFramework/Source/Audio/FmodAudioBackend.cs @@ -28,7 +28,7 @@ namespace DaggerFramework.Audio channels = 2; } - var channel = PlaySoundFromBuffer(sound.Path, sound.BufferSize, channels, sound.SampleRate, sound.Buffer, GetChannelGroup(bus)); + var channel = PlaySoundFromBuffer(sound.Path, (int)sound.BufferSize, channels, sound.SampleRate, sound.Buffer, GetChannelGroup(bus)); channel.setVolume(volume); channel.setPitch(pitch); } diff --git a/DaggerFramework/Source/Rendering/RaylibRenderer.cs b/DaggerFramework/Source/Rendering/RaylibRenderer.cs index 34a1978..84c3d9e 100755 --- a/DaggerFramework/Source/Rendering/RaylibRenderer.cs +++ b/DaggerFramework/Source/Rendering/RaylibRenderer.cs @@ -1,4 +1,5 @@ using System.Numerics; +using System.Runtime.InteropServices; using System.Text; using Raylib_cs; @@ -98,6 +99,35 @@ 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)); @@ -154,7 +184,14 @@ 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) }; } - private List _texturePool = new List(); + public override void DrawText(int fontId, string text, int fontSize, Color color) + { + var font = _fontPool[fontId]; + Raylib.DrawTextPro(font, text, _position, Vector2.Zero, _rotation, fontSize, 0.0f, DaggerColorToRaylibColor(color)); + } + + private List _texturePool = new(); + private List _fontPool = new(); private Vector2 _position; private float _rotation; private Vector2 _windowSize; diff --git a/DaggerFramework/Source/Rendering/Renderer.cs b/DaggerFramework/Source/Rendering/Renderer.cs index 4c1e45d..f7ba52e 100755 --- a/DaggerFramework/Source/Rendering/Renderer.cs +++ b/DaggerFramework/Source/Rendering/Renderer.cs @@ -71,6 +71,9 @@ namespace DaggerFramework.Rendering /// Texture to load. /// 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. /// @@ -109,6 +112,9 @@ namespace DaggerFramework.Rendering /// Size of the font. /// 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); + /// /// Draws the texture. /// diff --git a/DaggerFramework/Source/Rendering/GlRenderer.cs b/DaggerFramework/Source/Rendering/StandardRenderer.cs similarity index 94% rename from DaggerFramework/Source/Rendering/GlRenderer.cs rename to DaggerFramework/Source/Rendering/StandardRenderer.cs index 203c6c4..1843504 100644 --- a/DaggerFramework/Source/Rendering/GlRenderer.cs +++ b/DaggerFramework/Source/Rendering/StandardRenderer.cs @@ -156,6 +156,16 @@ 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(); + } + private GL _gl; private Glfw _glfw; private unsafe WindowHandle* _windowHandle; diff --git a/DaggerFramework/Source/Resources/Loaders/FontLoader.cs b/DaggerFramework/Source/Resources/Loaders/FontLoader.cs index 9cd9dae..2e52c4a 100644 --- a/DaggerFramework/Source/Resources/Loaders/FontLoader.cs +++ b/DaggerFramework/Source/Resources/Loaders/FontLoader.cs @@ -4,13 +4,16 @@ public class FontLoader : IResourceLoader { public IEnumerable SupportedExtensions => new string[] { - ".ttf" + "ttf" }; public Type ResourceType => typeof(Font); public Resource Load(string path) { - return default; + byte[] fileBuffer = File.ReadAllBytes(path); + var result = new Font(path, fileBuffer); + result.BufferSize = fileBuffer.Length; + return result; } } \ No newline at end of file diff --git a/DaggerFramework/Source/Resources/Resource.cs b/DaggerFramework/Source/Resources/Resource.cs index 427fda0..aee1dcf 100755 --- a/DaggerFramework/Source/Resources/Resource.cs +++ b/DaggerFramework/Source/Resources/Resource.cs @@ -5,6 +5,8 @@ namespace DaggerFramework public string? Path { get => _path; set => _path = value; } public byte[]? Buffer { get => _buffer; set => _buffer = value; } + public long BufferSize { get; set; } + public Resource(string path, byte[] buffer) { _path = path; diff --git a/DaggerFramework/Source/Resources/ResourceManager.cs b/DaggerFramework/Source/Resources/ResourceManager.cs index 10974b8..38efd58 100644 --- a/DaggerFramework/Source/Resources/ResourceManager.cs +++ b/DaggerFramework/Source/Resources/ResourceManager.cs @@ -102,7 +102,8 @@ namespace DaggerFramework.Resources private readonly Dictionary _resourceLoaderAssociations = new() { {typeof(Sound), new SoundLoader()}, - {typeof(Texture2d), new Texture2dLoader()} + {typeof(Texture2d), new Texture2dLoader()}, + {typeof(Font), new FontLoader()} }; private Dictionary _loadedResources = new(); diff --git a/DaggerFramework/Source/Resources/Sound.cs b/DaggerFramework/Source/Resources/Sound.cs index 766d369..87273eb 100644 --- a/DaggerFramework/Source/Resources/Sound.cs +++ b/DaggerFramework/Source/Resources/Sound.cs @@ -4,7 +4,6 @@ namespace DaggerFramework { public SoundFormat Format { get; set; } public int SampleRate { get; set; } - public int BufferSize { get; set; } public Sound(string path, byte[] buffer) : base(path, buffer) { diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index 5e3830a..8ac31de 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -32,12 +32,18 @@ public class TestGame : Game { _resourceManager.TryGetResource("my_sound", out _testSound); } + + if (_resourceManager.TryLoad("inter_regular", "fonts/Inter-Regular.ttf")) + { + _resourceManager.TryGetResource("inter_regular", out _font); + } } protected override void Ready() { _audioBackend.AddBusEffect(new AudioEffectReverb()); _inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); + _fontHandle = _renderer.LoadFont(_font); } protected override void Run() @@ -59,6 +65,9 @@ public class TestGame : Game _renderer.SetTransform(new Vector2(640, 480)); _renderer.DrawCircle(16f, Color.Chocolate); + + _renderer.DrawText(_fontHandle, "Hello World!", 24, Color.White); + _renderer.EndFrame(); } } @@ -72,6 +81,8 @@ public class TestGame : Game private Renderer _renderer; private ResourceManager _resourceManager = new(); private Sound? _testSound; + private Font? _font; + private int _fontHandle; private FmodAudioBackend _audioBackend; private InputHandler _inputHandler; } \ No newline at end of file diff --git a/TestGame/TestGame.csproj b/TestGame/TestGame.csproj index a23f452..6fad062 100644 --- a/TestGame/TestGame.csproj +++ b/TestGame/TestGame.csproj @@ -19,4 +19,4 @@ - + \ No newline at end of file