Removed begin and end because thats not how it works... Still working on rectangle drawing for rotating rectangle.

This commit is contained in:
Harrison Deng 2019-02-11 23:27:36 -06:00
parent cec8ff1f17
commit 4fb9e64f06

View File

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