recrownedgtk/RecrownedAthenaeum/Graphics/Render/RectangleRenderer.cs

120 lines
4.0 KiB
C#
Raw Normal View History

using OpenTK.Graphics;
using OpenTK;
using RecrownedAthenaeum.Types;
2019-01-12 06:47:17 +00:00
using System;
namespace RecrownedAthenaeum.Graphics.Render
2019-01-12 06:47:17 +00:00
{
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="graphicsDevice">Graphics device to use.</param>
public RectangleRenderer(GraphicsDevice graphicsDevice)
2019-01-12 06:47:17 +00:00
{
primitiveBatch = new PrimitiveBatch(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>
public void Draw(int x, int y, int width, int height, Color4 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
}
/// <summary>
/// Draws the given rectangle.
/// </summary>
/// <param name="rectangle">Uses the x, y and dimensions to draw a rectangle.</param>
/// <param name="color">The color of the rectangle.</param>
public void Draw(Rectangle rectangle, Color4 color)
{
Draw(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color);
}
2019-01-12 06:47:17 +00:00
}
}