Primitive batch now works with strips.
This commit is contained in:
parent
ec62b81a12
commit
c9991f08c5
@ -11,8 +11,8 @@ namespace RecrownedAthenaeum.Render
|
|||||||
{
|
{
|
||||||
public class PrimitiveBatch : IDisposable
|
public class PrimitiveBatch : IDisposable
|
||||||
{
|
{
|
||||||
VertexPositionColor[] vertices;
|
List<VertexPositionColor> vertices;
|
||||||
int MaxVertices { get { return vertices.Length; } }
|
int maxVertices;
|
||||||
int bufferPosition;
|
int bufferPosition;
|
||||||
BasicEffect basicEffect;
|
BasicEffect basicEffect;
|
||||||
PrimitiveType primitiveType;
|
PrimitiveType primitiveType;
|
||||||
@ -26,16 +26,16 @@ namespace RecrownedAthenaeum.Render
|
|||||||
/// </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.</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>
|
||||||
public PrimitiveBatch(GraphicsDevice graphicsDevice, Camera2D camera, int verticesPerBatch = 450)
|
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;
|
||||||
|
|
||||||
vertices = new VertexPositionColor[verticesPerBatch];
|
vertices = new List<VertexPositionColor>(verticesPerBatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -46,11 +46,12 @@ namespace RecrownedAthenaeum.Render
|
|||||||
{
|
{
|
||||||
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.");
|
||||||
this.primitiveType = primitiveType;
|
this.primitiveType = primitiveType;
|
||||||
this.verticesPerPrimitive = 0;
|
verticesPerPrimitive = 0;
|
||||||
switch (primitiveType)
|
switch (primitiveType)
|
||||||
{
|
{
|
||||||
case PrimitiveType.LineList: verticesPerPrimitive = 2; break;
|
case PrimitiveType.LineList: verticesPerPrimitive = 2; break;
|
||||||
case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break;
|
case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break;
|
||||||
|
default: verticesPerPrimitive = 1; break;
|
||||||
}
|
}
|
||||||
|
|
||||||
basicEffect.CurrentTechnique.Passes[0].Apply();
|
basicEffect.CurrentTechnique.Passes[0].Apply();
|
||||||
@ -74,27 +75,24 @@ 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 (bufferPosition + verticesPerPrimitive >= MaxVertices && (bufferPosition % MaxVertices == 0))
|
if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip && bufferPosition + verticesPerPrimitive >= maxVertices && (bufferPosition % maxVertices == 0))
|
||||||
{
|
{
|
||||||
Flush();
|
Flush();
|
||||||
}
|
}
|
||||||
|
vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
|
||||||
vertices[bufferPosition].Position = (new Vector3(vertex, 0));
|
|
||||||
vertices[bufferPosition].Color = color;
|
|
||||||
|
|
||||||
bufferPosition++;
|
bufferPosition++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Flushes the batch. Automatically called if required.
|
/// Flushes the batch. Automatically called if required if using <see cref="PrimitiveType.LineList"/> or <see cref="PrimitiveType.TriangleList"/>. Otherwise, manual flushing is required.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
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 (bufferPosition == 0) return;
|
if (bufferPosition == 0) return;
|
||||||
|
|
||||||
graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, bufferPosition / verticesPerPrimitive);
|
graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive);
|
||||||
|
|
||||||
bufferPosition = 0;
|
bufferPosition = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user