recrownedathenaeum/RecrownedAthenaeum/Render/RectangleRenderer.cs

138 lines
5.2 KiB
C#
Raw Normal View History

2019-01-12 06:47:17 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Camera;
using System;
namespace RecrownedAthenaeum.Render
{
2019-01-14 06:34:35 +00:00
/// <summary>
/// Renders rectangles using the <see cref="PrimitiveBatch"/>.
/// </summary>
2019-01-13 06:12:55 +00:00
public class RectangleRenderer : IDisposable
2019-01-12 06:47:17 +00:00
{
2019-01-14 06:34:35 +00:00
/// <summary>
/// The <see cref="PrimitiveBatch"/> used.
/// </summary>
2019-01-12 06:47:17 +00:00
public readonly PrimitiveBatch primitiveBatch;
private bool disposed;
private bool began;
private bool filling;
2019-01-14 06:34:35 +00:00
/// <summary>
/// Creates a rectangle renderer with the given <see cref="PrimitiveBatch"/>.
/// </summary>
/// <param name="primitiveBatch"></param>
2019-01-13 06:12:55 +00:00
public RectangleRenderer(PrimitiveBatch primitiveBatch)
2019-01-12 06:47:17 +00:00
{
this.primitiveBatch = primitiveBatch;
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Creates a rectangle renderer.
/// </summary>
/// <param name="graphicsDevice">The graphics device used to create the <see cref="PrimitiveBatch"/></param>
/// <param name="camera">The camera containing the matrix to be used for the transformations.</param>
2019-01-13 06:12:55 +00:00
public RectangleRenderer(GraphicsDevice graphicsDevice, Camera2D camera) : this(new PrimitiveBatch(graphicsDevice, camera, 4))
2019-01-12 06:47:17 +00:00
{
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Begins the render batch.
/// </summary>
/// <param name="filled">Whether or not to fill the rectangle.</param>
2019-01-12 06:47:17 +00:00
public void Begin(bool filled)
{
filling = filled;
if (disposed) throw new ObjectDisposedException(GetType().Name);
2019-01-12 06:47:17 +00:00
if (began) throw new InvalidOperationException("Cannot begin twice.");
primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip);
began = true;
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Ends the batch.
/// </summary>
2019-01-12 06:47:17 +00:00
public void End()
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
2019-01-12 06:47:17 +00:00
if (!began) throw new InvalidOperationException("Cannot end before beginning.");
primitiveBatch.End();
began = false;
}
/// <summary>
/// Draws a basic rectangle given bottom left and top right.
/// </summary>
/// <param name="x">X coordinate of bottom left.</param>
/// <param name="y">Y coordinate of bottom left.</param>
/// <param name="width">Width of rectangle.</param>
/// <param name="height">Height of rectangle.</param>
/// <param name="color">Color of all vertices of this rectangle.</param>
/// <param name="rotation">Rotation of rectangle. Default is 0 radians.</param>
2019-01-13 06:12:55 +00:00
public void DrawRectangle(int x, int y, int width, int height, Color color, double rotation = 0)
2019-01-12 06:47:17 +00:00
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
2019-01-12 06:47:17 +00:00
if (!began) throw new InvalidOperationException("Renderer must be started by calling Begin.");
Vector2[] corners = new Vector2[4];
corners[0] = new Vector2(x, y);
corners[1] = new Vector2(x + (float)Math.Cos(rotation) * width, y + (float)Math.Sin(rotation) * width);
double topRightAngleFromOrig = Math.Atan(height / (double)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(rotation + Math.PI / 4f) * height, y + (float)Math.Sin(rotation) * height);
if (filling)
{
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);
}
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Attempts to dispose of this object.
/// </summary>
2019-01-12 06:47:17 +00:00
public void Dispose()
{
2019-01-14 06:34:35 +00:00
if (disposed) throw new ObjectDisposedException(typeof(PrimitiveBatch).Name);
2019-01-12 06:47:17 +00:00
Dispose(true);
GC.SuppressFinalize(this);
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// An overridable of disposable.
/// </summary>
/// <param name="disposing">Only true when called by user code dispose. Destructor calling this results in false.</param>
public virtual void Dispose(bool disposing)
2019-01-12 06:47:17 +00:00
{
2019-01-14 06:34:35 +00:00
disposed = true;
2019-01-12 06:47:17 +00:00
if (!disposed && disposing)
{
2019-01-14 06:34:35 +00:00
disposed = true;
2019-01-12 06:47:17 +00:00
if (primitiveBatch != null)
{
primitiveBatch.Dispose();
}
}
2019-01-14 06:34:35 +00:00
}
/// <summary>
/// Destructor.
/// </summary>
~RectangleRenderer ()
{
Dispose(false);
2019-01-12 06:47:17 +00:00
}
}
}