Files
Syntriax.Engine/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatcher.cs
Syntriax 988a6f67f2 BREAKING CHANGE: renamed original Behaviour class to BehaviourInternal, and replaced it with BehaviourBase
Original Behaviour was using old methods for detecting entering/exiting universe,
they are now all under the same hood and the original is kept for UniverseEntranceManager
because it needs to enter the universe without itself. The internal behaviour kept under
a subnamespace of "Core.Internal" for the purpose that it might come in handy for other use cases.
2025-10-22 16:50:19 +03:00

35 lines
1.2 KiB
C#

using System.Collections.Generic;
using Engine.Core;
namespace Engine.Integration.MonoGame;
public class SpriteBatcher : Behaviour, IFirstFrameUpdate, IDraw
{
private static Comparer<int> SortByPriority() => Comparer<int>.Create((x, y) => y.CompareTo(x));
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
private ISpriteBatch spriteBatch = null!;
private MonoGameCamera2D camera2D = null!;
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableSprite> drawableSprites = new(GetPriority(), SortByPriority());
public void FirstActiveFrame()
{
MonoGameWindowContainer windowContainer = Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
camera2D = Universe.FindRequiredBehaviour<MonoGameCamera2D>();
spriteBatch = new SpriteBatchWrapper(windowContainer.Window.GraphicsDevice);
drawableSprites.Unassign();
drawableSprites.Assign(Universe);
}
public void Draw()
{
spriteBatch.Begin(transformMatrix: camera2D.MatrixTransform);
for (int i = 0; i < drawableSprites.Count; i++)
drawableSprites[i].Draw(spriteBatch);
spriteBatch.End();
}
}