2019-01-12 06:47:17 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2019-02-12 05:27:36 +00:00
|
|
|
|
using RecrownedAthenaeum.Camera;
|
2019-01-12 06:47:17 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace RecrownedAthenaeum.Render
|
|
|
|
|
{
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Renders rectangles using the <see cref="PrimitiveBatch"/>.
|
|
|
|
|
/// </summary>
|
2019-02-12 05:27:36 +00:00
|
|
|
|
public class RectangleRenderer : IDisposable
|
2019-01-12 06:47:17 +00:00
|
|
|
|
{
|
2019-02-22 07:59:51 +00:00
|
|
|
|
private bool filled;
|
2019-02-12 05:27:36 +00:00
|
|
|
|
private bool disposed;
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
2019-01-22 01:56:51 +00:00
|
|
|
|
/// The <see cref="PrimitiveBatch"/> used. Needs to be disposed.
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// </summary>
|
2019-02-12 05:27:36 +00:00
|
|
|
|
private readonly PrimitiveBatch primitiveBatch;
|
2019-01-12 06:47:17 +00:00
|
|
|
|
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a rectangle renderer with the given <see cref="PrimitiveBatch"/>.
|
|
|
|
|
/// </summary>
|
2019-02-12 05:27:36 +00:00
|
|
|
|
/// <param name="camera">Camera to use for <see cref="PrimitiveBatch"/>. Default will use <see cref="Configuration"/>.</param>
|
|
|
|
|
/// <param name="graphicsDevice">Graphics device to use. Default will use <see cref="Configuration"/>.</param>
|
|
|
|
|
public RectangleRenderer(Camera2D camera = null, GraphicsDevice graphicsDevice = null)
|
2019-01-12 06:47:17 +00:00
|
|
|
|
{
|
2019-02-22 07:59:51 +00:00
|
|
|
|
primitiveBatch = new PrimitiveBatch(camera, graphicsDevice);
|
2019-01-12 06:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
2019-02-12 05:27:36 +00:00
|
|
|
|
/// Disposes the rectangle renderer.
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// </summary>
|
2019-02-12 05:27:36 +00:00
|
|
|
|
public void Dispose()
|
2019-01-12 06:47:17 +00:00
|
|
|
|
{
|
2019-02-12 05:27:36 +00:00
|
|
|
|
Dispose(true);
|
2019-01-12 06:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// <summary>
|
2019-02-12 05:27:36 +00:00
|
|
|
|
/// Overridable for dispose.
|
2019-01-14 06:34:35 +00:00
|
|
|
|
/// </summary>
|
2019-02-12 05:27:36 +00:00
|
|
|
|
/// <param name="disposing">True when its a player calling the dispose.</param>
|
|
|
|
|
public virtual void Dispose(bool disposing)
|
2019-01-12 06:47:17 +00:00
|
|
|
|
{
|
2019-02-12 05:27:36 +00:00
|
|
|
|
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
primitiveBatch.Dispose();
|
|
|
|
|
}
|
|
|
|
|
disposed = true;
|
2019-01-12 06:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 07:59:51 +00:00
|
|
|
|
/// <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();
|
|
|
|
|
}
|
2019-01-12 06:47:17 +00:00
|
|
|
|
/// <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-02-22 07:59:51 +00:00
|
|
|
|
public void Draw(int x, int y, int width, int height, Color color, float rotation = 0)
|
2019-01-12 06:47:17 +00:00
|
|
|
|
{
|
2019-02-22 07:59:51 +00:00
|
|
|
|
primitiveBatch.primitiveCount = filled ? 3 : 4;
|
2019-01-12 06:47:17 +00:00
|
|
|
|
Vector2[] corners = new Vector2[4];
|
2019-02-22 08:17:50 +00:00
|
|
|
|
corners[1] = new Vector2 (width, 0);
|
|
|
|
|
corners[2] = new Vector2(width, height);
|
|
|
|
|
corners[3] = new Vector2(0, height);
|
2019-01-12 06:47:17 +00:00
|
|
|
|
|
2019-02-22 07:59:51 +00:00
|
|
|
|
if (rotation != 0)
|
2019-01-12 06:47:17 +00:00
|
|
|
|
{
|
2019-02-22 07:59:51 +00:00
|
|
|
|
Matrix rotMat = Matrix.CreateRotationZ(rotation);
|
|
|
|
|
for (int i = 0; i < corners.Length; i++)
|
|
|
|
|
{
|
2019-02-22 08:17:50 +00:00
|
|
|
|
corners[i] = Vector2.Transform(corners[i], rotMat);
|
|
|
|
|
corners[i].X += x;
|
|
|
|
|
corners[i].Y += y;
|
2019-02-22 07:59:51 +00:00
|
|
|
|
}
|
2019-01-12 06:47:17 +00:00
|
|
|
|
}
|
2019-02-12 05:27:36 +00:00
|
|
|
|
|
2019-02-22 07:59:51 +00:00
|
|
|
|
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);
|
2019-01-12 06:47:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|