From 985f8983274dd3dac414f0b7904aae8747450f4f Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 27 Jan 2026 13:04:04 +0300 Subject: [PATCH] refactor: moved drawable triangles into systems project for platform agnosticism --- .../Abstract/ITriangleBatch.cs | 12 ------ .../Behaviours/SpriteBatcher.cs | 2 +- .../Behaviours/TriangleBatcher.cs | 40 ------------------- ...angleBatch.cs => MonoGameTriangleBatch.cs} | 18 +++++---- .../Graphics/DrawableShape2D.cs | 10 ++--- .../Graphics}/IDrawableTriangle.cs | 2 +- Engine.Systems/Graphics/ITriangleBatch.cs | 10 +++++ Engine.Systems/Graphics/TriangleBatcher.cs | 33 +++++++++++++++ 8 files changed, 60 insertions(+), 67 deletions(-) delete mode 100644 Engine.Integration/Engine.Integration.MonoGame/Abstract/ITriangleBatch.cs delete mode 100644 Engine.Integration/Engine.Integration.MonoGame/Behaviours/TriangleBatcher.cs rename Engine.Integration/Engine.Integration.MonoGame/{TriangleBatch.cs => MonoGameTriangleBatch.cs} (78%) rename Engine.Integration/Engine.Integration.MonoGame/Behaviours/DrawableShape.cs => Engine.Systems/Graphics/DrawableShape2D.cs (66%) rename {Engine.Integration/Engine.Integration.MonoGame/Abstract => Engine.Systems/Graphics}/IDrawableTriangle.cs (75%) create mode 100644 Engine.Systems/Graphics/ITriangleBatch.cs create mode 100644 Engine.Systems/Graphics/TriangleBatcher.cs diff --git a/Engine.Integration/Engine.Integration.MonoGame/Abstract/ITriangleBatch.cs b/Engine.Integration/Engine.Integration.MonoGame/Abstract/ITriangleBatch.cs deleted file mode 100644 index 1de8f80..0000000 --- a/Engine.Integration/Engine.Integration.MonoGame/Abstract/ITriangleBatch.cs +++ /dev/null @@ -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(); -} diff --git a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatcher.cs b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatcher.cs index b9ca10e..7ca0b88 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatcher.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/SpriteBatcher.cs @@ -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(); diff --git a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/TriangleBatcher.cs b/Engine.Integration/Engine.Integration.MonoGame/Behaviours/TriangleBatcher.cs deleted file mode 100644 index b8bf5d0..0000000 --- a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/TriangleBatcher.cs +++ /dev/null @@ -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 SortByAscendingPriority() => Comparer.Create((x, y) => x.CompareTo(y)); - private static System.Func GetPriority() => (b) => b.Priority; - - private TriangleBatch triangleBatch = null!; - private MonoGameCamera2D camera2D = null!; - - private readonly ActiveBehaviourCollectorOrdered drawableShapes = new(GetPriority(), SortByAscendingPriority()); - - public void FirstActiveFrame() - { - MonoGameWindowContainer windowContainer = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour(); - camera2D = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour(); - - 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(); -} diff --git a/Engine.Integration/Engine.Integration.MonoGame/TriangleBatch.cs b/Engine.Integration/Engine.Integration.MonoGame/MonoGameTriangleBatch.cs similarity index 78% rename from Engine.Integration/Engine.Integration.MonoGame/TriangleBatch.cs rename to Engine.Integration/Engine.Integration.MonoGame/MonoGameTriangleBatch.cs index 499a6ed..7f8cb2c 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/TriangleBatch.cs +++ b/Engine.Integration/Engine.Integration.MonoGame/MonoGameTriangleBatch.cs @@ -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().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; diff --git a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/DrawableShape.cs b/Engine.Systems/Graphics/DrawableShape2D.cs similarity index 66% rename from Engine.Integration/Engine.Integration.MonoGame/Behaviours/DrawableShape.cs rename to Engine.Systems/Graphics/DrawableShape2D.cs index 0078088..2a6d179 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/Behaviours/DrawableShape.cs +++ b/Engine.Systems/Graphics/DrawableShape2D.cs @@ -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 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; } } diff --git a/Engine.Integration/Engine.Integration.MonoGame/Abstract/IDrawableTriangle.cs b/Engine.Systems/Graphics/IDrawableTriangle.cs similarity index 75% rename from Engine.Integration/Engine.Integration.MonoGame/Abstract/IDrawableTriangle.cs rename to Engine.Systems/Graphics/IDrawableTriangle.cs index 7d3372d..2606b18 100644 --- a/Engine.Integration/Engine.Integration.MonoGame/Abstract/IDrawableTriangle.cs +++ b/Engine.Systems/Graphics/IDrawableTriangle.cs @@ -1,6 +1,6 @@ using Engine.Core; -namespace Engine.Integration.MonoGame; +namespace Engine.Systems.Graphics; public interface IDrawableTriangle : IBehaviour { diff --git a/Engine.Systems/Graphics/ITriangleBatch.cs b/Engine.Systems/Graphics/ITriangleBatch.cs new file mode 100644 index 0000000..c25d6aa --- /dev/null +++ b/Engine.Systems/Graphics/ITriangleBatch.cs @@ -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(); +} diff --git a/Engine.Systems/Graphics/TriangleBatcher.cs b/Engine.Systems/Graphics/TriangleBatcher.cs new file mode 100644 index 0000000..8373833 --- /dev/null +++ b/Engine.Systems/Graphics/TriangleBatcher.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; + +using Engine.Core; + +namespace Engine.Systems.Graphics; + +public class TriangleBatcher : Behaviour, IFirstFrameUpdate, IDraw +{ + private static Comparer SortByAscendingPriority() => Comparer.Create((x, y) => x.CompareTo(y)); + private static System.Func GetPriority() => (b) => b.Priority; + + private ITriangleBatch triangleBatch = null!; + private ICamera camera = null!; + + private readonly ActiveBehaviourCollectorOrdered drawableShapes = new(GetPriority(), SortByAscendingPriority()); + + public void FirstActiveFrame() + { + camera = Universe.FindRequiredBehaviour(); + + triangleBatch = Universe.FindRequiredBehaviour(); + 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(); + } +}