From ad365dc7225df5df0d8be0729f6c62490c845f77 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Fri, 25 Jul 2025 21:40:57 +0300 Subject: [PATCH] feat: monogame content loader interface added --- .../Abstract/ILoadContent.cs | 10 ++++ .../Behaviours/LoadContentManager.cs | 53 +++++++++++++++++++ .../MonoGameWindow.cs | 3 ++ 3 files changed, 66 insertions(+) create mode 100644 Engine.Integration/Engine.Integration.MonoGame/Abstract/ILoadContent.cs create mode 100644 Engine.Integration/Engine.Integration.MonoGame/Behaviours/LoadContentManager.cs diff --git a/Engine.Integration/Engine.Integration.MonoGame/Abstract/ILoadContent.cs b/Engine.Integration/Engine.Integration.MonoGame/Abstract/ILoadContent.cs new file mode 100644 index 0000000..042a5b7 --- /dev/null +++ b/Engine.Integration/Engine.Integration.MonoGame/Abstract/ILoadContent.cs @@ -0,0 +1,10 @@ +using Microsoft.Xna.Framework.Content; + +using Syntriax.Engine.Core; + +namespace Syntriax.Engine.Integration.MonoGame; + +public interface ILoadContent : IBehaviour +{ + void LoadContent(ContentManager content); +} diff --git a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/LoadContentManager.cs b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/LoadContentManager.cs new file mode 100644 index 0000000..ccaf8da --- /dev/null +++ b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/LoadContentManager.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using Syntriax.Engine.Core; + +namespace Syntriax.Engine.Integration.MonoGame; + +public class LoadContentManager : Behaviour, IFirstFrameUpdate +{ + // We use Ascending order because we are using reverse for loop to call them + private static Comparer SortByAscendingPriority() => Comparer.Create((x, y) => x.Priority.CompareTo(y.Priority)); + + private readonly ActiveBehaviourCollectorSorted loadContents = new() { SortBy = SortByAscendingPriority() }; + private readonly List toCallLoadContents = new(32); + + private MonoGameWindowContainer monoGameWindowContainer = null!; + + public void FirstActiveFrame() + { + monoGameWindowContainer = Universe.FindRequiredBehaviour(); + } + + protected override void OnEnteredUniverse(IUniverse universe) + { + loadContents.Assign(universe); + + universe.OnPreUpdate.AddListener(OnPreUpdate); + } + + protected override void OnExitedUniverse(IUniverse universe) + { + loadContents.Unassign(); + + universe.OnPreUpdate.RemoveListener(OnPreUpdate); + } + + private void OnPreUpdate(IUniverse sender, IUniverse.UpdateArguments args) + { + for (int i = toCallLoadContents.Count - 1; i >= 0; i--) + { + toCallLoadContents[i].LoadContent(monoGameWindowContainer.Window.Content); + toCallLoadContents.RemoveAt(i); + } + } + + private void OnFirstFrameCollected(IBehaviourCollector sender, IBehaviourCollector.BehaviourCollectedArguments args) + { + toCallLoadContents.Add(args.BehaviourCollected); + } + + public LoadContentManager() + { + loadContents.OnCollected.AddListener(OnFirstFrameCollected); + } +} diff --git a/Engine.Integration/Engine.Integration.MonoGame/MonoGameWindow.cs b/Engine.Integration/Engine.Integration.MonoGame/MonoGameWindow.cs index 62e9ae5..8b8740c 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/MonoGameWindow.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/MonoGameWindow.cs @@ -21,6 +21,9 @@ public class MonoGameWindow : Game Universe.InstantiateUniverseObject().SetUniverseObject("Window Container") .BehaviourController.AddBehaviour(this); + + Universe.InstantiateUniverseObject().SetUniverseObject("Content Loader") + .BehaviourController.AddBehaviour(); } protected override void Initialize()