Split the platform out of Core: new Host library + HeadlessHost
CI / build-test (push) Successful in 1m24s

Core now depends only on Friflo.Engine.ECS — no MonoGame, no platform.
The windowed MonoGame host moves to the new MrGameEng.Host library:
GameHost, GameHostOptions, visual scene transitions (OverlayTransition,
Transitions.Fade/Wipe, TransitionRenderer) and the Input feature
(namespace MrGameEng.Input is unchanged). Transition timing stays in
Core (SceneManager exposes ActiveTransition/TransitionCoverage/
TransitionPhase; the host draws the overlay). EngineContext loses its
GraphicsDevice property: hosts publish the device as a service and
graphics code reads it via context.GetGraphicsDevice() in Graphics.

Core gains HeadlessHost: a fixed-timestep loop without a window or GPU
(Tick/RunTicks, Run with wall-clock pacing and lag resync) for dedicated
servers, batch simulation and tests. Graphics and Audio now carry their
own MonoGame.Framework.DesktopGL reference instead of inheriting it
from Core.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Leonid Pershin
2026-06-12 23:04:34 +03:00
co-authored by Claude Fable 5
parent 79d406a9f4
commit 2ac074004a
33 changed files with 560 additions and 184 deletions
@@ -0,0 +1,117 @@
using MrGameEng.Core;
using Xunit;
namespace MrGameEng.Core.Tests;
public class HeadlessHostTests
{
private sealed class CountingScene : Scene
{
public int LoadCount;
public int UnloadCount;
public int UpdateCount;
protected override void OnLoad() => LoadCount++;
protected override void OnUnload() => UnloadCount++;
public override void Update(GameClock clock)
{
UpdateCount++;
base.Update(clock);
}
}
[Fact]
public void RunTicks_AdvancesClock_ByExactFixedStep()
{
var scene = new CountingScene();
using var host = new HeadlessHost(new HeadlessHostOptions { TicksPerSecond = 10f }, scene);
host.RunTicks(30);
Assert.Equal(0.1f, host.FixedDeltaTime, 3);
Assert.Equal(30, host.TickCount);
Assert.Equal(3.0, host.Context.Clock.TotalTime, 3);
Assert.Equal(30, scene.UpdateCount);
}
[Fact]
public void FirstTick_LoadsTheInitialScene()
{
var scene = new CountingScene();
using var host = new HeadlessHost(new HeadlessHostOptions(), scene);
Assert.Equal(0, scene.LoadCount);
host.Tick();
Assert.Equal(1, scene.LoadCount);
Assert.Same(scene, host.Context.Scenes.Current);
}
[Fact]
public void Dispose_UnloadsTheActiveScene()
{
var scene = new CountingScene();
var host = new HeadlessHost(new HeadlessHostOptions(), scene);
host.Tick();
host.Dispose();
Assert.Equal(1, scene.UnloadCount);
Assert.Null(host.Context.Scenes.Current);
}
[Fact]
public void Run_StopsWhenCancelled()
{
using var cancellation = new CancellationTokenSource();
var scene = new CancellingScene(cancellation, afterTicks: 5);
using var host = new HeadlessHost(new HeadlessHostOptions { Realtime = false }, scene);
host.Run(cancellation.Token);
Assert.Equal(5, scene.UpdateCount);
}
[Fact]
public void TimeScale_StillApplies_OnTopOfFixedStep()
{
var scene = new CountingScene();
using var host = new HeadlessHost(new HeadlessHostOptions { TicksPerSecond = 10f }, scene);
host.Context.Clock.TimeScale = 3f;
host.RunTicks(10);
Assert.Equal(3.0, host.Context.Clock.TotalTime, 3);
Assert.Equal(1.0, host.Context.Clock.UnscaledTotalTime, 3);
}
[Fact]
public void NonPositiveTickRate_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(() =>
new HeadlessHost(new HeadlessHostOptions { TicksPerSecond = 0f }, new CountingScene())
);
}
private sealed class CancellingScene(CancellationTokenSource cancellation, int afterTicks)
: Scene
{
public int UpdateCount;
protected override void OnLoad() { }
public override void Update(GameClock clock)
{
UpdateCount++;
if (UpdateCount >= afterTicks)
{
cancellation.Cancel();
}
base.Update(clock);
}
}
}
@@ -15,6 +15,14 @@ public class SceneTransitionTests
protected override void OnUnload() => UnloadCount++;
}
// Тайминг-машина живёт в ядре и не зависит от визуала перехода —
// тестируем на голой заглушке с длительностями, как у Fade.
private sealed class TimedTransition(float outDuration, float inDuration)
: Transition(outDuration, inDuration);
private static Transition Fade(float duration) =>
new TimedTransition(duration / 2f, duration / 2f);
private static void Tick(EngineContext context, float seconds)
{
context.Clock.Advance(seconds);
@@ -31,7 +39,7 @@ public class SceneTransitionTests
Tick(context, 0.016f);
// Fade(1.0) → фаза закрытия 0.5 c, фаза открытия 0.5 c.
context.Scenes.Switch(second, Transition.Fade(1f));
context.Scenes.Switch(second, Fade(1f));
Tick(context, 0.2f);
Assert.True(context.Scenes.IsTransitioning);
@@ -61,7 +69,7 @@ public class SceneTransitionTests
Tick(context, 0.016f);
context.Clock.TimeScale = 0f; // игра на паузе
context.Scenes.Switch(second, Transition.Fade(0.2f));
context.Scenes.Switch(second, Fade(0.2f));
Tick(context, 0.15f);
Tick(context, 0.15f);
@@ -79,7 +87,7 @@ public class SceneTransitionTests
context.Scenes.Switch(first);
Tick(context, 0.016f);
context.Scenes.Switch(second, Transition.Fade(1f));
context.Scenes.Switch(second, Fade(1f));
Tick(context, 0.1f);
context.Scenes.Switch(third); // передумали, пока экран закрывается
@@ -99,12 +107,12 @@ public class SceneTransitionTests
context.Scenes.Switch(first);
Tick(context, 0.016f);
context.Scenes.Switch(second, Transition.Fade(1f)); // 0.5 c закрытие + 0.5 c открытие
context.Scenes.Switch(second, Fade(1f)); // 0.5 c закрытие + 0.5 c открытие
Tick(context, 0.6f); // закрыто, своп на second, началось открытие
Assert.Same(second, context.Scenes.Current);
Tick(context, 0.25f); // открытие наполовину (coverage ~0.5)
context.Scenes.Switch(third, Transition.Fade(1f)); // передумали во время открытия
context.Scenes.Switch(third, Fade(1f)); // передумали во время открытия
Tick(context, 0.05f); // экран снова закрывается — свопа ещё нет
Assert.Same(second, context.Scenes.Current);
@@ -124,7 +132,7 @@ public class SceneTransitionTests
context.Scenes.Switch(first);
Tick(context, 0.016f);
context.Scenes.Switch(second, Transition.Fade(0f));
context.Scenes.Switch(second, Fade(0f));
Tick(context, 0.016f);
Tick(context, 0.016f);
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MrGameEng.Host\MrGameEng.Host.csproj" />
</ItemGroup>
</Project>