WIP: fonts.

This commit is contained in:
2023-06-20 00:18:35 +02:00
parent 72b6896d3e
commit 193462b747
11 changed files with 79 additions and 9 deletions

View File

@@ -11,14 +11,15 @@
<PackageReference Include="DaggerFramework.Fmod" Version="0.2.2.8" />
<PackageReference Include="ImGui.NET" Version="1.89.4" />
<PackageReference Include="Raylib-cs" Version="4.2.0.1" />
<PackageReference Include="SharpFont" Version="4.0.1" />
<PackageReference Include="Silk.NET" Version="2.17.0" />
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta19" />
<PackageReference Include="StbImageSharp" Version="2.27.13" />
<PackageReference Include="StbVorbisSharp" Version="1.22.4" />
</ItemGroup>
<Target Name="BuildFmod" BeforeTargets="BeforeBuild">
<MSBuild Projects="../DaggerFramework.Fmod/DaggerFramework.Fmod.csproj"
Targets="Restore;Build" />
<MSBuild Projects="../DaggerFramework.Fmod/DaggerFramework.Fmod.csproj" Targets="Restore;Build" />
</Target>
</Project>

View File

@@ -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);
}

View File

@@ -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<Texture2D> _texturePool = new List<Texture2D>();
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<Texture2D> _texturePool = new();
private List<Raylib_cs.Font> _fontPool = new();
private Vector2 _position;
private float _rotation;
private Vector2 _windowSize;

View File

@@ -71,6 +71,9 @@ namespace DaggerFramework.Rendering
/// <param name="texture">Texture to load.</param>
/// <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>
@@ -109,6 +112,9 @@ namespace DaggerFramework.Rendering
/// <param name="fontSize">Size of the font.</param>
/// <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);
/// <summary>
/// Draws the texture.
/// </summary>

View File

@@ -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;

View File

@@ -4,13 +4,16 @@ public class FontLoader : IResourceLoader
{
public IEnumerable<string> 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;
}
}

View File

@@ -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;

View File

@@ -102,7 +102,8 @@ namespace DaggerFramework.Resources
private readonly Dictionary<Type, IResourceLoader> _resourceLoaderAssociations = new()
{
{typeof(Sound), new SoundLoader()},
{typeof(Texture2d), new Texture2dLoader()}
{typeof(Texture2d), new Texture2dLoader()},
{typeof(Font), new FontLoader()}
};
private Dictionary<string, Resource> _loadedResources = new();

View File

@@ -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)
{

View File

@@ -32,12 +32,18 @@ public class TestGame : Game
{
_resourceManager.TryGetResource<Sound>("my_sound", out _testSound);
}
if (_resourceManager.TryLoad<Font>("inter_regular", "fonts/Inter-Regular.ttf"))
{
_resourceManager.TryGetResource<Font>("inter_regular", out _font);
}
}
protected override void Ready()
{
_audioBackend.AddBusEffect<AudioEffectReverb>(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;
}

View File

@@ -19,4 +19,4 @@
</Content>
</ItemGroup>
</Project>
</Project>