refactor: moved drawable triangles into systems project for platform agnosticism

This commit is contained in:
2026-01-27 13:04:04 +03:00
parent 7c9973a5e7
commit 985f898327
8 changed files with 60 additions and 67 deletions

View File

@@ -1,12 +0,0 @@
using Microsoft.Xna.Framework;
using Engine.Core;
namespace Engine.Integration.MonoGame;
public interface ITriangleBatch
{
void Begin(Matrix? view = null, Matrix? projection = null);
void Draw(Triangle triangle, ColorRGBA colorRGBA);
void End();
}

View File

@@ -26,7 +26,7 @@ public class SpriteBatcher : Behaviour, IFirstFrameUpdate, IDraw
public void Draw()
{
spriteBatch.Begin(transformMatrix: camera2D.MatrixTransform);
spriteBatch.Begin(transformMatrix: camera2D.ViewMatrix.ToXnaMatrix());
for (int i = 0; i < drawableSprites.Count; i++)
drawableSprites[i].Draw(spriteBatch);
spriteBatch.End();

View File

@@ -1,40 +0,0 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Engine.Core;
namespace Engine.Integration.MonoGame;
public class TriangleBatcher : Behaviour, ITriangleBatch, 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 TriangleBatch triangleBatch = null!;
private MonoGameCamera2D camera2D = null!;
private readonly ActiveBehaviourCollectorOrdered<int, IDrawableTriangle> drawableShapes = new(GetPriority(), SortByAscendingPriority());
public void FirstActiveFrame()
{
MonoGameWindowContainer windowContainer = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
camera2D = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameCamera2D>();
triangleBatch = new(windowContainer.Window.GraphicsDevice);
drawableShapes.Unassign();
drawableShapes.Assign(BehaviourController.UniverseObject.Universe);
}
public void Draw()
{
triangleBatch.Begin(camera2D.MatrixTransform);
for (int i = 0; i < drawableShapes.Count; i++)
drawableShapes[i].Draw(triangleBatch);
triangleBatch.End();
}
public void Begin(Matrix? view = null, Matrix? projection = null) => triangleBatch.Begin(view, projection);
public void Draw(Triangle triangle, ColorRGBA colorRGBA) => triangleBatch.Draw(triangle, colorRGBA);
public void End() => triangleBatch.End();
}

View File

@@ -2,24 +2,26 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Engine.Core;
using Engine.Systems.Graphics;
namespace Engine.Integration.MonoGame;
public class TriangleBatch : ITriangleBatch
public class MonoGameTriangleBatch : Behaviour, ITriangleBatch, IFirstFrameUpdate
{
private readonly GraphicsDevice graphicsDevice;
private readonly VertexBuffer vertexBuffer = default!;
private GraphicsDevice graphicsDevice = null!;
private VertexBuffer vertexBuffer = default!;
private readonly VertexPositionColor[] vertices = new VertexPositionColor[1024];
private int verticesIndex = 0;
private Matrix view = Matrix.Identity;
private Matrix projection = Matrix.Identity;
private readonly BasicEffect basicEffect = null!;
private BasicEffect basicEffect = null!;
private readonly RasterizerState rasterizerState = new() { CullMode = CullMode.None };
public TriangleBatch(GraphicsDevice graphicsDevice)
public void FirstActiveFrame()
{
GraphicsDevice graphicsDevice = Universe.FindRequiredBehaviour<MonoGameWindowContainer>().Window.GraphicsDevice;
this.graphicsDevice = graphicsDevice;
basicEffect = new(graphicsDevice);
basicEffect.VertexColorEnabled = true;
@@ -41,15 +43,15 @@ public class TriangleBatch : ITriangleBatch
vertices[verticesIndex++] = new(new(C.X, C.Y, 0f), color);
}
public void Begin(Matrix? view = null, Matrix? projection = null)
public void Begin(Matrix4x4? view = null, Matrix4x4? projection = null)
{
if (view != null)
this.view = view.Value;
this.view = view.Value.ToXnaMatrix();
else
this.view = Matrix.Identity;
if (projection != null)
this.projection = projection.Value;
this.projection = projection.Value.ToXnaMatrix();
else
{
Viewport viewport = graphicsDevice.Viewport;

View File

@@ -2,9 +2,9 @@ using System.Collections.Generic;
using Engine.Core;
namespace Engine.Integration.MonoGame;
namespace Engine.Systems.Graphics;
public class DrawableShape : Behaviour2D, IDrawableTriangle, IPreDraw
public class DrawableShape2D : Behaviour2D, IDrawableTriangle, IPreDraw
{
private readonly Shape2D shape = new([]);
private readonly List<Triangle> worldTriangles = [];
@@ -23,7 +23,7 @@ public class DrawableShape : Behaviour2D, IDrawableTriangle, IPreDraw
protected void UpdateWorldShape() => shape.Transform(Transform, worldShape);
public DrawableShape() => shape = Shape2D.Triangle;
public DrawableShape(Shape2D shape) => this.shape = shape;
public DrawableShape(Shape2D shape, ColorRGB color) { this.shape = shape; this.color = color; }
public DrawableShape2D() => shape = Shape2D.Triangle;
public DrawableShape2D(Shape2D shape) => this.shape = shape;
public DrawableShape2D(Shape2D shape, ColorRGB color) { this.shape = shape; this.color = color; }
}

View File

@@ -1,6 +1,6 @@
using Engine.Core;
namespace Engine.Integration.MonoGame;
namespace Engine.Systems.Graphics;
public interface IDrawableTriangle : IBehaviour
{

View 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();
}

View 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();
}
}