From bb0e20d039e93d3ec2d8a2a0750c0581971ef888 Mon Sep 17 00:00:00 2001 From: Harrison Deng Date: Sat, 12 Jan 2019 00:46:57 -0600 Subject: [PATCH] Added disposed checks, as well as safeties for strip primitives. --- RecrownedAthenaeum/Render/PrimitiveBatch.cs | 30 +++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/RecrownedAthenaeum/Render/PrimitiveBatch.cs b/RecrownedAthenaeum/Render/PrimitiveBatch.cs index eae9429..6b4914b 100644 --- a/RecrownedAthenaeum/Render/PrimitiveBatch.cs +++ b/RecrownedAthenaeum/Render/PrimitiveBatch.cs @@ -12,25 +12,24 @@ namespace RecrownedAthenaeum.Render public class PrimitiveBatch : IDisposable { List vertices; - int maxVertices; + public int MaxVertices { get { return vertices.Capacity; } set { vertices.Capacity = value; } } int bufferPosition; BasicEffect basicEffect; PrimitiveType primitiveType; int verticesPerPrimitive; GraphicsDevice graphicsDevice; bool began; - bool isDisposed; + bool disposed; /// /// Creates a batch used to draw primitives. /// /// The current graphics device being used. /// The current camera being used. - /// 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. + /// The amount of vertices every batch can hold before flushing. Default is 450. Should be changed to be the most optimal number if possible to prevent unnecessary resizing. 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; @@ -45,6 +44,7 @@ namespace RecrownedAthenaeum.Render public void Begin(PrimitiveType primitiveType) { if (began) throw new InvalidOperationException("Begin is being called twice before being ended."); + if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name); this.primitiveType = primitiveType; verticesPerPrimitive = 0; switch (primitiveType) @@ -63,6 +63,8 @@ namespace RecrownedAthenaeum.Render /// public void End() { + if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name); + if (!began) throw new InvalidOperationException("Begin must be called before ending."); Flush(); } @@ -75,9 +77,16 @@ namespace RecrownedAthenaeum.Render public void AddVertex(Vector2 vertex, Color color) { if (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex."); - if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip && bufferPosition + verticesPerPrimitive >= maxVertices && (bufferPosition % maxVertices == 0)) + if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name); + if (bufferPosition + verticesPerPrimitive >= MaxVertices && (bufferPosition % MaxVertices == 0)) { - Flush(); + if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip) + { + Flush(); + } else + { + throw new InvalidOperationException("Buffer size isn't large enough."); + } } vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color); @@ -90,6 +99,7 @@ namespace RecrownedAthenaeum.Render public void Flush() { if (!began) throw new InvalidOperationException("Begin needs to be called before flushing."); + if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name); if (bufferPosition == 0) return; graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive); @@ -104,10 +114,14 @@ namespace RecrownedAthenaeum.Render public void Dispose(bool disposing) { - if (disposing && !isDisposed) + if (disposing && !disposed) { basicEffect.Dispose(); - isDisposed = true; + disposed = true; + } + else + { + throw new ObjectDisposedException(typeof(PrimitiveBatch).Name); } } }