recrownedgtk/RecrownedAthenaeum/Render/RectangleRenderer.cs

111 lines
3.9 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;
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>
public class RectangleRenderer : IDisposable
2019-01-12 06:47:17 +00:00
{
2019-02-22 07:59:51 +00:00
private bool filled;
private bool disposed;
2019-01-14 06:34:35 +00:00
/// <summary>
/// The <see cref="PrimitiveBatch"/> used. Needs to be disposed.
2019-01-14 06:34:35 +00:00
/// </summary>
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>
/// <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>
/// Disposes the rectangle renderer.
2019-01-14 06:34:35 +00:00
/// </summary>
public void Dispose()
2019-01-12 06:47:17 +00:00
{
Dispose(true);
2019-01-12 06:47:17 +00:00
}
2019-01-14 06:34:35 +00:00
/// <summary>
/// Overridable for dispose.
2019-01-14 06:34:35 +00:00
/// </summary>
/// <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
{
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-25 04:44:02 +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++)
{
corners[i] = Vector2.Transform(corners[i], rotMat);
2019-02-22 07:59:51 +00:00
}
2019-01-12 06:47:17 +00:00
}
2019-02-25 04:44:02 +00:00
for (int i = 0; i < corners.Length; i++)
{
corners[i].X += x;
corners[i].Y += y;
}
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-02-25 04:44:02 +00:00
primitiveBatch.Flush();
2019-01-12 06:47:17 +00:00
}
}
}