From 522088227bdf8f221c32fa3428f4d634ca0c4b9b Mon Sep 17 00:00:00 2001 From: JacobTech Date: Mon, 2 Jan 2023 09:59:45 -0500 Subject: [PATCH] Optimization Anything to do with labes drawing fonts has more optimization --- GraphicsManager/GraphicsManager.csproj | 2 +- GraphicsManager/Objects/Core/Font.cs | 174 ++++++++---------------- GraphicsManager/Objects/Core/Texture.cs | 4 +- GraphicsManager/Objects/Label.cs | 45 ++---- 4 files changed, 76 insertions(+), 149 deletions(-) diff --git a/GraphicsManager/GraphicsManager.csproj b/GraphicsManager/GraphicsManager.csproj index 091acc0..50771e6 100644 --- a/GraphicsManager/GraphicsManager.csproj +++ b/GraphicsManager/GraphicsManager.csproj @@ -10,7 +10,7 @@ False https://git.jacobtech.com/JacobTech.com/GraphicsManager git - 1.0.0-alpha7 + 1.0.0-alpha8 diff --git a/GraphicsManager/Objects/Core/Font.cs b/GraphicsManager/Objects/Core/Font.cs index 4c4fad5..b2880db 100755 --- a/GraphicsManager/Objects/Core/Font.cs +++ b/GraphicsManager/Objects/Core/Font.cs @@ -9,10 +9,59 @@ public class Font private static List? System = null; private List _Faces = new(); private Library lib; + + internal Font() + { + Name = null!; + Assembly = null!; + Embeded = false; + if (lib is null) lib = new(); + //TODO add more systemfont dection methods + if (System is null) + { + System = new(); + if (OperatingSystem.IsLinux()) + { + try + { + Process proc = new() + { + StartInfo = new() + { + FileName = "/bin/bash", + Arguments = "-c \"fc-list ':' file\"", + RedirectStandardOutput = true, + } + }; + proc.Start(); + proc.WaitForExit(); + string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}"); + for (int i = 0; i < files.Length; i++) + { + System!.Add(new Face(lib, files[i], 0)); + } + } + catch + { + } + } + } + + for (int i = 0; i < System!.Count; i++) _Faces.Add(System![i]); + //TODO add a reserved backup font + } public IReadOnlyList Faces => _Faces.AsReadOnly(); public void SetEmbeddedFont(string Font, Assembly? Assembly = null) { _ = Font ?? throw new ArgumentNullException(nameof(Font)); + string Base = "GraphicsManager.Resources.Fonts."; + if (Assembly is not null) Base = string.Empty; + byte[] f = (Assembly is null + ? Tools.GetResourceBytes(Base + Font) + : Tools.GetResourceBytes(Assembly!, $"{Base}{Font}")); + if (HasTopFont) _Faces[0] = new Face(lib, f, 0); + else _Faces.Insert(0, new Face(lib, f, 0)); + HasTopFont = true; this.Assembly = Assembly; this.Embeded = true; this.Name = Font; @@ -26,89 +75,14 @@ public class Font Assembly = Assembly, Embeded = true, Name = Font, + HasTopFont = true }; - Library lib = new(); string Base = "GraphicsManager.Resources.Fonts."; if (Assembly is not null) Base = string.Empty; byte[] f = (Assembly is null ? Tools.GetResourceBytes(Base + Font) : Tools.GetResourceBytes(Assembly!, $"{Base}{Font}")); - fontclass._Faces.Add(new Face(lib, f, 0)); - if (System is null) - { - System = new(); - if (OperatingSystem.IsLinux()) - { - try - { - Process proc = new() - { - StartInfo = new() - { - FileName = "/bin/bash", - Arguments = "-c \"fc-list ':' file\"", - RedirectStandardOutput = true, - } - }; - proc.Start(); - proc.WaitForExit(); - string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}"); - for (int i = 0; i < files.Length; i++) - { - System!.Add(new Face(fontclass.lib, files[i], 0)); - } - } - catch - { - } - } - } - - for (int i = 0; i < System!.Count; i++) fontclass._Faces.Add(System![i]); - return fontclass; - } - - public static Font MakeFontFromSystem() - { - - Font fontclass = new() - { - Assembly = null, - Embeded = false, - Name = default!, - lib = new() - }; - if (Font.System is null) - { - Font.System = new(); - if (OperatingSystem.IsLinux()) - { - try - { - Process proc = new() - { - StartInfo = new() - { - FileName = "/bin/bash", - Arguments = "-c \"fc-list ':' file\"", - RedirectStandardOutput = true, - } - }; - proc.Start(); - proc.WaitForExit(); - string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}"); - for (int i = 0; i < files.Length; i++) - { - Font.System!.Add(new Face(fontclass.lib, files[i], 0)); - } - } - catch - { - } - } - } - - for (int i = 0; i < Font.System!.Count; i++) fontclass._Faces.Add(Font.System![i]); + fontclass._Faces.Insert(0, new Face(fontclass.lib, f, 0)); return fontclass; } @@ -120,55 +94,25 @@ public class Font Assembly = null, Embeded = false, Name = Font, + HasTopFont = true }; - fontclass.lib = new(); - fontclass._Faces.Add(new Face(fontclass.lib, File.ReadAllBytes(Font), 0)); - if (System is null) - { - System = new(); - if (OperatingSystem.IsLinux()) - { - try - { - Process proc = new() - { - StartInfo = new() - { - FileName = "/bin/bash", - Arguments = "-c \"fc-list ':' file\"", - RedirectStandardOutput = true, - } - }; - proc.Start(); - proc.WaitForExit(); - string[] files = proc.StandardOutput.ReadToEnd().Split($": {Environment.NewLine}"); - for (int i = 0; i < files.Length; i++) - { - System!.Add(new Face(fontclass.lib, files[i], 0)); - } - } - catch - { - } - } - } - - for (int i = 0; i < System!.Count; i++) fontclass._Faces.Add(System![i]); + fontclass._Faces.Insert(0, new Face(fontclass.lib, Font, 0)); return fontclass; } - + public void SetFontFile(string Font) { _ = Font ?? throw new ArgumentNullException(nameof(Font)); - _Faces.RemoveAt(0); - _Faces.Reverse(); - _Faces.Add(new Face(lib, File.ReadAllBytes(Font), 0)); - _Faces.Reverse(); + if (HasTopFont) _Faces[0] = new Face(lib, Font, 0); + else _Faces.Insert(0, new Face(lib, Font, 0)); + HasTopFont = true; this.Assembly = null; this.Embeded = false; this.Name = Font; } + private bool HasTopFont = false; + public uint PixelHeight { get; set; } = 20; public string Name { get; private set; } = default!; public bool Embeded { get; private set; } public Assembly? Assembly { get; private set; } diff --git a/GraphicsManager/Objects/Core/Texture.cs b/GraphicsManager/Objects/Core/Texture.cs index df63372..cbd43db 100755 --- a/GraphicsManager/Objects/Core/Texture.cs +++ b/GraphicsManager/Objects/Core/Texture.cs @@ -45,7 +45,7 @@ public class Texture { } - internal static Texture TextureForChar(Label l, char charter, uint PixelHeight, Face[] faces) + internal static Texture TextureForChar(Font l, char charter, Face[] faces) { Texture t = new(); for (int i = 0; i < faces.Length; i++) @@ -54,7 +54,7 @@ public class Texture { if (!Label._characters.ContainsKey(l)) Label._characters.Add(l, new Dictionary()); if (Label._characters[l].ContainsKey(charter)) return Label._characters[l][(ushort)charter].Texture; - faces[i].SetPixelSizes(0, PixelHeight); + faces[i].SetPixelSizes(0, l.PixelHeight); GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1); diff --git a/GraphicsManager/Objects/Label.cs b/GraphicsManager/Objects/Label.cs index 6606f05..c55386a 100755 --- a/GraphicsManager/Objects/Label.cs +++ b/GraphicsManager/Objects/Label.cs @@ -12,6 +12,7 @@ namespace GraphicsManager.Objects; public class Label : IRenderObject { public static readonly Shader DefaultTextShader = new("Label", true); + public static readonly Font DefaultFont = new(); public IParent? Parent { get; private set; } public ObjectAnchor Anchor { get; set; } = ObjectAnchor.Left | ObjectAnchor.Top; private Vector2 laf = new(), saf = new(); @@ -20,10 +21,10 @@ public class Label : IRenderObject public Vector2 SizeAsFloat { get { return saf; } } public bool Visible { get; set; } = true; - public static readonly Dictionary> _characters = new(); + public static readonly Dictionary> _characters = new(); private string text = string.Empty; - public int VAO { get; set; } - public int VBO { get; set; } + public int VAO { get; private set; } + public int VBO { get; private set; } public Vector2 DIR { get; set; } = new Vector2(1f, 0f); public string Text { @@ -34,22 +35,20 @@ public class Label : IRenderObject if (Loaded) { GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1); - if (!_characters.ContainsKey(this)) _characters.Add(this, new Dictionary()); + if (!_characters.ContainsKey(Font)) _characters.Add(Font, new Dictionary()); foreach (char character in value) { - if (_characters[this].ContainsKey(character)) continue; - var f = Texture.TextureForChar(this, character, PixelHeight, Font.Faces.ToArray()); + if (_characters[Font].ContainsKey(character)) continue; + var f = Texture.TextureForChar(Font, character, Font.Faces.ToArray()); f.LoadText(); } if (Window is not null && Window.CanControleUpdate && Loaded) Window.DrawFrame(); } } } - public uint PixelHeight { get; set; } = 20; - public float Scale { get; set; } = 1.0f; public Shader Shader { get; set; } = DefaultTextShader; - public Font Font { get; set; } = Font.MakeFontFromSystem(); - + public Font Font { get; set; } = DefaultFont; + public float Scale { get; set; } = 1.0f; public Vector4 Color { get; set; } = new Vector4(1, 1, 1, 1); public Vector2i Distance { get; private set; } private Vector2i loc_ = new(); @@ -101,12 +100,12 @@ public class Label : IRenderObject float hhh = 0f; foreach (char c in Text) { - if (!_characters[this].ContainsKey(c)) break; - Character ch = _characters[this][c]; + if (!_characters[Font].ContainsKey(c)) break; + Character ch = _characters[Font][c]; if (c == '\n') { - hhh += PixelHeight; + hhh += Font.PixelHeight; char_x = 0f; } else @@ -138,18 +137,11 @@ public class Label : IRenderObject public void LoadToParent(IParent window, Window win) { if (Loaded) return; - if (!_characters.ContainsKey(this)) _characters.Add(this, new Dictionary()); + if (!_characters.ContainsKey(Font)) _characters.Add(Font, new Dictionary()); Parent = window; Window = win; - Library lib = new(); - - //Face face = new(lib, Font.Faces.First(), 0); - //face.SetPixelSizes(0, PixelHeight); GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1); - //face.SelectCharmap(Encoding.Unicode); - GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4); - float[] vquad = { 0.0f, -1.0f, 0.0f, 0.0f, @@ -164,16 +156,7 @@ public class Label : IRenderObject GL.BindBuffer(BufferTarget.ArrayBuffer, VBO); GL.BufferData(BufferTarget.ArrayBuffer, 4 * 6 * 4, vquad, BufferUsageHint.StaticDraw); - VAO = GL.GenVertexArray();/* - if (!_characters.ContainsKey(this)) _characters.Add(this, new Dictionary()); - foreach (char character in Text) - { - if (_characters[this].ContainsKey(character) == false) - { - var f = new Texture(this, character, PixelHeight, face); - f.LoadText(); - } - }*/ + VAO = GL.GenVertexArray(); GL.BindVertexArray(VAO); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 4 * 4, 0);