refactor: moved drawable triangles into systems project for platform agnosticism
This commit is contained in:
29
Engine.Systems/Graphics/DrawableShape2D.cs
Normal file
29
Engine.Systems/Graphics/DrawableShape2D.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public class DrawableShape2D : Behaviour2D, IDrawableTriangle, IPreDraw
|
||||
{
|
||||
private readonly Shape2D shape = new([]);
|
||||
private readonly List<Triangle> worldTriangles = [];
|
||||
private readonly Shape2D worldShape = new([]);
|
||||
protected ColorRGB color = new(255, 255, 255);
|
||||
|
||||
public void PreDraw() => UpdateWorldShape();
|
||||
|
||||
public void Draw(ITriangleBatch triangleBatch)
|
||||
{
|
||||
worldShape.ToTrianglesConvex(worldTriangles);
|
||||
|
||||
foreach (Triangle triangle in worldTriangles)
|
||||
triangleBatch.Draw(new(triangle.C, triangle.B, triangle.A), color);
|
||||
}
|
||||
|
||||
protected void UpdateWorldShape() => shape.Transform(Transform, worldShape);
|
||||
|
||||
public DrawableShape2D() => shape = Shape2D.Triangle;
|
||||
public DrawableShape2D(Shape2D shape) => this.shape = shape;
|
||||
public DrawableShape2D(Shape2D shape, ColorRGB color) { this.shape = shape; this.color = color; }
|
||||
}
|
||||
8
Engine.Systems/Graphics/IDrawableTriangle.cs
Normal file
8
Engine.Systems/Graphics/IDrawableTriangle.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public interface IDrawableTriangle : IBehaviour
|
||||
{
|
||||
void Draw(ITriangleBatch triangleBatch);
|
||||
}
|
||||
10
Engine.Systems/Graphics/ITriangleBatch.cs
Normal file
10
Engine.Systems/Graphics/ITriangleBatch.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public interface ITriangleBatch
|
||||
{
|
||||
void Begin(Matrix4x4? view = null, Matrix4x4? projection = null);
|
||||
void Draw(Triangle triangle, ColorRGBA colorRGBA);
|
||||
void End();
|
||||
}
|
||||
33
Engine.Systems/Graphics/TriangleBatcher.cs
Normal file
33
Engine.Systems/Graphics/TriangleBatcher.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Engine.Core;
|
||||
|
||||
namespace Engine.Systems.Graphics;
|
||||
|
||||
public class TriangleBatcher : Behaviour, IFirstFrameUpdate, 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 ITriangleBatch triangleBatch = null!;
|
||||
private ICamera camera = null!;
|
||||
|
||||
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableTriangle> drawableShapes = new(GetPriority(), SortByAscendingPriority());
|
||||
|
||||
public void FirstActiveFrame()
|
||||
{
|
||||
camera = Universe.FindRequiredBehaviour<ICamera>();
|
||||
|
||||
triangleBatch = Universe.FindRequiredBehaviour<ITriangleBatch>();
|
||||
drawableShapes.Unassign();
|
||||
drawableShapes.Assign(Universe);
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
triangleBatch.Begin(camera.ViewMatrix, camera.ProjectionMatrix);
|
||||
for (int i = 0; i < drawableShapes.Count; i++)
|
||||
drawableShapes[i].Draw(triangleBatch);
|
||||
triangleBatch.End();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user