diff --git a/RecrownedAthenaeum/Render/RectangleRenderer.cs b/RecrownedAthenaeum/Render/RectangleRenderer.cs index 2e53330..a9173a8 100644 --- a/RecrownedAthenaeum/Render/RectangleRenderer.cs +++ b/RecrownedAthenaeum/Render/RectangleRenderer.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using RecrownedAthenaeum.Camera; using System; namespace RecrownedAthenaeum.Render @@ -7,47 +8,46 @@ namespace RecrownedAthenaeum.Render /// /// Renders rectangles using the . /// - public class RectangleRenderer + public class RectangleRenderer : IDisposable { + private bool disposed; /// /// The used. Needs to be disposed. /// - public readonly PrimitiveBatch primitiveBatch; - private bool began; - private bool filling; + private readonly PrimitiveBatch primitiveBatch; /// /// Creates a rectangle renderer with the given . /// - /// - public RectangleRenderer(PrimitiveBatch primitiveBatch) + /// Camera to use for . Default will use . + /// Graphics device to use. Default will use . + public RectangleRenderer(Camera2D camera = null, GraphicsDevice graphicsDevice = null) { - this.primitiveBatch = primitiveBatch; + primitiveBatch = new PrimitiveBatch(camera, graphicsDevice, 4); } /// - /// Begins the render batch. + /// Disposes the rectangle renderer. /// - /// Whether or not to fill the rectangle. - public void Begin(bool filled = false) + public void Dispose() { - filling = filled; - if (began) throw new InvalidOperationException("Cannot begin twice."); - primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip); - began = true; + Dispose(true); } /// - /// Ends the batch. + /// Overridable for dispose. /// - public void End() + /// True when its a player calling the dispose. + public virtual void Dispose(bool disposing) { - if (!began) throw new InvalidOperationException("Cannot end before beginning."); - primitiveBatch.End(); - began = false; + if (disposed) throw new ObjectDisposedException(GetType().Name); + if (disposing) + { + primitiveBatch.Dispose(); + } + disposed = true; } - /// /// Draws a basic rectangle given bottom left and top right. /// @@ -57,9 +57,11 @@ namespace RecrownedAthenaeum.Render /// Height of rectangle. /// Color of all vertices of this rectangle. /// Rotation of rectangle. Default is 0 radians. - public void DrawRectangle(int x, int y, int width, int height, Color color, double rotation = 0) + /// 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) { - if (!began) throw new InvalidOperationException("Renderer must be started by calling Begin."); + primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip); + Vector2[] corners = new Vector2[4]; corners[0] = new Vector2(x, y); @@ -67,9 +69,9 @@ namespace RecrownedAthenaeum.Render 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); + corners[3] = new Vector2(x + (float)Math.Cos(rotation + (Math.PI / 4f)) * height, y + (float)Math.Sin(rotation) * height); - if (filling) + if (filled) { primitiveBatch.AddVertex(corners[1], color); primitiveBatch.AddVertex(corners[2], color); @@ -83,6 +85,8 @@ namespace RecrownedAthenaeum.Render primitiveBatch.AddVertex(corners[2], color); primitiveBatch.AddVertex(corners[3], color); } + + primitiveBatch.End(); } } }