Primitive batch now works. Probably.

This commit is contained in:
Harrison Deng 2019-02-10 23:32:00 -06:00
parent 56eb4e9e7f
commit 2bd6d68490

View File

@ -19,10 +19,15 @@ namespace RecrownedAthenaeum.Render
int bufferPosition; int bufferPosition;
BasicEffect basicEffect; BasicEffect basicEffect;
PrimitiveType primitiveType; PrimitiveType primitiveType;
int verticesPerPrimitive;
/// <summary>
/// The vertices per primitive. Can be ignored if using any of the list primitive types. Otherwise, needs to be manually set.
/// </summary>
public int verticesPerPrimitive;
GraphicsDevice graphicsDevice; GraphicsDevice graphicsDevice;
bool began; bool began;
bool disposed; bool disposed;
Camera3D camera;
/// <summary> /// <summary>
/// Creates a batch used to draw primitives. /// Creates a batch used to draw primitives.
@ -33,12 +38,9 @@ namespace RecrownedAthenaeum.Render
public PrimitiveBatch(Camera2D camera = null, GraphicsDevice graphicsDevice = null, int verticesPerBatch = 500) public PrimitiveBatch(Camera2D camera = null, GraphicsDevice graphicsDevice = null, int verticesPerBatch = 500)
{ {
this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice); this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
camera = camera ?? (Configuration.Camera2D); this.camera = camera ?? (Configuration.Camera2D);
basicEffect = new BasicEffect(this.graphicsDevice); basicEffect = new BasicEffect(this.graphicsDevice);
basicEffect.VertexColorEnabled = true; basicEffect.VertexColorEnabled = true;
basicEffect.Projection = camera.projectionMatrix;
basicEffect.View = camera.ViewMatrix;
basicEffect.World = Matrix.Identity;
vertices = new List<VertexPositionColor>(verticesPerBatch); vertices = new List<VertexPositionColor>(verticesPerBatch);
} }
@ -56,9 +58,12 @@ namespace RecrownedAthenaeum.Render
{ {
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; default: verticesPerPrimitive = -1; break;
} }
basicEffect.World = camera.worldMatrix;
basicEffect.View = camera.ViewMatrix;
basicEffect.Projection = camera.projectionMatrix;
basicEffect.CurrentTechnique.Passes[0].Apply(); basicEffect.CurrentTechnique.Passes[0].Apply();
began = true; began = true;
} }
@ -106,7 +111,7 @@ namespace RecrownedAthenaeum.Render
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(this.GetType().Name); if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (bufferPosition == 0) return; if (bufferPosition == 0) return;
if (verticesPerPrimitive == -1) throw new InvalidOperationException("Vertices per primitive variable not set and using primitive type other than list.");
graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive); graphicsDevice.DrawUserPrimitives(primitiveType, vertices.ToArray(), 0, bufferPosition / verticesPerPrimitive);
vertices.Clear(); vertices.Clear();
bufferPosition = 0; bufferPosition = 0;