39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
|
|
using Engine.Core;
|
|
|
|
namespace Engine.Systems.Graphics;
|
|
|
|
public class TriangleBatcher : Behaviour, IFirstFrameUpdate, ILastFrameUpdate, IDraw
|
|
{
|
|
private static Comparer<int> SortByAscendingPriority() => Comparer<int>.Create((x, y) => x.CompareTo(y));
|
|
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
|
|
|
|
private readonly BehaviourCollector<ITriangleBatch> triangleBatches = new();
|
|
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableTriangle> drawableShapes = new(GetPriority(), SortByAscendingPriority());
|
|
|
|
public void FirstActiveFrame()
|
|
{
|
|
drawableShapes.Assign(Universe);
|
|
triangleBatches.Assign(Universe);
|
|
}
|
|
|
|
public void Draw()
|
|
{
|
|
for (int i = 0; i < triangleBatches.Count; i++)
|
|
{
|
|
ITriangleBatch triangleBatch = triangleBatches[i];
|
|
triangleBatch.Begin();
|
|
for (int j = 0; j < drawableShapes.Count; j++)
|
|
drawableShapes[j].Draw(triangleBatch);
|
|
triangleBatch.End();
|
|
}
|
|
}
|
|
|
|
public void LastActiveFrame()
|
|
{
|
|
triangleBatches.Unassign();
|
|
drawableShapes.Unassign();
|
|
}
|
|
}
|