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
{
List<VertexPositionColor> 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;
/// <summary>
/// Creates a batch used to draw primitives.
/// </summary>
/// <param name="graphicsDevice">The current graphics device 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)
{
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
/// </summary>
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);
}
}
}