Visual C# (Windows) Guide
SFML Graphics: Drawing Lines - VertexArray
One method for drawing lines is to use a VertexArray. This is an SFML data structure that works a little bit like a list. You add pairs of Vector2f points to represent the lines that you want to draw. To test this out, amend you Canvas class as follows,
Add the following field,
// Data structure to store vertex pairs for lines
private VertexArray Lines;
Add the following line to the constructor to initialise the data structure,
Lines = new VertexArray(PrimitiveType.Lines);
Here is a method for adding vertex pairs to the VertexArray,
public void AddLine(Vertex start, Vertex end)
{
Lines.Append(start);
Lines.Append(end);
this.Refresh();
}
Add the following line to the Draw method to ensure that lines are drawn to the canvas,
RendWind.Draw(Lines);
Back to the main form and we can rewrite the code as follows to get some randomly positioned, randomly coloured lines,
public partial class Form1 : Form
{
private SFML.Graphics.Color[] colours =
{
SFML.Graphics.Color.Blue,
SFML.Graphics.Color.Green,
SFML.Graphics.Color.Red,
};
private Random rng = new Random();
public Form1()
{
InitializeComponent();
// Create an instance of the canvas and add to the form
SFMLCanvas Canvas = new SFMLCanvas(400, 400, SFML.Graphics.Color.White);
Canvas.Location = new Point(20, 20);
this.Controls.Add(Canvas);
Vertex a, b;
for (int i = 0; i < 10; i++)
{
a = new Vertex(new SFML.System.Vector2f(
(float)rng.NextDouble() * Canvas.Width,
(float)rng.NextDouble() * Canvas.Height)
, colours[rng.Next(colours.Length)]);
b = new Vertex(new SFML.System.Vector2f(
(float)rng.NextDouble() * Canvas.Width,
(float)rng.NextDouble() * Canvas.Height)
, colours[rng.Next(colours.Length)]);
Canvas.AddLine(a, b);
}
}
}
Note that you define the colour of the line along with the vertices. Your line will be a gradient if the two colours for the line are not the same.

