using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Camera;
using System;
using System.Collections.Generic;
namespace RecrownedAthenaeum.Render
{
///
/// A batch used to draw primitive shapes by batching together vertices.
///
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; } }
int bufferPosition;
BasicEffect basicEffect;
PrimitiveType primitiveType;
int verticesPerPrimitive;
GraphicsDevice graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice;
bool began;
bool disposed;
///
/// Creates a batch used to draw primitives.
///
/// The current camera being used.
/// The graphics device used to draw the primitives. Will be using 's graphics device from graphics device manager if null. Default is null.
/// 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(Camera2D camera, GraphicsDevice graphicsDevice = null, int verticesPerBatch = 500)
{
if (graphicsDevice != null) this.graphicsDevice = graphicsDevice;
if (this.graphicsDevice == null) throw new ArgumentNullException("Graphics device can't be null in setup and argument. One must not be null for use.");
basicEffect = new BasicEffect(graphicsDevice);
basicEffect.VertexColorEnabled = true;
basicEffect.View = camera.Matrix;
vertices = new List(verticesPerBatch);
}
///
/// Starts the batch. Batch cannot be started twice.
///
/// The type of primitive this batch would be drawing.
public void Begin(PrimitiveType primitiveType)
{
if (began) throw new InvalidOperationException("Begin is being called twice before being ended.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
this.primitiveType = primitiveType;
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();
began = true;
}
///
/// Ends the batch. Begin needs to be called before end.
///
public void End()
{
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (!began) throw new InvalidOperationException("Begin must be called before ending.");
Flush();
}
///
/// Adds a vertex position for the primitive being drawn. The batch needs to have beens started before this.
///
/// The vector that represents the vertex.
/// The color of that vertex.
public void AddVertex(Vector2 vertex, Color color)
{
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)
{
Flush();
}
else
{
throw new InvalidOperationException("Buffer size isn't large enough.");
}
}
vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
bufferPosition++;
}
///
/// 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 (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (bufferPosition == 0) return;
graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive);
bufferPosition = 0;
}
///
/// Disposes this.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Overridable dispose.
///
/// True for when user called for this to be disposed. False otherwise.
public virtual void Dispose(bool disposing)
{
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
disposed = true;
if (disposing && !disposed)
{
basicEffect.Dispose();
}
}
///
/// Destructor.
///
~PrimitiveBatch()
{
Dispose(false);
}
}
}