Drawing lines with the SpriteBatch

Have you ever wondered why SpriteBatch doesn’t allow simple line drawing? Well, I have.

And I never really understood why such basic and useful functionality isn’t supported by SpriteBatch. Well, the creators might have a good reason for it, but I just want to draw lines!

This post will be about the SpriteBatchEx class I made. It’s actually an extension to the regular SpriteBatch using extension methods.

I added three new methods to the SpriteBatch class that allow you to easily draw lines!

The three methods are:

DrawLine(Vector2 begin, Vector2 end, Color color, int width = 1)

DrawSingleLine(Vector2 begin, Vector2 end, Color color, int width = 1)

DrawPolyLine(Vector2[] points, Color color, int width = 1, bool closed = false)

DrawLine and DrawSingleLine are very similar, they both take some parameters (begin, end, color and width) and draw a line. The difference is that DrawSingleLine() calls SpriteBatch.Begin() and SpriteBatch.End() internally, while DrawLine() requires the user to do this.

DrawPolyLine takes an array of vectors, a color and a width and draws lines from one point to the next like you would expect. If the last parameter, closed, is set to true, it will also draw a line from the last point of the array to the first point. This method calls SpriteBatch.Begin() and SpriteBatch.End() internally, so you don’t have to do this.

Although these methods are defined in the SpriteBatchEx class, you can just call spriteBatch.DrawLine() etc. All this thanks to extension methods!

To use these methods, just follow these simple steps:

  1. Add SpriteBatchEx.cs to your project
  2. Add: 
    1. using LineBatch;
  3. In your Initialize method, write:
    1. SpriteBatchEx.GraphicsDevice = GraphicsDevice

That’s it!

At the bottom of the this post, you can find the links to download SpriteBatchEx.cs as well as a sample project that uses these DrawLine methods.

Here’s some example code:

            // There are three ways of drawing lines.
            // First the DrawLine.
            spriteBatch.DrawLine(new Vector2(100, 100), new Vector2(300, 200), Color.Black);
            spriteBatch.DrawLine(new Vector2(300, 200), new Vector2(400, 100), Color.Black);

            // The second is DrawSingleLine
            // This method doesn't require spriteBatch.Begin or End, it does this internally
            // NOTE: here, we also change the color and width
            spriteBatch.DrawSingleLine(new Vector2(100, 300), new Vector2(300, 400), Color.DarkRed, 4);
            spriteBatch.DrawSingleLine(new Vector2(300, 400), new Vector2(400, 300), Color.DarkRed, 4);

            // The third one is DrawPolyLine
            // This method doesn't require spriteBatch.Begin or End either, it does this internally.
            // We need to pass this method an array of Vector2s
            Vector2[] points = new Vector2[5];
            points[0] = new Vector2(400, 200);
            points[1] = new Vector2(500, 300);
            points[2] = new Vector2(600, 250);
            points[3] = new Vector2(700, 150);
            points[4] = new Vector2(600, 100);

            // By setting the last parameter to true, you can close the polygon, try it!
            spriteBatch.DrawPolyLine(points, Color.Green, 1, false);

Here’s a screenshot of what it draws:

However, these methods aren’t perfect.

One limitation is that it doesn’t do much at the connection of two line pieces. You can see this if you draw very thick lines (with around 50 width)

It’s probably also not the fastest way to draw lines, but I wanted to keep it short. Besides, I usually draw lines as a debugging/analysis tool where that little bit of performance doesn’t matter.

For most line drawing tasks, it works well enough though.

Also note that in the sample project, we enabled multisampling, we do this, because aliased lines (especially thin lines) look bad.

That’s all about these line drawing methods, if you’ve got any questions or if I’ve forgotten to mention something, please post in the comments below!



LineBatchSample (Also contains SpriteBatchEx.cs)