1using System.Numerics;
2using SDL2;
3using static SDL2.SDL_ttf;
4using static SDL2.SDL;
5
6namespace Fjord.Graphics;
7
8public static class Font
9{
10 internal static Dictionary<string, IntPtr> Fonts = new ();
11 internal static Dictionary<string, IntPtr> FontCache = new();
12
13 public static string DefaultFont { internal set; get; } = "selawk.ttf";
14
15 public static void Initialize()
16 {
17 TTF_Init();
18 }
19
20 public static void Destroy()
21 {
22 foreach (string id in FontCache.Keys)
23 {
24 SDL_DestroyTexture(FontCache[id]);
25 }
26 TTF_Quit();
27 }
28
29 public static Vector2 DrawSize(string font, string text, int size, Vector4 color)
30 {
31 if (!Fonts.ContainsKey(font + size.ToString()))
32 {
33 Fonts.Add(font + size.ToString(), TTF_OpenFont(font, size));
34 }
35
36 string CacheKey = font + text + size + color.X + color.Y + color.Z + color.W;
37 if (!FontCache.ContainsKey(CacheKey))
38 {
39 IntPtr surface = TTF_RenderText_Blended(Fonts[font + size.ToString()], text, Helpers.V4ToColor(color));
40 IntPtr texture = SDL.SDL_CreateTextureFromSurface(Game.SDLRenderer, surface);
41 FontCache.Add(CacheKey, texture);
42 SDL.SDL_FreeSurface(surface);
43 }
44
45 SDL_QueryTexture(FontCache[CacheKey], out uint format, out int access, out int textureW, out int textureH);
46
47 return new()
48 {
49 X = textureW,
50 Y = textureH
51 };
52 }
53}