Primitive batch now works with strips.
This commit is contained in:
		@@ -11,8 +11,8 @@ namespace RecrownedAthenaeum.Render
 | 
			
		||||
{
 | 
			
		||||
    public class PrimitiveBatch : IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        VertexPositionColor[] vertices;
 | 
			
		||||
        int MaxVertices { get { return vertices.Length; } }
 | 
			
		||||
        List<VertexPositionColor> vertices;
 | 
			
		||||
        int maxVertices;
 | 
			
		||||
        int bufferPosition;
 | 
			
		||||
        BasicEffect basicEffect;
 | 
			
		||||
        PrimitiveType primitiveType;
 | 
			
		||||
@@ -26,16 +26,16 @@ namespace RecrownedAthenaeum.Render
 | 
			
		||||
        /// </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.</param>
 | 
			
		||||
        public PrimitiveBatch(GraphicsDevice graphicsDevice, Camera2D camera, int verticesPerBatch = 450)
 | 
			
		||||
        /// <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 = 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;
 | 
			
		||||
 | 
			
		||||
            vertices = new VertexPositionColor[verticesPerBatch];
 | 
			
		||||
            vertices = new List<VertexPositionColor>(verticesPerBatch);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -46,11 +46,12 @@ namespace RecrownedAthenaeum.Render
 | 
			
		||||
        {
 | 
			
		||||
            if (began) throw new InvalidOperationException("Begin is being called twice before being ended.");
 | 
			
		||||
            this.primitiveType = primitiveType;
 | 
			
		||||
            this.verticesPerPrimitive = 0;
 | 
			
		||||
            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();
 | 
			
		||||
@@ -74,27 +75,24 @@ namespace RecrownedAthenaeum.Render
 | 
			
		||||
        public void AddVertex(Vector2 vertex, Color color)
 | 
			
		||||
        {
 | 
			
		||||
            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();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            vertices[bufferPosition].Position = (new Vector3(vertex, 0));
 | 
			
		||||
            vertices[bufferPosition].Color = color;
 | 
			
		||||
            vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
 | 
			
		||||
 | 
			
		||||
            bufferPosition++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <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>
 | 
			
		||||
        public void Flush()
 | 
			
		||||
        {
 | 
			
		||||
            if (!began) throw new InvalidOperationException("Begin needs to be called before flushing.");
 | 
			
		||||
            if (bufferPosition == 0) return;
 | 
			
		||||
 | 
			
		||||
            graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, bufferPosition / verticesPerPrimitive);
 | 
			
		||||
 | 
			
		||||
            graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive);
 | 
			
		||||
            bufferPosition = 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user