From 7dff8076b9bc57311d7b4ef82462674d34aa50b3 Mon Sep 17 00:00:00 2001 From: dnesov Date: Tue, 15 Oct 2024 21:51:26 +0200 Subject: [PATCH] Use ResourceRefs for RenderSystem, add IReloadableSystem, remove UI package. --- TestGame/Resources/test_emitter.toml | 12 ++-- TestGame/TestGame.cs | 24 ++++++- Voile/Source/Rendering/RaylibRenderSystem.cs | 15 ++++- Voile/Source/Rendering/RenderSystem.cs | 11 ++-- .../Source/Rendering/StandardRenderSystem.cs | 8 ++- Voile/Source/Systems/ISystem.cs | 11 ++++ Voile/Source/UI/Container.cs | 63 ------------------- Voile/Source/UI/MarginPanel.cs | 24 ------- Voile/Source/UI/Panel.cs | 20 ------ Voile/Source/UI/PanelStyle.cs | 6 -- Voile/Source/UI/Rect.cs | 10 --- Voile/Source/UI/TextLabel.cs | 31 --------- Voile/Source/UI/UIElement.cs | 55 ---------------- Voile/Source/UI/VerticalPanel.cs | 22 ------- 14 files changed, 64 insertions(+), 248 deletions(-) delete mode 100644 Voile/Source/UI/Container.cs delete mode 100644 Voile/Source/UI/MarginPanel.cs delete mode 100644 Voile/Source/UI/Panel.cs delete mode 100644 Voile/Source/UI/PanelStyle.cs delete mode 100644 Voile/Source/UI/Rect.cs delete mode 100644 Voile/Source/UI/TextLabel.cs delete mode 100644 Voile/Source/UI/UIElement.cs delete mode 100644 Voile/Source/UI/VerticalPanel.cs diff --git a/TestGame/Resources/test_emitter.toml b/TestGame/Resources/test_emitter.toml index 964572b..8ab88e7 100644 --- a/TestGame/Resources/test_emitter.toml +++ b/TestGame/Resources/test_emitter.toml @@ -1,14 +1,14 @@ [ParticleEmitterSettings] -MaxParticles = 256 -EmitRadius = 32 -Explosiveness = 0.0 -LifeTime = 0.5 +MaxParticles = 4096 +EmitRadius = 128 +Explosiveness = 1.0 +LifeTime = 1.0 Direction = [0.0, 1.0] LinearVelocity = 200 -Gravity = [0.0, 980.0] +Gravity = [0.0, 0.0] LinearVelocityRandom = 0.5 ScaleBegin = 1.0 -ScaleEnd = 2.0 +ScaleEnd = 0.0 ColorBegin = [255, 0, 255, 255] ColorEnd = [0, 0, 0, 0] diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index d49439e..3853b62 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -5,6 +5,7 @@ using Voile.Input; using Voile.Systems.Particles; using System.Numerics; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics; public class TestGame : Game { @@ -19,6 +20,8 @@ public class TestGame : Game ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader()); Input.AddInputMapping("reload", new InputAction[] { new KeyInputAction(KeyboardKey.R) }); + + _particleSimStopwatch = new Stopwatch(); } protected override void LoadResources() @@ -28,10 +31,12 @@ public class TestGame : Game // } - // if (!ResourceManager.TryLoad("inter_regular", "fonts/Inter-Regular.ttf", out Font? _font)) - // { + if (!ResourceManager.TryLoad("fonts/Inter-Regular.ttf", out _font)) + { - // } + } + + ResourceManager.TryLoad("icon.png", out _icon); if (!ResourceManager.TryLoad("test_emitter.toml", out _emitterSettings)) { @@ -54,7 +59,9 @@ public class TestGame : Game _particleSystem!.RestartEmitter(_emitterId); } + _particleSimStopwatch = Stopwatch.StartNew(); _particleSystem!.Update(Renderer.FrameTime); + _particleSimStopwatch.Stop(); Renderer.BeginFrame(); Renderer.ClearBackground(Color.Black); @@ -63,7 +70,14 @@ public class TestGame : Game DrawEmitter(emitter); } + Renderer.SetTransform(Renderer.WindowSize / 2, Vector2.Zero); + Renderer.DrawTexture(_icon, Color.White); + + Renderer.ResetTransform(); + Renderer.DrawText(_font, $"Particle Sim: {TimeSpan.FromTicks(_particleSimStopwatch.ElapsedTicks).TotalMilliseconds} ms", Color.White); + Renderer.EndFrame(); + _particleSimStopwatch.Restart(); } } @@ -90,7 +104,11 @@ public class TestGame : Game } [NotNull] private ParticleSystem _particleSystem; + + private Stopwatch _particleSimStopwatch; private int _emitterId; private ResourceRef _emitterSettings; + private ResourceRef _font; + private ResourceRef _icon; private Logger _logger = new(nameof(TestGame)); } \ No newline at end of file diff --git a/Voile/Source/Rendering/RaylibRenderSystem.cs b/Voile/Source/Rendering/RaylibRenderSystem.cs index e933732..d572fa4 100644 --- a/Voile/Source/Rendering/RaylibRenderSystem.cs +++ b/Voile/Source/Rendering/RaylibRenderSystem.cs @@ -83,6 +83,12 @@ namespace Voile.Rendering // Raylib.SetWindowState(windowFlags); } + // TODO + protected override void ReloadResources() + { + throw new NotImplementedException(); + } + protected override void SetWindowTitle(string title) { Raylib.SetWindowTitle(title); @@ -158,8 +164,10 @@ namespace Voile.Rendering Raylib.DrawCircle((int)transformPosition.X, (int)transformPosition.Y, radius, VoileColorToRaylibColor(color)); } - public override void DrawTexture(Texture2d texture, Color tint) + public override void DrawTexture(ResourceRef textureResource, Color tint) { + var texture = textureResource.Value; + if (texture.Handle == -1) { LoadTexture(texture); @@ -214,14 +222,17 @@ namespace Voile.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(Font font, string text, Color color) + public override void DrawText(ResourceRef fontResource, string text, Color color) { + var font = fontResource.Value; + if (font.Handle == -1) { LoadFont(font); } var rayFont = _fontPool[font.Handle]; + Raylib.DrawTextPro(rayFont, text, transformPosition, transformPivot, transformRotation, font.Size, 0.0f, VoileColorToRaylibColor(color)); } diff --git a/Voile/Source/Rendering/RenderSystem.cs b/Voile/Source/Rendering/RenderSystem.cs index 1dbaa27..bd66c83 100644 --- a/Voile/Source/Rendering/RenderSystem.cs +++ b/Voile/Source/Rendering/RenderSystem.cs @@ -37,13 +37,15 @@ namespace Voile.Rendering /// /// An abstract class representing the graphics renderer. /// - public abstract class RenderSystem : IStartableSystem, IDisposable + public abstract class RenderSystem : IStartableSystem, IReloadableSystem, IDisposable { public void Start(RendererSettings settings) { CreateAndInitializeWithWindow(settings); } + public void Reload() => ReloadResources(); + /// public void Dispose() { @@ -89,6 +91,8 @@ namespace Voile.Rendering /// public abstract Vector2 MonitorSize { get; } + protected abstract void ReloadResources(); + /// /// Creates a window. /// @@ -178,15 +182,14 @@ namespace Voile.Rendering /// Color of the text. public abstract void DrawSdfText(string text, int fontSize, Color color); - public abstract void DrawText(Font font, string text, Color color); + public abstract void DrawText(ResourceRef font, string text, Color color); /// /// Draws the texture. /// /// Texture to draw. /// Texture tint. - public abstract void DrawTexture(Texture2d texture, Color tint); - + public abstract void DrawTexture(ResourceRef textureResource, Color tint); protected Vector2 transformPosition, transformPivot; protected float transformRotation; diff --git a/Voile/Source/Rendering/StandardRenderSystem.cs b/Voile/Source/Rendering/StandardRenderSystem.cs index b0555f8..2202e9c 100644 --- a/Voile/Source/Rendering/StandardRenderSystem.cs +++ b/Voile/Source/Rendering/StandardRenderSystem.cs @@ -78,7 +78,7 @@ namespace Voile.Rendering _clearColor = color; } - public override void DrawText(Font font, string text, Color color) + public override void DrawText(ResourceRef fontResource, string text, Color color) { throw new NotImplementedException(); } @@ -117,7 +117,7 @@ namespace Voile.Rendering } /// - public override void DrawTexture(Texture2d texture, Color tint) + public override void DrawTexture(ResourceRef textureResource, Color tint) { throw new NotImplementedException(); } @@ -394,6 +394,10 @@ namespace Voile.Rendering throw new NotImplementedException(); } + protected override void ReloadResources() + { + throw new NotImplementedException(); + } private Vector2 _windowSize = Vector2.Zero; private IWindow? _window; diff --git a/Voile/Source/Systems/ISystem.cs b/Voile/Source/Systems/ISystem.cs index 5035b72..f6843f9 100644 --- a/Voile/Source/Systems/ISystem.cs +++ b/Voile/Source/Systems/ISystem.cs @@ -17,6 +17,17 @@ public interface IStartableSystem void Start(T configuration); } +/// +/// A system that has the ability to reload its internal state. +/// +public interface IReloadableSystem +{ + /// + /// Reloads internal system state. + /// + void Reload(); +} + public interface IUpdatableSystem { /// diff --git a/Voile/Source/UI/Container.cs b/Voile/Source/UI/Container.cs deleted file mode 100644 index 49128d5..0000000 --- a/Voile/Source/UI/Container.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.Numerics; -using Voile.Rendering; - -namespace Voile.UI; - -/// -/// A basic container for UI elements. All container's children will update their constraints based on container's sizing and positioning. -/// -public class Container : UIElement -{ - /// - /// Updates the sizes of the container and rearranges its children. - /// - /// - /// - public void UpdateRect(Vector2 position, Vector2 size) - { - Rect.Position = position; - - UpdateSize(size); - RearrangeChildren(); - } - - protected void RearrangeChildren() - { - int idx = 0; - foreach (var child in children) - { - if (child is Container container) - { - container.UpdateRect(Rect.Position, Rect.Size); - } - RearrangeChild(idx, child); - idx++; - } - } - - protected virtual void RearrangeChild(int idx, UIElement child) { } - - protected override void OnRender(RenderSystem renderer) - { - - } - - private void UpdateSize(Vector2 baseSize) - { - if (parent == null) - { - Rect.Size = baseSize; - return; - } - - if (VerticalSizeFlags == SizeFlags.Fill) - { - Rect.Size = new Vector2(Rect.Size.X, baseSize.Y * ExpandRatio.Y); - } - - if (HorizontalSizeFlags == SizeFlags.Fill) - { - Rect.Size = new Vector2(baseSize.X * ExpandRatio.X, Rect.Size.Y); - } - } -} \ No newline at end of file diff --git a/Voile/Source/UI/MarginPanel.cs b/Voile/Source/UI/MarginPanel.cs deleted file mode 100644 index 080fa29..0000000 --- a/Voile/Source/UI/MarginPanel.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Numerics; - -namespace Voile.UI; - -public class MarginPanel : Panel -{ - public Vector2 RelativeMargin { get; set; } - public Vector2 AbsoluteMargin { get; set; } - public MarginPanel(PanelStyle style) : base(style) - { - } - - protected override void RearrangeChild(int idx, UIElement child) - { - base.RearrangeChild(idx, child); - - var rect = child.Rect; - - var absoluteMargin = Rect.Size * RelativeMargin + AbsoluteMargin; - - rect.Position = Rect.Position + absoluteMargin; - rect.Size = rect.Size - absoluteMargin * 2; - } -} \ No newline at end of file diff --git a/Voile/Source/UI/Panel.cs b/Voile/Source/UI/Panel.cs deleted file mode 100644 index 1987700..0000000 --- a/Voile/Source/UI/Panel.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Numerics; -using Voile.Rendering; - -namespace Voile.UI; - -public class Panel : Container -{ - public PanelStyle Style { get; set; } - - public Panel(PanelStyle style) - { - Style = style; - } - - protected override void OnRender(RenderSystem renderer) - { - base.OnRender(renderer); - renderer.DrawRectangle(Rect.Size, Style.BackgroundColor); - } -} \ No newline at end of file diff --git a/Voile/Source/UI/PanelStyle.cs b/Voile/Source/UI/PanelStyle.cs deleted file mode 100644 index abb907a..0000000 --- a/Voile/Source/UI/PanelStyle.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Voile.UI; - -public struct PanelStyle -{ - public Color BackgroundColor { get; set; } -} \ No newline at end of file diff --git a/Voile/Source/UI/Rect.cs b/Voile/Source/UI/Rect.cs deleted file mode 100644 index 1096a7b..0000000 --- a/Voile/Source/UI/Rect.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Numerics; - -namespace Voile.UI; - -public class Rect -{ - public Vector2 Position { get; set; } - public Vector2 Size { get; set; } - public Vector2 Scale { get; set; } -} \ No newline at end of file diff --git a/Voile/Source/UI/TextLabel.cs b/Voile/Source/UI/TextLabel.cs deleted file mode 100644 index 3fd84cd..0000000 --- a/Voile/Source/UI/TextLabel.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Voile.Rendering; - -namespace Voile.UI; - -public class TextLabel : UIElement -{ - public string Text { get; set; } = string.Empty; - public Font Font - { - get => _font; set - { - _font = value; - } - } - public int FontSize { get; set; } = 16; - public Color FontColor { get; set; } = Color.White; - protected override void OnRender(RenderSystem renderer) - { - - if (_font == null) - { - renderer.DrawDebugText(Text, FontSize, FontColor); - } - else - { - renderer.DrawText(_font, Text, FontColor); - } - } - - private Font? _font; -} \ No newline at end of file diff --git a/Voile/Source/UI/UIElement.cs b/Voile/Source/UI/UIElement.cs deleted file mode 100644 index c6d4cf5..0000000 --- a/Voile/Source/UI/UIElement.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System.Numerics; -using Voile.Rendering; -using Voile.Utils; - -namespace Voile.UI; - -public abstract class UIElement -{ - public Rect Rect { get; set; } = new Rect(); - public SizeFlags VerticalSizeFlags { get; set; } = SizeFlags.Fill; - public SizeFlags HorizontalSizeFlags { get; set; } = SizeFlags.Fill; - - public Vector2 ExpandRatio { get; set; } = Vector2.One; - - public UIElement() - { - children = new(); - } - - public void AddChild(UIElement child) - { - children.Add(child); - child.parent = this; - } - - public void Render(RenderSystem renderer) - { - Vector2 parentPos = parent != null ? parent.Rect.Position : Vector2.Zero; - renderer.SetTransform(Rect.Position + parentPos, Vector2.Zero, 0); - OnRender(renderer); - - foreach (UIElement child in children) - { - renderer.SetTransform(child.Rect.Position, Vector2.Zero, 0); - child.Render(renderer); - } - } - - protected abstract void OnRender(RenderSystem renderer); - - protected List children; - protected UIElement? parent; - - private Logger _logger = new(nameof(UIElement)); -} - - -[Flags] -public enum SizeFlags -{ - ShrinkBegin, - ShrinkCenter, - ShrinkEnd, - Fill -} \ No newline at end of file diff --git a/Voile/Source/UI/VerticalPanel.cs b/Voile/Source/UI/VerticalPanel.cs deleted file mode 100644 index 5483d32..0000000 --- a/Voile/Source/UI/VerticalPanel.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Numerics; - -namespace Voile.UI; - -public class VerticalPanel : Panel -{ - public float Spacing { get; set; } = 16; - public VerticalPanel(PanelStyle style) : base(style) - { - } - - protected override void RearrangeChild(int idx, UIElement child) - { - base.RearrangeChild(idx, child); - - var yOffset = idx * Spacing; - var rect = child.Rect; - - rect.Position = Rect.Position; - rect.Position = new Vector2(rect.Position.X, yOffset); - } -} \ No newline at end of file