BaseGame with fixed timestep.
This commit is contained in:
@@ -7,25 +7,14 @@ using System.Numerics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics;
|
||||
|
||||
public class TestGame : Game
|
||||
public class TestGame : BaseGame
|
||||
{
|
||||
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()
|
||||
{
|
||||
ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader());
|
||||
|
||||
if (!ResourceManager.TryLoad("fonts/Inter-Regular.ttf", out _font))
|
||||
{
|
||||
|
||||
@@ -41,60 +30,53 @@ public class TestGame : Game
|
||||
|
||||
protected override void Ready()
|
||||
{
|
||||
_particleSystem = new ParticleSystem();
|
||||
Input.AddInputMapping("reload", new InputAction[] { new KeyInputAction(KeyboardKey.R) });
|
||||
_particleSimStopwatch = new Stopwatch();
|
||||
|
||||
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
||||
}
|
||||
|
||||
protected override void Run()
|
||||
|
||||
protected override void Update(double deltaTime)
|
||||
{
|
||||
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();
|
||||
}
|
||||
_particleSimStopwatch = Stopwatch.StartNew();
|
||||
_particleSystem!.Update(deltaTime);
|
||||
_particleSimStopwatch.Stop();
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
protected override void Render(double deltaTime)
|
||||
{
|
||||
ShutdownDefault();
|
||||
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());
|
||||
}
|
||||
|
||||
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: {RenderFrameTime.TotalMilliseconds:F1} ms", Color.White);
|
||||
|
||||
Renderer.SetTransform(new Vector2(0.0f, 32.0f), Vector2.Zero);
|
||||
Renderer.DrawText(_font, $"Update: {UpdateTimeStep * 1000:F1} ms", Color.White);
|
||||
}
|
||||
|
||||
private void DrawEmitter(ParticleEmitter emitter)
|
||||
@@ -118,8 +100,7 @@ public class TestGame : Game
|
||||
|
||||
[NotNull] private ParticleSystem _particleSystem;
|
||||
|
||||
private Stopwatch _particleSimStopwatch, _renderStopwatch;
|
||||
private long _lastRenderTime;
|
||||
private Stopwatch _particleSimStopwatch;
|
||||
private int _emitterId;
|
||||
private ResourceRef<ParticleEmitterSettingsResource> _fireEffect;
|
||||
private ResourceRef<Font> _font;
|
||||
|
||||
Reference in New Issue
Block a user