using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using RecrownedAthenaeum.Camera; using System; namespace RecrownedAthenaeum.Render { /// /// Renders rectangles using the . /// public class RectangleRenderer : IDisposable { private bool disposed; /// /// The used. Needs to be disposed. /// private readonly PrimitiveBatch primitiveBatch; /// /// Creates a rectangle renderer with the given . /// /// Camera to use for . Default will use . /// Graphics device to use. Default will use . public RectangleRenderer(Camera2D camera = null, GraphicsDevice graphicsDevice = null) { primitiveBatch = new PrimitiveBatch(camera, graphicsDevice, 4); } /// /// Disposes the rectangle renderer. /// public void Dispose() { Dispose(true); } /// /// Overridable for dispose. /// /// True when its a player calling the dispose. public virtual void Dispose(bool disposing) { if (disposed) throw new ObjectDisposedException(GetType().Name); if (disposing) { primitiveBatch.Dispose(); } disposed = true; } /// /// Draws a basic rectangle given bottom left and top right. /// /// X coordinate of bottom left. /// Y coordinate of bottom left. /// Width of rectangle. /// Height of rectangle. /// Color of all vertices of this rectangle. /// Rotation of rectangle. Default is 0 radians. /// If this rectangle should be filled. public void Draw(int x, int y, int width, int height, Color color, double rotation = 0, bool filled = false) { primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip); 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)); if (filled) { 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); } primitiveBatch.End(); } } }