-
Notifications
You must be signed in to change notification settings - Fork 1
/
Primitives.cs
executable file
·109 lines (92 loc) · 3.78 KB
/
Primitives.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Nano
{
/// <summary>
/// Line Batch
/// For drawing lines in a spritebatch
/// </summary>
static public class Primitives
{
static private Texture2D _empty_texture;
static private bool _set_data = false;
static public void Init(ContentManager content)
{
_empty_texture = content.Load<Texture2D>("pixel");
}
static public void DrawLine(SpriteBatch batch, Color color,
Vector2 point1, Vector2 point2)
{
DrawLine(batch, color, point1, point2, 0);
}
static public void DrawRect(SpriteBatch batch, Color color,
Rectangle rect)
{
Vector2 point1 = new Vector2(rect.Left, rect.Top);
Vector2 point2 = new Vector2(rect.Right, rect.Top);
Vector2 point3 = new Vector2(rect.Right, rect.Bottom);
Vector2 point4 = new Vector2(rect.Left, rect.Bottom);
DrawLine(batch, color, point1, point2, 0);
DrawLine(batch, color, point2, point3, 0);
DrawLine(batch, color, point3, point4, 0);
DrawLine(batch, color, point4, point1, 0);
}
/// <summary>
/// Draw a line into a SpriteBatch
/// </summary>
/// <param name="batch">SpriteBatch to draw line</param>
/// <param name="color">The line color</param>
/// <param name="point1">Start Point</param>
/// <param name="point2">End Point</param>
/// <param name="Layer">Layer or Z position</param>
static public void DrawLine(SpriteBatch batch, Color color, Vector2 point1,
Vector2 point2, float Layer)
{
//Check if data has been set for texture
//Do this only once otherwise
if (!_set_data)
{
_empty_texture.SetData(new[] { Color.White });
_set_data = true;
}
float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
float length = (point2 - point1).Length();
batch.Draw(_empty_texture, point1, null, color,
angle, Vector2.Zero, new Vector2(length, 1),
SpriteEffects.None, Layer);
}
static public void DrawFillRect(SpriteBatch batch, Color color, Vector2 point1,
Vector2 point2)
{
//Check if data has been set for texture
//Do this only once otherwise
if (!_set_data)
{
_empty_texture.SetData(new[] { Color.White });
_set_data = true;
}
Vector2 length = (point2 - point1);
batch.Draw(_empty_texture, point1, null, color,
0, Vector2.Zero, new Vector2(length.X, length.Y),
SpriteEffects.None, 0);
}
static public void DrawFillRect(SpriteBatch batch, Color color, Rectangle rect)
{
//Check if data has been set for texture
//Do this only once otherwise
if (!_set_data)
{
_empty_texture.SetData(new[] { Color.White });
_set_data = true;
}
batch.Draw(_empty_texture, new Vector2(rect.Left, rect.Top), null, color,
0, Vector2.Zero, new Vector2(rect.Width, rect.Height),
SpriteEffects.None, 0);
}
}
}