using System;
using Amphora;
using Azimuth;
using Blacklight.Core;
using Blacklight.Core.Cameras;
using Blacklight.Core.Keyboard;
namespace Konis.Testbed
{
///
/// The whole testbed behind its host: the particle demo, the click spawner, the UI and the stats, driven one frame
/// at a time. A host supplies the window and its input and calls once per frame.
///
public class TestbedWorld
{
// A host can stall for a while, such as a window being dragged or a tab in the background, and the next frame
// would otherwise get all of it as one step.
public static readonly TimeSpan LongestFrame = TimeSpan.FromMilliseconds(100);
private readonly WindowHandle window;
private readonly IPointerSource pointerSource;
private readonly ParticleDemo demo;
private readonly FrameStats stats;
private readonly TestbedUi ui;
///
/// The pan and zoom controller already bound to . It is added to the router here.
///
public TestbedWorld(WindowHandle window, Camera camera, MicroscopeController controller,
InputRouter inputRouter, IPointerSource pointerSource, IPointerLock pointerLock, ITextSource textSource,
IHostClipboard clipboard, VerticalSyncMode verticalSync, Rand random)
{
this.window = window;
this.pointerSource = pointerSource;
demo = new ParticleDemo(window, camera, random);
// The left button spawns, so the camera drags with the middle one.
controller.Options.DragNavigateButton = PointerButtons.Middle;
inputRouter.AddLowestPriority(controller);
inputRouter.AddHighestPriority(new ClickSpawner(demo, window, camera));
// Last, so the UI sits above the click spawner and a click on a control doesn't also spawn.
stats = new FrameStats(verticalSync);
window.MeasureGpuCompletion = stats.MeasuresGpuWait;
ui = new TestbedUi(window, inputRouter, pointerLock, textSource, clipboard, demo, stats);
}
///
/// Spawns every effect and selects the first, so the next frame draws particles and measures stats for both the
/// selected effect and the whole world.
///
public void SpawnEveryEffect()
{
for (int i = 0; i < Effects.All.Length; i++)
{
demo.EffectIndex = i;
demo.Spawn(new Azimuth.Vector3(0, 0, 0), preWarmSeconds: 1f);
}
// With nothing selected the stats cover every effect, which is a different path from one effect's.
demo.MeasureStats();
demo.EffectIndex = 0;
demo.Select(demo.Spawned[0]);
}
///
/// Runs one frame of the elapsed time, up to , and submits it.
///
public void Tick(TimeSpan elapsed)
{
if (elapsed > LongestFrame)
{
elapsed = LongestFrame;
}
Duration real = Duration.FromSeconds(elapsed.TotalSeconds);
stats.BeginFrame();
demo.Update(new FxTime(real, real * demo.Speed, demo.Speed));
stats.EndSimulation();
ui.Update(real, pointerSource.PointerState);
stats.EndInterfaceUpdate();
// Only FrameReset empties the batch pools, so the reset/queue/submit triple has to stay together.
demo.Queue();
ui.Render(window);
stats.EndDraw();
window.SubmitDrawCalls();
stats.EndFrame(window.LastFrameCost);
}
///
/// Lets go of the window and its input.
///
public void Unbind()
{
ui.Unbind();
}
}
}