/
LineBatch.cs
67 lines (57 loc) · 2.17 KB
/
LineBatch.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Rain
{
/// <summary>
/// Line Batch
/// For drawing lines in a spritebatch
/// </summary>
static public class LineBatch
{
static private Texture2D _empty_texture;
static private bool _set_data = false;
static public void Init(GraphicsDevice device)
{
_empty_texture = new Texture2D(device, 1, 1, true, SurfaceFormat.Color);
}
static public void DrawLine(SpriteBatch batch, Color color,
Vector2 point1, Vector2 point2)
{
DrawLine(batch, color, point1, point2, 0);
}
//Added 8/20/2011
static public void DrawLine(SpriteBatch batch, Camera camera, Color color,
Line line, float layer= 0)
{
DrawLine(batch, color, camera.getRenderPosition(line.Point1), camera.getRenderPosition(line.Point2), layer);
}
/// <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);
}
}
}