/
SpriteBatchPrimitiveExtensions.cs
89 lines (78 loc) · 2.94 KB
/
SpriteBatchPrimitiveExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Color = Microsoft.Xna.Framework.Color;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
using Vector2 = Microsoft.Xna.Framework.Vector2;
namespace IronXna
{
/// <summary>
/// A class to draw 2D Primitives.
/// If you want to use it, call SpriteBatchPrimitiveExtensions.Initialize in your LoadContent function
/// </summary>
public static class SpriteBatchPrimitiveExtensions
{
public static SubTexture2D WhitePixel;
public static void Initialize(GraphicsDevice graphics, SubTexture2D whitePixel = null)
{
//1px x 1px white texture
if (whitePixel != null)
{
WhitePixel = whitePixel;
}
else
{
Texture2D pixel;
#if WINDOWS_PHONE
pixel = new Texture2D(graphics, 1, 1);
pixel.SetData(new [] { 0xffffffff });
#else
pixel = Texture2D.FromStream(graphics, new MemoryStream(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAAxJREFUCNdj+P//PwAF/gL+3MxZ5wAAAABJRU5ErkJggg==")));
#endif
WhitePixel = new SubTexture2D(pixel);
}
}
public static void FillRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color, float depth = 0)
{
spriteBatch.Draw(WhitePixel, rectangle, color, 0, Vector2.Zero, SpriteEffects.None, depth);
}
/// <summary>
/// Renders a primitive line object.
/// </summary>
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 p, Vector2 p1, Color color, int width = 1, float depth = 0)
{
float distance = Vector2.Distance(p, p1);
float angle = (float)Math.Atan2(p1.Y - p.Y, p1.X - p.X);
spriteBatch.Draw(WhitePixel, p, color, angle, Vector2.Zero, new Vector2(distance, width), SpriteEffects.None, depth);
}
/// <summary>
/// Renders a primitive line object.
/// </summary>
public static void DrawLine(this SpriteBatch spriteBatch, Texture2D texture, Vector2 p, Vector2 p1, Color color, int width = 1, float depth = 0)
{
float distance = Vector2.Distance(p, p1);
float angle = (float)Math.Atan2(p1.Y - p.Y, p1.X - p.X);
spriteBatch.Draw(texture, p, null, color, angle, Vector2.Zero, new Vector2(distance, width / (float)texture.Height), SpriteEffects.None, depth);
}
/*
/// <summary>
/// Creates a circle starting from 0, 0.
/// </summary>
/// <param name="radius">The radius (half the width) of the circle.</param>
/// <param name="sides">The number of sides on the circle (the more the detailed).</param>
public void CreateCircle(float radius, int sides)
{
_vectors.Clear();
const float max = 2 * (float)Math.PI;
float step = max / sides;
for (float theta = 0; theta < max; theta += step)
{
_vectors.Add(new Vector2(radius * (float)Math.Cos(theta), radius * (float)Math.Sin(theta)));
}
// then add the first vector again so it's a complete loop
_vectors.Add(new Vector2(radius * (float)Math.Cos(0), radius * (float)Math.Sin(0)));
}
* */
}
}