Rectangle renderers work.

This commit is contained in:
2019-02-22 01:59:51 -06:00
parent c8e8bbf2be
commit 8d5435ad6c
6 changed files with 56 additions and 76 deletions

View File

@@ -10,6 +10,7 @@ namespace RecrownedAthenaeum.Render
/// </summary>
public class RectangleRenderer : IDisposable
{
private bool filled;
private bool disposed;
/// <summary>
/// The <see cref="PrimitiveBatch"/> used. Needs to be disposed.
@@ -23,7 +24,7 @@ namespace RecrownedAthenaeum.Render
/// <param name="graphicsDevice">Graphics device to use. Default will use <see cref="Configuration"/>.</param>
public RectangleRenderer(Camera2D camera = null, GraphicsDevice graphicsDevice = null)
{
primitiveBatch = new PrimitiveBatch(camera, graphicsDevice, 4);
primitiveBatch = new PrimitiveBatch(camera, graphicsDevice);
}
/// <summary>
@@ -48,6 +49,23 @@ namespace RecrownedAthenaeum.Render
disposed = true;
}
/// <summary>
/// Begins a batch for rectangles.
/// </summary>
/// <param name="filled">Whether or not this batch should be filled rectangles.</param>
public void Begin(bool filled = false)
{
this.filled = filled;
primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip);
}
/// <summary>
/// Ends the batch.
/// </summary>
public void End()
{
primitiveBatch.End();
}
/// <summary>
/// Draws a basic rectangle given bottom left and top right.
/// </summary>
@@ -58,35 +76,29 @@ namespace RecrownedAthenaeum.Render
/// <param name="color">Color of all vertices of this rectangle.</param>
/// <param name="rotation">Rotation of rectangle. Default is 0 radians.</param>
/// <param name="filled">If this rectangle should be filled.</param>
public void Draw(int x, int y, int width, int height, Color color, double rotation = 0, bool filled = false)
public void Draw(int x, int y, int width, int height, Color color, float rotation = 0)
{
primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip);
primitiveBatch.primitiveCount = filled ? 3 : 4;
Vector2[] corners = new Vector2[4];
double topRightAngleFromOrig = Math.Atan((double)height / width);
corners[0] = new Vector2(x, y);
corners[1] = new Vector2(x + (float)Math.Cos(rotation) * width, y + (float)Math.Sin(rotation) * width);
float origDiagonalHypotenuse = (float)Math.Sqrt((width * width) + (height * height));
corners[2] = new Vector2(x + (float)Math.Cos(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse, y + (float)Math.Sin(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse);
corners[3] = new Vector2(x - (float)(Math.Cos((Math.PI / 2f) - rotation) * height), y + (float)(Math.Sin((Math.PI / 2f) - rotation) * height));
corners[1] = new Vector2(x + width, y);
corners[2] = new Vector2(x + width, y + height);
corners[3] = new Vector2(x, y + height);
if (filled)
if (rotation != 0)
{
primitiveBatch.AddVertex(corners[1], color);
primitiveBatch.AddVertex(corners[2], color);
primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[3], color);
}
else
{
primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[1], color);
primitiveBatch.AddVertex(corners[2], color);
primitiveBatch.AddVertex(corners[3], color);
Matrix rotMat = Matrix.CreateRotationZ(rotation);
for (int i = 0; i < corners.Length; i++)
{
Vector2.Transform(corners[i], rotMat);
}
}
primitiveBatch.End();
primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[1], color);
primitiveBatch.AddVertex(corners[2], color);
primitiveBatch.AddVertex(corners[3], color);
primitiveBatch.AddVertex(corners[0], color);
}
}
}