128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
using Voile;
|
|
using Voile.Resources;
|
|
using Voile.Utils;
|
|
using Voile.Input;
|
|
using Voile.Systems.Particles;
|
|
using System.Numerics;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Diagnostics;
|
|
|
|
public class TestGame : Game
|
|
{
|
|
public override string ResourceRoot => "Resources/";
|
|
|
|
public override void Initialize()
|
|
{
|
|
InitializeDefault();
|
|
|
|
_particleSystem = new ParticleSystem();
|
|
|
|
ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader());
|
|
|
|
Input.AddInputMapping("reload", new InputAction[] { new KeyInputAction(KeyboardKey.R) });
|
|
|
|
_particleSimStopwatch = new Stopwatch();
|
|
}
|
|
|
|
protected override void LoadResources()
|
|
{
|
|
if (!ResourceManager.TryLoad("fonts/Inter-Regular.ttf", out _font))
|
|
{
|
|
|
|
}
|
|
|
|
ResourceManager.TryLoad("icon.png", out _icon);
|
|
|
|
if (!ResourceManager.TryLoad("fire_effect.toml", out _fireEffect))
|
|
{
|
|
throw new Exception("Failed to load emitter settings!");
|
|
}
|
|
}
|
|
|
|
protected override void Ready()
|
|
{
|
|
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
|
}
|
|
|
|
protected override void Run()
|
|
{
|
|
while (Renderer.ShouldRun)
|
|
{
|
|
if (Input.IsActionPressed("reload"))
|
|
{
|
|
ResourceManager.Reload();
|
|
_particleSystem!.RestartEmitter(_emitterId);
|
|
}
|
|
|
|
if (Input.KeyboardKeyJustPressed(KeyboardKey.One))
|
|
{
|
|
_particleSystem.CreateEmitter(Input.GetMousePosition(), _fireEffect);
|
|
}
|
|
|
|
if (Input.IsMouseButtonDown(MouseButton.Left))
|
|
{
|
|
_particleSystem.SetEmitterPosition(_emitterId, Input.GetMousePosition());
|
|
}
|
|
|
|
_particleSimStopwatch = Stopwatch.StartNew();
|
|
_particleSystem!.Update(Renderer.FrameTime);
|
|
_particleSimStopwatch.Stop();
|
|
|
|
_renderStopwatch = Stopwatch.StartNew();
|
|
|
|
Renderer.BeginFrame();
|
|
Renderer.ClearBackground(Color.Black);
|
|
foreach (var emitter in _particleSystem!.Emitters)
|
|
{
|
|
DrawEmitter(emitter);
|
|
}
|
|
|
|
Renderer.ResetTransform();
|
|
Renderer.DrawText(_font, $"Particle Sim: {TimeSpan.FromTicks(_particleSimStopwatch.ElapsedTicks).TotalMilliseconds} ms", Color.White);
|
|
|
|
Renderer.SetTransform(new Vector2(0.0f, 16.0f), Vector2.Zero);
|
|
Renderer.DrawText(_font, $"Render: {TimeSpan.FromTicks(_lastRenderTime).TotalMilliseconds} ms", Color.White);
|
|
|
|
Renderer.EndFrame();
|
|
|
|
_lastRenderTime = _renderStopwatch.ElapsedTicks;
|
|
|
|
_renderStopwatch.Restart();
|
|
_particleSimStopwatch.Restart();
|
|
}
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
ShutdownDefault();
|
|
}
|
|
|
|
private void DrawEmitter(ParticleEmitter emitter)
|
|
{
|
|
Renderer.BeginBlended(Voile.Rendering.BlendMode.BlendAlpha);
|
|
|
|
var maxParticles = emitter.Settings.MaxParticles;
|
|
var particleSize = new Vector2(16.0f, 16.0f);
|
|
var pivot = particleSize / 2;
|
|
|
|
for (int i = maxParticles - 1; i >= 0; i--)
|
|
{
|
|
var particle = emitter.GetParticle(i);
|
|
|
|
Renderer.SetTransform(particle.Position, pivot, particle.Rotation);
|
|
Renderer.DrawRectangle(particleSize * particle.Scale, particle.Color);
|
|
}
|
|
|
|
Renderer.EndBlended();
|
|
}
|
|
|
|
[NotNull] private ParticleSystem _particleSystem;
|
|
|
|
private Stopwatch _particleSimStopwatch, _renderStopwatch;
|
|
private long _lastRenderTime;
|
|
private int _emitterId;
|
|
private ResourceRef<ParticleEmitterSettingsResource> _fireEffect;
|
|
private ResourceRef<Font> _font;
|
|
private ResourceRef<Texture2d> _icon;
|
|
private Logger _logger = new(nameof(TestGame));
|
|
} |