diff --git a/RecrownedAthenaeum/Render/PrimitiveBatch.cs b/RecrownedAthenaeum/Render/PrimitiveBatch.cs index d796444..275f9f2 100644 --- a/RecrownedAthenaeum/Render/PrimitiveBatch.cs +++ b/RecrownedAthenaeum/Render/PrimitiveBatch.cs @@ -11,19 +11,14 @@ namespace RecrownedAthenaeum.Render /// public class PrimitiveBatch : IDisposable { - List vertices; - /// - /// The maximum vertices expected. The further off this expectancy is from the true value, the less efficient. - /// - public int MaxVertices { get { return vertices.Capacity; } set { vertices.Capacity = value; } } + VertexPositionColor[] vertices; int bufferPosition; + int maxVertices; + BasicEffect basicEffect; PrimitiveType primitiveType; - - /// - /// The vertices per primitive. Can be ignored if using any of the list primitive types. Otherwise, needs to be manually set. - /// - public int verticesPerPrimitive; + + int verticesPerPrimitive; GraphicsDevice graphicsDevice; bool began; bool disposed; @@ -41,7 +36,8 @@ namespace RecrownedAthenaeum.Render this.camera = camera ?? (Configuration.Camera2D); basicEffect = new BasicEffect(this.graphicsDevice); basicEffect.VertexColorEnabled = true; - vertices = new List(verticesPerBatch); + vertices = new VertexPositionColor[verticesPerBatch+1]; + maxVertices = verticesPerBatch; } /// @@ -58,7 +54,7 @@ namespace RecrownedAthenaeum.Render { case PrimitiveType.LineList: verticesPerPrimitive = 2; break; case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break; - default: verticesPerPrimitive = -1; break; + default: verticesPerPrimitive = 1; break; } basicEffect.World = camera.worldMatrix; @@ -88,18 +84,23 @@ namespace RecrownedAthenaeum.Render { if (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex."); if (disposed) throw new ObjectDisposedException(this.GetType().Name); - if (bufferPosition + verticesPerPrimitive >= MaxVertices && (bufferPosition % MaxVertices == 0)) + if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip) { - if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip) + if (bufferPosition + verticesPerPrimitive > maxVertices && (bufferPosition % maxVertices == 0)) { Flush(); } else { - throw new InvalidOperationException("Buffer size isn't large enough."); + throw new InvalidOperationException("Buffer size doesn't match with primitive type."); } } - vertices.Add(new VertexPositionColor(new Vector3(vertex, 0), color)); + else if (bufferPosition + verticesPerPrimitive > maxVertices) + { + throw new InvalidOperationException("Buffer size isn't large enough."); + } + + vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color); bufferPosition++; } @@ -111,9 +112,7 @@ namespace RecrownedAthenaeum.Render if (!began) throw new InvalidOperationException("Begin needs to be called before flushing."); if (disposed) throw new ObjectDisposedException(this.GetType().Name); if (bufferPosition == 0) return; - if (verticesPerPrimitive == -1) throw new InvalidOperationException("Vertices per primitive variable not set and using primitive type other than list."); - graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive); - vertices.Clear(); + graphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertices, 0, bufferPosition / verticesPerPrimitive); bufferPosition = 0; }