basic primitive batch progress.

This commit is contained in:
Harrison Deng 2019-01-11 22:09:42 -06:00
parent 74a9ff74b2
commit ec62b81a12
5 changed files with 123 additions and 6 deletions

View File

@ -12,7 +12,7 @@ namespace RecrownedAthenaeum.Camera
{ {
public float Zoom; public float Zoom;
public Vector2 Position; public Vector2 Position;
public Matrix TransformMatrix { get; private set; } public Matrix Matrix { get; private set; }
public GraphicsDevice graphicsDevice; public GraphicsDevice graphicsDevice;
public Camera2D(GraphicsDevice graphicsDevice) public Camera2D(GraphicsDevice graphicsDevice)
@ -26,7 +26,7 @@ namespace RecrownedAthenaeum.Camera
public void Update() public void Update()
{ {
Rectangle bounds = graphicsDevice.Viewport.Bounds; Rectangle bounds = graphicsDevice.Viewport.Bounds;
TransformMatrix = Matrix =
Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) * Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
Matrix.CreateScale(Zoom) * Matrix.CreateScale(Zoom) *
Matrix.CreateTranslation(new Vector3(bounds.Width * 0.5f, bounds.Height * 0.5f, 0f)); Matrix.CreateTranslation(new Vector3(bounds.Width * 0.5f, bounds.Height * 0.5f, 0f));

View File

@ -68,6 +68,7 @@
<Compile Include="Pipeline\NinePatchDataReader.cs" /> <Compile Include="Pipeline\NinePatchDataReader.cs" />
<Compile Include="Pipeline\TextureAtlasDataReader.cs" /> <Compile Include="Pipeline\TextureAtlasDataReader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Render\PrimitiveBatch.cs" />
<Compile Include="ScreenSystem\ITransition.cs" /> <Compile Include="ScreenSystem\ITransition.cs" />
<Compile Include="ScreenSystem\LoadingScreen.cs" /> <Compile Include="ScreenSystem\LoadingScreen.cs" />
<Compile Include="ScreenSystem\Screen.cs" /> <Compile Include="ScreenSystem\Screen.cs" />

View File

@ -0,0 +1,116 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Camera;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RecrownedAthenaeum.Render
{
public class PrimitiveBatch : IDisposable
{
VertexPositionColor[] vertices;
int MaxVertices { get { return vertices.Length; } }
int bufferPosition;
BasicEffect basicEffect;
PrimitiveType primitiveType;
int verticesPerPrimitive;
GraphicsDevice graphicsDevice;
bool began;
bool isDisposed;
/// <summary>
/// Creates a batch used to draw primitives.
/// </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)
{
this.graphicsDevice = graphicsDevice ?? throw new ArgumentNullException("Graphics device can't be null.");
basicEffect = new BasicEffect(graphicsDevice);
basicEffect.VertexColorEnabled = true;
basicEffect.View = camera.Matrix;
vertices = new VertexPositionColor[verticesPerBatch];
}
/// <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)
{
if (began) throw new InvalidOperationException("Begin is being called twice before being ended.");
this.primitiveType = primitiveType;
this.verticesPerPrimitive = 0;
switch (primitiveType)
{
case PrimitiveType.LineList: verticesPerPrimitive = 2; break;
case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break;
}
basicEffect.CurrentTechnique.Passes[0].Apply();
began = true;
}
/// <summary>
/// Ends the batch. Begin needs to be called before end.
/// </summary>
public void End()
{
if (!began) throw new InvalidOperationException("Begin must be called before ending.");
Flush();
}
/// <summary>
/// Adds a vertex position for the primitive being drawn. The batch needs to have beens started before this.
/// </summary>
/// <param name="vertex">The vector that represents the vertex.</param>
/// <param name="color">The color of that vertex.</param>
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))
{
Flush();
}
vertices[bufferPosition].Position = (new Vector3(vertex, 0));
vertices[bufferPosition].Color = color;
bufferPosition++;
}
/// <summary>
/// Flushes the batch. Automatically called if 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);
bufferPosition = 0;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (disposing && !isDisposed)
{
basicEffect.Dispose();
isDisposed = true;
}
}
}
}

View File

@ -104,13 +104,13 @@ namespace RecrownedAthenaeum.ScreenSystem
{ {
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget); graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor); graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix); spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.Matrix);
previousScreen.Draw(spriteBatch); previousScreen.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
graphics.GraphicsDevice.SetRenderTarget(null); graphics.GraphicsDevice.SetRenderTarget(null);
Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget); Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget);
} }
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix); spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.Matrix);
Screen.Draw(spriteBatch); Screen.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
} }

View File

@ -33,7 +33,7 @@ namespace RecrownedAthenaeum.UI.Modular
if (scissorBounds != null) if (scissorBounds != null)
{ {
batch.End(); batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, Camera?.TransformMatrix); batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, Camera?.Matrix);
scissorBounds.Width = bounds.Width; scissorBounds.Width = bounds.Width;
scissorBounds.Height = bounds.Height; scissorBounds.Height = bounds.Height;
scissorBounds.X = bounds.X; scissorBounds.X = bounds.X;
@ -57,7 +57,7 @@ namespace RecrownedAthenaeum.UI.Modular
{ {
batch.GraphicsDevice.ScissorRectangle = scissorBounds; batch.GraphicsDevice.ScissorRectangle = scissorBounds;
batch.End(); batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Camera?.TransformMatrix); batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Camera?.Matrix);
} }
} }