Use ResourceRefs for RenderSystem, add IReloadableSystem, remove UI package.

This commit is contained in:
2024-10-15 21:51:26 +02:00
parent 4d5c6bbe9a
commit 7dff8076b9
14 changed files with 64 additions and 248 deletions

View File

@@ -1,14 +1,14 @@
[ParticleEmitterSettings] [ParticleEmitterSettings]
MaxParticles = 256 MaxParticles = 4096
EmitRadius = 32 EmitRadius = 128
Explosiveness = 0.0 Explosiveness = 1.0
LifeTime = 0.5 LifeTime = 1.0
Direction = [0.0, 1.0] Direction = [0.0, 1.0]
LinearVelocity = 200 LinearVelocity = 200
Gravity = [0.0, 980.0] Gravity = [0.0, 0.0]
LinearVelocityRandom = 0.5 LinearVelocityRandom = 0.5
ScaleBegin = 1.0 ScaleBegin = 1.0
ScaleEnd = 2.0 ScaleEnd = 0.0
ColorBegin = [255, 0, 255, 255] ColorBegin = [255, 0, 255, 255]
ColorEnd = [0, 0, 0, 0] ColorEnd = [0, 0, 0, 0]

View File

@@ -5,6 +5,7 @@ using Voile.Input;
using Voile.Systems.Particles; using Voile.Systems.Particles;
using System.Numerics; using System.Numerics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
public class TestGame : Game public class TestGame : Game
{ {
@@ -19,6 +20,8 @@ public class TestGame : Game
ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader()); ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader());
Input.AddInputMapping("reload", new InputAction[] { new KeyInputAction(KeyboardKey.R) }); Input.AddInputMapping("reload", new InputAction[] { new KeyInputAction(KeyboardKey.R) });
_particleSimStopwatch = new Stopwatch();
} }
protected override void LoadResources() 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)) if (!ResourceManager.TryLoad("test_emitter.toml", out _emitterSettings))
{ {
@@ -54,7 +59,9 @@ public class TestGame : Game
_particleSystem!.RestartEmitter(_emitterId); _particleSystem!.RestartEmitter(_emitterId);
} }
_particleSimStopwatch = Stopwatch.StartNew();
_particleSystem!.Update(Renderer.FrameTime); _particleSystem!.Update(Renderer.FrameTime);
_particleSimStopwatch.Stop();
Renderer.BeginFrame(); Renderer.BeginFrame();
Renderer.ClearBackground(Color.Black); Renderer.ClearBackground(Color.Black);
@@ -63,7 +70,14 @@ public class TestGame : Game
DrawEmitter(emitter); 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(); Renderer.EndFrame();
_particleSimStopwatch.Restart();
} }
} }
@@ -90,7 +104,11 @@ public class TestGame : Game
} }
[NotNull] private ParticleSystem _particleSystem; [NotNull] private ParticleSystem _particleSystem;
private Stopwatch _particleSimStopwatch;
private int _emitterId; private int _emitterId;
private ResourceRef<ParticleEmitterSettingsResource> _emitterSettings; private ResourceRef<ParticleEmitterSettingsResource> _emitterSettings;
private ResourceRef<Font> _font;
private ResourceRef<Texture2d> _icon;
private Logger _logger = new(nameof(TestGame)); private Logger _logger = new(nameof(TestGame));
} }

View File

@@ -83,6 +83,12 @@ namespace Voile.Rendering
// Raylib.SetWindowState(windowFlags); // Raylib.SetWindowState(windowFlags);
} }
// TODO
protected override void ReloadResources()
{
throw new NotImplementedException();
}
protected override void SetWindowTitle(string title) protected override void SetWindowTitle(string title)
{ {
Raylib.SetWindowTitle(title); Raylib.SetWindowTitle(title);
@@ -158,8 +164,10 @@ namespace Voile.Rendering
Raylib.DrawCircle((int)transformPosition.X, (int)transformPosition.Y, radius, VoileColorToRaylibColor(color)); Raylib.DrawCircle((int)transformPosition.X, (int)transformPosition.Y, radius, VoileColorToRaylibColor(color));
} }
public override void DrawTexture(Texture2d texture, Color tint) public override void DrawTexture(ResourceRef<Texture2d> textureResource, Color tint)
{ {
var texture = textureResource.Value;
if (texture.Handle == -1) if (texture.Handle == -1)
{ {
LoadTexture(texture); 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) }; 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<Font> fontResource, string text, Color color)
{ {
var font = fontResource.Value;
if (font.Handle == -1) if (font.Handle == -1)
{ {
LoadFont(font); LoadFont(font);
} }
var rayFont = _fontPool[font.Handle]; var rayFont = _fontPool[font.Handle];
Raylib.DrawTextPro(rayFont, text, transformPosition, transformPivot, transformRotation, font.Size, 0.0f, VoileColorToRaylibColor(color)); Raylib.DrawTextPro(rayFont, text, transformPosition, transformPivot, transformRotation, font.Size, 0.0f, VoileColorToRaylibColor(color));
} }

View File

@@ -37,13 +37,15 @@ namespace Voile.Rendering
/// <summary> /// <summary>
/// An abstract class representing the graphics renderer. /// An abstract class representing the graphics renderer.
/// </summary> /// </summary>
public abstract class RenderSystem : IStartableSystem<RendererSettings>, IDisposable public abstract class RenderSystem : IStartableSystem<RendererSettings>, IReloadableSystem, IDisposable
{ {
public void Start(RendererSettings settings) public void Start(RendererSettings settings)
{ {
CreateAndInitializeWithWindow(settings); CreateAndInitializeWithWindow(settings);
} }
public void Reload() => ReloadResources();
/// <inheritdoc/> /// <inheritdoc/>
public void Dispose() public void Dispose()
{ {
@@ -89,6 +91,8 @@ namespace Voile.Rendering
/// </summary> /// </summary>
public abstract Vector2 MonitorSize { get; } public abstract Vector2 MonitorSize { get; }
protected abstract void ReloadResources();
/// <summary> /// <summary>
/// Creates a window. /// Creates a window.
/// </summary> /// </summary>
@@ -178,15 +182,14 @@ namespace Voile.Rendering
/// <param name="color">Color of the text.</param> /// <param name="color">Color of the text.</param>
public abstract void DrawSdfText(string text, int fontSize, Color color); 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> font, string text, Color color);
/// <summary> /// <summary>
/// Draws the texture. /// Draws the texture.
/// </summary> /// </summary>
/// <param name="id">Texture to draw.</param> /// <param name="id">Texture to draw.</param>
/// <param name="tint">Texture tint.</param> /// <param name="tint">Texture tint.</param>
public abstract void DrawTexture(Texture2d texture, Color tint); public abstract void DrawTexture(ResourceRef<Texture2d> textureResource, Color tint);
protected Vector2 transformPosition, transformPivot; protected Vector2 transformPosition, transformPivot;
protected float transformRotation; protected float transformRotation;

View File

@@ -78,7 +78,7 @@ namespace Voile.Rendering
_clearColor = color; _clearColor = color;
} }
public override void DrawText(Font font, string text, Color color) public override void DrawText(ResourceRef<Font> fontResource, string text, Color color)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@@ -117,7 +117,7 @@ namespace Voile.Rendering
} }
/// <inheritdoc /> /// <inheritdoc />
public override void DrawTexture(Texture2d texture, Color tint) public override void DrawTexture(ResourceRef<Texture2d> textureResource, Color tint)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@@ -394,6 +394,10 @@ namespace Voile.Rendering
throw new NotImplementedException(); throw new NotImplementedException();
} }
protected override void ReloadResources()
{
throw new NotImplementedException();
}
private Vector2 _windowSize = Vector2.Zero; private Vector2 _windowSize = Vector2.Zero;
private IWindow? _window; private IWindow? _window;

View File

@@ -17,6 +17,17 @@ public interface IStartableSystem<T>
void Start(T configuration); void Start(T configuration);
} }
/// <summary>
/// A system that has the ability to reload its internal state.
/// </summary>
public interface IReloadableSystem
{
/// <summary>
/// Reloads internal system state.
/// </summary>
void Reload();
}
public interface IUpdatableSystem public interface IUpdatableSystem
{ {
/// <summary> /// <summary>

View File

@@ -1,63 +0,0 @@
using System.Numerics;
using Voile.Rendering;
namespace Voile.UI;
/// <summary>
/// A basic container for UI elements. All container's children will update their constraints based on container's sizing and positioning.
/// </summary>
public class Container : UIElement
{
/// <summary>
/// Updates the sizes of the container and rearranges its children.
/// </summary>
/// <param name="position"></param>
/// <param name="size"></param>
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);
}
}
}

View File

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

View File

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

View File

@@ -1,6 +0,0 @@
namespace Voile.UI;
public struct PanelStyle
{
public Color BackgroundColor { get; set; }
}

View File

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

View File

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

View File

@@ -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<UIElement> children;
protected UIElement? parent;
private Logger _logger = new(nameof(UIElement));
}
[Flags]
public enum SizeFlags
{
ShrinkBegin,
ShrinkCenter,
ShrinkEnd,
Fill
}

View File

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