refactoring; removed configuration setup; added consistent sprite batch; got rid of sprite batch settings; implemented newer setup;

This commit is contained in:
2019-03-23 19:04:43 -05:00
parent e3535f5662
commit b045033b25
28 changed files with 342 additions and 329 deletions

View File

@@ -12,7 +12,7 @@ namespace RecrownedAthenaeum.Render
{
VertexPositionColor[] vertices;
int bufferPosition;
BasicEffect basicEffect;
Effect effect;
PrimitiveType primitiveType;
private int verticesPerPrimitive;
@@ -24,28 +24,26 @@ namespace RecrownedAthenaeum.Render
GraphicsDevice graphicsDevice;
bool began;
bool disposed;
Camera3D camera;
BasicEffect basicEffect;
/// <summary>
/// Creates a batch used to draw primitives.
/// </summary>
/// <param name="camera">The current camera being used. Will use default set in <see cref="Configuration"/> if left null.</param>
/// <param name="graphicsDevice">The graphics device used to draw the primitives. Will be using <see cref="Configuration"/>'s graphics device from graphics device manager if null. Default is null.</param>
/// <param name="graphicsDevice">The graphics device used to draw the primitives.</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(Camera2D camera = null, GraphicsDevice graphicsDevice = null, int verticesPerBatch = 500)
public PrimitiveBatch(GraphicsDevice graphicsDevice, int verticesPerBatch = 500)
{
this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
this.camera = camera ?? (Configuration.Camera2D);
basicEffect = new BasicEffect(this.graphicsDevice);
basicEffect.VertexColorEnabled = true;
vertices = new VertexPositionColor[verticesPerBatch + 1];
this.graphicsDevice = graphicsDevice;
basicEffect = new BasicEffect(graphicsDevice);
}
/// <summary>
/// Starts the batch. Batch cannot be started twice.
/// </summary>
/// <param name="primitiveType">The type of primitive this batch would be drawing.</param>
public void Begin(PrimitiveType primitiveType)
/// <param name="effect">Effect to use to render the primitives. Default will use a <see cref="BasicEffect"/>.</param>
public void Begin(PrimitiveType primitiveType, Effect effect = null)
{
if (began) throw new InvalidOperationException("Begin is being called twice before being ended.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
@@ -57,9 +55,7 @@ namespace RecrownedAthenaeum.Render
case PrimitiveType.LineStrip: verticesPerPrimitive = 1; break;
case PrimitiveType.TriangleStrip: verticesPerPrimitive = 3; break;
}
basicEffect.World = camera.worldMatrix;
basicEffect.View = camera.ViewMatrix;
basicEffect.Projection = camera.projectionMatrix;
this.effect = effect ?? basicEffect;
began = true;
}
@@ -116,7 +112,7 @@ namespace RecrownedAthenaeum.Render
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (bufferPosition == 0) return;
if (primitiveCount == 0) primitiveCount = bufferPosition / verticesPerPrimitive;
foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
foreach (EffectPass effectPass in effect.CurrentTechnique.Passes)
{
effectPass.Apply();
graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, primitiveCount);