diff --git a/Source/Demos/Features/Demos/ShapesDemo.cs b/Source/Demos/Features/Demos/ShapesDemo.cs
new file mode 100644
index 00000000..70a976dc
--- /dev/null
+++ b/Source/Demos/Features/Demos/ShapesDemo.cs
@@ -0,0 +1,53 @@
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using MonoGame.Extended;
+using MonoGame.Extended.Shapes;
+
+namespace Features.Demos
+{
+ public class ShapesDemo : DemoBase
+ {
+ private SpriteBatch _spriteBatch;
+
+ public ShapesDemo(GameMain game)
+ : base(game)
+ {
+ }
+
+ public override string Name => "Shapes";
+
+ private readonly Polygon _polygon = new Polygon(new[]
+ {
+ new Vector2(0, 0),
+ new Vector2(40, 0),
+ new Vector2(40, 40),
+ new Vector2(60, 40),
+ new Vector2(60, 60),
+ new Vector2(0, 60),
+ });
+
+ protected override void LoadContent()
+ {
+ _spriteBatch = new SpriteBatch(GraphicsDevice);
+ }
+
+ protected override void Draw(GameTime gameTime)
+ {
+ _spriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.AlphaBlend);
+
+ _spriteBatch.FillRectangle(10, 10, 50, 50, Color.DarkRed);
+ _spriteBatch.DrawRectangle(10, 10, 50, 50, Color.Red, 2);
+
+ _spriteBatch.DrawCircle(100, 100, 32, 32, Color.Green, 4);
+ _spriteBatch.DrawEllipse(new Vector2(200, 200), new Vector2(50, 25), 32, Color.Orange, 8);
+
+ _spriteBatch.DrawLine(300, 300, 400, 100, Color.White, 6);
+
+ _spriteBatch.DrawPoint(500, 300, Color.Brown, 64);
+
+ _spriteBatch.DrawPolygon(new Vector2(600, 200), _polygon, Color.Aqua);
+
+ _spriteBatch.End();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Source/Demos/Features/GameMain.cs b/Source/Demos/Features/GameMain.cs
index 8f5b1bf6..a527bdef 100644
--- a/Source/Demos/Features/GameMain.cs
+++ b/Source/Demos/Features/GameMain.cs
@@ -45,6 +45,7 @@ namespace Features
//new GuiLayoutDemo(this),
//new GuiDemo(this),
//new ScreensDemo(this),
+ new ShapesDemo(this),
new ViewportAdaptersDemo(this),
new CollisionDemo(this),
new TiledMapsDemo(this),
diff --git a/Source/MonoGame.Extended/Math/ShapeExtensions.cs b/Source/MonoGame.Extended/Math/ShapeExtensions.cs
index 30a0b59b..5424ada8 100644
--- a/Source/MonoGame.Extended/Math/ShapeExtensions.cs
+++ b/Source/MonoGame.Extended/Math/ShapeExtensions.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Net.NetworkInformation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Shapes;
@@ -19,7 +18,12 @@ namespace MonoGame.Extended
if (_whitePixelTexture == null)
{
_whitePixelTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
- _whitePixelTexture.SetData(new[] {Color.White});
+ _whitePixelTexture.SetData(new[] { Color.White });
+ spriteBatch.Disposing += (sender, args) =>
+ {
+ _whitePixelTexture?.Dispose();
+ _whitePixelTexture = null;
+ };
}
return _whitePixelTexture;
@@ -34,10 +38,10 @@ namespace MonoGame.Extended
/// The polygon to draw
/// The color to use
/// The thickness of the lines
- public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, Polygon polygon, Color color,
- float thickness = 1f)
+ /// /// The depth of the layer of this shape
+ public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 position, Polygon polygon, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawPolygon(spriteBatch, position, polygon.Vertices, color, thickness);
+ DrawPolygon(spriteBatch, position, polygon.Vertices, color, thickness, layerDepth);
}
///
@@ -49,61 +53,61 @@ namespace MonoGame.Extended
/// The points to connect with lines
/// The color to use
/// The thickness of the lines
- public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 offset, IReadOnlyList points, Color color,
- float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawPolygon(this SpriteBatch spriteBatch, Vector2 offset, IReadOnlyList points, Color color, float thickness = 1f, int layerDepth = 0)
{
if (points.Count == 0)
return;
if (points.Count == 1)
{
- DrawPoint(spriteBatch, points[0], color, (int) thickness);
+ DrawPoint(spriteBatch, points[0], color, (int)thickness);
return;
}
var texture = GetTexture(spriteBatch);
for (var i = 0; i < points.Count - 1; i++)
- DrawPolygonEdge(spriteBatch, texture, points[i] + offset, points[i + 1] + offset, color, thickness);
+ DrawPolygonEdge(spriteBatch, texture, points[i] + offset, points[i + 1] + offset, color, thickness, layerDepth);
- DrawPolygonEdge(spriteBatch, texture, points[points.Count - 1] + offset, points[0] + offset, color,
- thickness);
+ DrawPolygonEdge(spriteBatch, texture, points[points.Count - 1] + offset, points[0] + offset, color, thickness, layerDepth);
}
- private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2,
- Color color, float thickness)
+ private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2, Color color, float thickness, int layerDepth)
{
var length = Vector2.Distance(point1, point2);
- var angle = (float) Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
+ var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
var scale = new Vector2(length, thickness);
- spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, scale, SpriteEffects.None, 0);
+ spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
}
///
- /// Draws a filled rectangle
+ /// Draws a filled rectangle
///
/// The destination drawing surface
/// The rectangle to draw
/// The color to draw the rectangle in
- public static void FillRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color)
+ /// The depth of the layer of this shape
+ public static void FillRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, int layerDepth = 0)
{
- FillRectangle(spriteBatch, rectangle.Position, rectangle.Size, color);
+ FillRectangle(spriteBatch, rectangle.Position, rectangle.Size, color, layerDepth);
}
///
- /// Draws a filled rectangle
+ /// Draws a filled rectangle
///
/// The destination drawing surface
/// Where to draw
/// The size of the rectangle
/// The color to draw the rectangle in
- public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color)
+ /// The depth of the layer of this shape
+ public static void FillRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color, int layerDepth = 0)
{
- spriteBatch.Draw(GetTexture(spriteBatch), location, null, color, 0, Vector2.Zero, size, SpriteEffects.None, 0);
+ spriteBatch.Draw(GetTexture(spriteBatch), location, null, color, 0, Vector2.Zero, size, SpriteEffects.None, layerDepth);
}
///
- /// Draws a filled rectangle
+ /// Draws a filled rectangle
///
/// The destination drawing surface
/// The X coord of the left side
@@ -111,19 +115,21 @@ namespace MonoGame.Extended
/// Width
/// Height
/// The color to draw the rectangle in
- public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color)
+ /// The depth of the layer of this shape
+ public static void FillRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, int layerDepth = 0)
{
- FillRectangle(spriteBatch, new Vector2(x, y), new Size2(width, height), color);
+ FillRectangle(spriteBatch, new Vector2(x, y), new Size2(width, height), color, layerDepth);
}
///
- /// Draws a rectangle with the thickness provided
+ /// Draws a rectangle with the thickness provided
///
/// The destination drawing surface
/// The rectangle to draw
/// The color to draw the rectangle in
/// The thickness of the lines
- public static void DrawRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawRectangle(this SpriteBatch spriteBatch, RectangleF rectangle, Color color, float thickness = 1f, int layerDepth = 0)
{
var texture = GetTexture(spriteBatch);
var topLeft = new Vector2(rectangle.X, rectangle.Y);
@@ -132,10 +138,10 @@ namespace MonoGame.Extended
var horizontalScale = new Vector2(rectangle.Width, thickness);
var verticalScale = new Vector2(thickness, rectangle.Height);
- spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, 0);
- spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, 0);
- spriteBatch.Draw(texture, topRight, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, 0);
- spriteBatch.Draw(texture, bottomLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, 0);
+ spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth);
+ spriteBatch.Draw(texture, topLeft, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth);
+ spriteBatch.Draw(texture, topRight, null, color, 0f, Vector2.Zero, verticalScale, SpriteEffects.None, layerDepth);
+ spriteBatch.Draw(texture, bottomLeft, null, color, 0f, Vector2.Zero, horizontalScale, SpriteEffects.None, layerDepth);
}
///
@@ -146,23 +152,23 @@ namespace MonoGame.Extended
/// The size of the rectangle
/// The color to draw the rectangle in
/// The thickness of the line
- public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color,
- float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawRectangle(this SpriteBatch spriteBatch, Vector2 location, Size2 size, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawRectangle(spriteBatch, new RectangleF(location.X, location.Y, size.Width, size.Height), color, thickness);
+ DrawRectangle(spriteBatch, new RectangleF(location.X, location.Y, size.Width, size.Height), color, thickness, layerDepth);
}
///
/// Draws a rectangle outline.
///
- public static void DrawRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float thickness = 1f)
+ public static void DrawRectangle(this SpriteBatch spriteBatch, float x, float y, float width, float height, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawRectangle(spriteBatch, new RectangleF(x, y, width, height), color, thickness);
+ DrawRectangle(spriteBatch, new RectangleF(x, y, width, height), color, thickness, layerDepth);
}
///
- /// Draws a line from point1 to point2 with an offset
+ /// Draws a line from point1 to point2 with an offset
///
/// The destination drawing surface
/// The X coord of the first point
@@ -171,34 +177,34 @@ namespace MonoGame.Extended
/// The Y coord of the second point
/// The color to use
/// The thickness of the line
- public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color,
- float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawLine(this SpriteBatch spriteBatch, float x1, float y1, float x2, float y2, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness);
+ DrawLine(spriteBatch, new Vector2(x1, y1), new Vector2(x2, y2), color, thickness, layerDepth);
}
///
- /// Draws a line from point1 to point2 with an offset
+ /// Draws a line from point1 to point2 with an offset
///
/// The destination drawing surface
/// The first point
/// The second point
/// The color to use
/// The thickness of the line
- public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color,
- float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness = 1f, int layerDepth = 0)
{
// calculate the distance between the two vectors
var distance = Vector2.Distance(point1, point2);
// calculate the angle between the two vectors
- var angle = (float) Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
+ var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
- DrawLine(spriteBatch, point1, distance, angle, color, thickness);
+ DrawLine(spriteBatch, point1, distance, angle, color, thickness, layerDepth);
}
///
- /// Draws a line from point1 to point2 with an offset
+ /// Draws a line from point1 to point2 with an offset
///
/// The destination drawing surface
/// The starting point
@@ -206,48 +212,48 @@ namespace MonoGame.Extended
/// The angle of this line from the starting point
/// The color to use
/// The thickness of the line
- public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color,
- float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness = 1f, int layerDepth = 0)
{
var origin = new Vector2(0f, 0.5f);
var scale = new Vector2(length, thickness);
- spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, 0);
+ spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, layerDepth);
}
///
- /// Draws a point at the specified x, y position. The center of the point will be at the position.
+ /// Draws a point at the specified x, y position. The center of the point will be at the position.
///
- public static void DrawPoint(this SpriteBatch spriteBatch, float x, float y, Color color, float size = 1f)
+ public static void DrawPoint(this SpriteBatch spriteBatch, float x, float y, Color color, float size = 1f, int layerDepth = 0)
{
- DrawPoint(spriteBatch, new Vector2(x, y), color, size);
+ DrawPoint(spriteBatch, new Vector2(x, y), color, size, layerDepth);
}
///
- /// Draws a point at the specified position. The center of the point will be at the position.
+ /// Draws a point at the specified position. The center of the point will be at the position.
///
- public static void DrawPoint(this SpriteBatch spriteBatch, Vector2 position, Color color, float size = 1f)
+ public static void DrawPoint(this SpriteBatch spriteBatch, Vector2 position, Color color, float size = 1f, int layerDepth = 0)
{
- var scale = Vector2.One*size;
- var offset = new Vector2(0.5f) - new Vector2(size*0.5f);
- spriteBatch.Draw(GetTexture(spriteBatch), position + offset, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0);
+ var scale = Vector2.One * size;
+ var offset = new Vector2(0.5f) - new Vector2(size * 0.5f);
+ spriteBatch.Draw(GetTexture(spriteBatch), position + offset, null, color, 0f, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
}
///
- /// Draw a circle from a shape
+ /// Draw a circle from a shape
///
/// The destination drawing surface
/// The circle shape to draw
/// The number of sides to generate
/// The color of the circle
/// The thickness of the lines used
- public static void DrawCircle(this SpriteBatch spriteBatch, CircleF circle, int sides, Color color,
- float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawCircle(this SpriteBatch spriteBatch, CircleF circle, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawCircle(spriteBatch, circle.Center, circle.Radius, sides, color, thickness);
+ DrawCircle(spriteBatch, circle.Center, circle.Radius, sides, color, thickness, layerDepth);
}
///
- /// Draw a circle
+ /// Draw a circle
///
/// The destination drawing surface
/// The center of the circle
@@ -255,14 +261,14 @@ namespace MonoGame.Extended
/// The number of sides to generate
/// The color of the circle
/// The thickness of the lines used
- public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color,
- float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawPolygon(spriteBatch, center, CreateCircle(radius, sides), color, thickness);
+ DrawPolygon(spriteBatch, center, CreateCircle(radius, sides), color, thickness, layerDepth);
}
///
- /// Draw a circle
+ /// Draw a circle
///
/// The destination drawing surface
/// The center X of the circle
@@ -271,14 +277,14 @@ namespace MonoGame.Extended
/// The number of sides to generate
/// The color of the circle
/// The thickness of the line
- public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides,
- Color color, float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawCircle(this SpriteBatch spriteBatch, float x, float y, float radius, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawPolygon(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness);
+ DrawPolygon(spriteBatch, new Vector2(x, y), CreateCircle(radius, sides), color, thickness, layerDepth);
}
///
- /// Draw an ellipse.
+ /// Draw an ellipse.
///
/// The destination drawing surface
/// Center of the ellipse
@@ -286,22 +292,22 @@ namespace MonoGame.Extended
/// The number of sides to generate.
/// The color of the ellipse.
/// The thickness of the line around the ellipse.
- public static void DrawEllipse(this SpriteBatch spriteBatch, Vector2 center, Vector2 radius, int sides,
- Color color, float thickness = 1f)
+ /// The depth of the layer of this shape
+ public static void DrawEllipse(this SpriteBatch spriteBatch, Vector2 center, Vector2 radius, int sides, Color color, float thickness = 1f, int layerDepth = 0)
{
- DrawPolygon(spriteBatch, center, CreateEllipse(radius.X, radius.Y, sides), color, thickness);
+ DrawPolygon(spriteBatch, center, CreateEllipse(radius.X, radius.Y, sides), color, thickness, layerDepth);
}
private static Vector2[] CreateCircle(double radius, int sides)
{
- const double max = 2.0*Math.PI;
+ const double max = 2.0 * Math.PI;
var points = new Vector2[sides];
- var step = max/sides;
+ var step = max / sides;
var theta = 0.0;
for (var i = 0; i < sides; i++)
{
- points[i] = new Vector2((float) (radius*Math.Cos(theta)), (float) (radius*Math.Sin(theta)));
+ points[i] = new Vector2((float)(radius * Math.Cos(theta)), (float)(radius * Math.Sin(theta)));
theta += step;
}