Added disposed checks, as well as safeties for strip primitives.

This commit is contained in:
Harrison Deng 2019-01-12 00:46:57 -06:00
parent c9991f08c5
commit bb0e20d039

View File

@ -12,25 +12,24 @@ namespace RecrownedAthenaeum.Render
public class PrimitiveBatch : IDisposable public class PrimitiveBatch : IDisposable
{ {
List<VertexPositionColor> vertices; List<VertexPositionColor> vertices;
int maxVertices; public int MaxVertices { get { return vertices.Capacity; } set { vertices.Capacity = value; } }
int bufferPosition; int bufferPosition;
BasicEffect basicEffect; BasicEffect basicEffect;
PrimitiveType primitiveType; PrimitiveType primitiveType;
int verticesPerPrimitive; int verticesPerPrimitive;
GraphicsDevice graphicsDevice; GraphicsDevice graphicsDevice;
bool began; bool began;
bool isDisposed; bool disposed;
/// <summary> /// <summary>
/// Creates a batch used to draw primitives. /// Creates a batch used to draw primitives.
/// </summary> /// </summary>
/// <param name="graphicsDevice">The current graphics device being used.</param> /// <param name="graphicsDevice">The current graphics device being used.</param>
/// <param name="camera">The current camera being used.</param> /// <param name="camera">The current camera being used.</param>
/// <param name="verticesPerBatch">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.</param> /// <param name="verticesPerBatch">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.</param>
public PrimitiveBatch(GraphicsDevice graphicsDevice, Camera2D camera, int verticesPerBatch = 500) public PrimitiveBatch(GraphicsDevice graphicsDevice, Camera2D camera, int verticesPerBatch = 500)
{ {
this.graphicsDevice = graphicsDevice ?? throw new ArgumentNullException("Graphics device can't be null."); this.graphicsDevice = graphicsDevice ?? throw new ArgumentNullException("Graphics device can't be null.");
maxVertices = verticesPerBatch;
basicEffect = new BasicEffect(graphicsDevice); basicEffect = new BasicEffect(graphicsDevice);
basicEffect.VertexColorEnabled = true; basicEffect.VertexColorEnabled = true;
basicEffect.View = camera.Matrix; basicEffect.View = camera.Matrix;
@ -45,6 +44,7 @@ namespace RecrownedAthenaeum.Render
public void Begin(PrimitiveType primitiveType) public void Begin(PrimitiveType primitiveType)
{ {
if (began) throw new InvalidOperationException("Begin is being called twice before being ended."); if (began) throw new InvalidOperationException("Begin is being called twice before being ended.");
if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name);
this.primitiveType = primitiveType; this.primitiveType = primitiveType;
verticesPerPrimitive = 0; verticesPerPrimitive = 0;
switch (primitiveType) switch (primitiveType)
@ -63,6 +63,8 @@ namespace RecrownedAthenaeum.Render
/// </summary> /// </summary>
public void End() public void End()
{ {
if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name);
if (!began) throw new InvalidOperationException("Begin must be called before ending."); if (!began) throw new InvalidOperationException("Begin must be called before ending.");
Flush(); Flush();
} }
@ -75,9 +77,16 @@ namespace RecrownedAthenaeum.Render
public void AddVertex(Vector2 vertex, Color color) public void AddVertex(Vector2 vertex, Color color)
{ {
if (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex."); 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))
{
if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip)
{ {
Flush(); Flush();
} else
{
throw new InvalidOperationException("Buffer size isn't large enough.");
}
} }
vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color); vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
@ -90,6 +99,7 @@ namespace RecrownedAthenaeum.Render
public void Flush() public void Flush()
{ {
if (!began) throw new InvalidOperationException("Begin needs to be called before flushing."); if (!began) throw new InvalidOperationException("Begin needs to be called before flushing.");
if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name);
if (bufferPosition == 0) return; if (bufferPosition == 0) return;
graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive); graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive);
@ -104,10 +114,14 @@ namespace RecrownedAthenaeum.Render
public void Dispose(bool disposing) public void Dispose(bool disposing)
{ {
if (disposing && !isDisposed) if (disposing && !disposed)
{ {
basicEffect.Dispose(); basicEffect.Dispose();
isDisposed = true; disposed = true;
}
else
{
throw new ObjectDisposedException(typeof(PrimitiveBatch).Name);
} }
} }
} }