114 lines
3.2 KiB
C#
114 lines
3.2 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("my_sound", "sounds/test_sound.ogg", out Sound? _testSound))
|
|
// {
|
|
|
|
// }
|
|
|
|
if (!ResourceManager.TryLoad("fonts/Inter-Regular.ttf", out _font))
|
|
{
|
|
|
|
}
|
|
|
|
ResourceManager.TryLoad("icon.png", out _icon);
|
|
|
|
if (!ResourceManager.TryLoad("test_emitter.toml", out _emitterSettings))
|
|
{
|
|
throw new Exception("Failed to load emitter settings!");
|
|
}
|
|
}
|
|
|
|
protected override void Ready()
|
|
{
|
|
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _emitterSettings);
|
|
}
|
|
|
|
protected override void Run()
|
|
{
|
|
while (Renderer.ShouldRun)
|
|
{
|
|
if (Input.IsActionPressed("reload"))
|
|
{
|
|
ResourceManager.Reload();
|
|
_particleSystem!.RestartEmitter(_emitterId);
|
|
}
|
|
|
|
_particleSimStopwatch = Stopwatch.StartNew();
|
|
_particleSystem!.Update(Renderer.FrameTime);
|
|
_particleSimStopwatch.Stop();
|
|
|
|
Renderer.BeginFrame();
|
|
Renderer.ClearBackground(Color.Black);
|
|
foreach (var emitter in _particleSystem!.Emitters)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
ShutdownDefault();
|
|
}
|
|
|
|
private void DrawEmitter(ParticleEmitter emitter)
|
|
{
|
|
Renderer.BeginBlended(Voile.Rendering.BlendMode.BlendAlpha);
|
|
|
|
for (int i = 0; i < emitter.Particles.Length; i++)
|
|
{
|
|
var particle = emitter.Particles[i];
|
|
|
|
var color = new Color(particle.ColorArgb);
|
|
|
|
Renderer.SetTransform(emitter.OriginPosition + particle.Position, Vector2.Zero);
|
|
Renderer.DrawCircle(16f * particle.Scale, color);
|
|
}
|
|
|
|
Renderer.EndBlended();
|
|
}
|
|
|
|
[NotNull] private ParticleSystem _particleSystem;
|
|
|
|
private Stopwatch _particleSimStopwatch;
|
|
private int _emitterId;
|
|
private ResourceRef<ParticleEmitterSettingsResource> _emitterSettings;
|
|
private ResourceRef<Font> _font;
|
|
private ResourceRef<Texture2d> _icon;
|
|
private Logger _logger = new(nameof(TestGame));
|
|
} |