From c9991f08c5437e53ea71c175159e82f868819bd6 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Fri, 11 Jan 2019 23:11:45 -0600 Subject: [PATCH] Primitive batch now works with strips. --- RecrownedAthenaeum/Render/PrimitiveBatch.cs | 26 ++++++++++----------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/RecrownedAthenaeum/Render/PrimitiveBatch.cs b/RecrownedAthenaeum/Render/PrimitiveBatch.cs index cb532b3..eae9429 100644 --- a/RecrownedAthenaeum/Render/PrimitiveBatch.cs +++ b/RecrownedAthenaeum/Render/PrimitiveBatch.cs @@ -11,8 +11,8 @@ namespace RecrownedAthenaeum.Render { public class PrimitiveBatch : IDisposable { - VertexPositionColor[] vertices; - int MaxVertices { get { return vertices.Length; } } + List vertices; + int maxVertices; int bufferPosition; BasicEffect basicEffect; PrimitiveType primitiveType; @@ -26,16 +26,16 @@ namespace RecrownedAthenaeum.Render /// /// The current graphics device being used. /// The current camera being used. - /// The amount of vertices every batch can hold before flushing. Default is 450. - public PrimitiveBatch(GraphicsDevice graphicsDevice, Camera2D camera, int verticesPerBatch = 450) + /// The amount of vertices every batch can hold before flushing. Default is 450. Should be changed to be the most optimal number if possible. Especially if using strip primitive types. + public PrimitiveBatch(GraphicsDevice graphicsDevice, Camera2D camera, int verticesPerBatch = 500) { this.graphicsDevice = graphicsDevice ?? throw new ArgumentNullException("Graphics device can't be null."); - + maxVertices = verticesPerBatch; basicEffect = new BasicEffect(graphicsDevice); basicEffect.VertexColorEnabled = true; basicEffect.View = camera.Matrix; - vertices = new VertexPositionColor[verticesPerBatch]; + vertices = new List(verticesPerBatch); } /// @@ -46,11 +46,12 @@ namespace RecrownedAthenaeum.Render { if (began) throw new InvalidOperationException("Begin is being called twice before being ended."); this.primitiveType = primitiveType; - this.verticesPerPrimitive = 0; + verticesPerPrimitive = 0; switch (primitiveType) { case PrimitiveType.LineList: verticesPerPrimitive = 2; break; case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break; + default: verticesPerPrimitive = 1; break; } basicEffect.CurrentTechnique.Passes[0].Apply(); @@ -74,27 +75,24 @@ namespace RecrownedAthenaeum.Render public void AddVertex(Vector2 vertex, Color color) { if (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex."); - if (bufferPosition + verticesPerPrimitive >= MaxVertices && (bufferPosition % MaxVertices == 0)) + if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip && bufferPosition + verticesPerPrimitive >= maxVertices && (bufferPosition % maxVertices == 0)) { Flush(); } - - vertices[bufferPosition].Position = (new Vector3(vertex, 0)); - vertices[bufferPosition].Color = color; + vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color); bufferPosition++; } /// - /// Flushes the batch. Automatically called if required. + /// Flushes the batch. Automatically called if required if using or . Otherwise, manual flushing is required. /// public void Flush() { if (!began) throw new InvalidOperationException("Begin needs to be called before flushing."); if (bufferPosition == 0) return; - graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, bufferPosition / verticesPerPrimitive); - + graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive); bufferPosition = 0; }