What's the simplest way to draw a rectangular outline without generating the texture?

I’m looking for a simple way to draw a rectangular border/outline, or just a simple line, without having to either generate a texture first. I’ve seen people draw “pixel by pixel” using a texture, which seems horrible.

I do have some experience with OpenGL, so I kind of know how I would use this by using OpenTK directly, but I’m not sure if that’s a good practice? Is there some MonoGame specific API that allows access for primitive drawing?

Check out GraphicsDevice.Draw*Primitives.

1 Like

Thanks! That’s exactly what I was looking for.

1 Like

I have been using the following technique to create a filled rectangle using a Texture2D but not loading Content into it:

Texture2D _texture;

_texture = new Texture2D(GraphicsDevice, 1, 1);
_texture.SetData(new Color[] { Color.DarkSlateGray });

SpriteBatch.Draw(_texture, new Rectangle(100, 100, 100, 100), Color.White);

Yeah, that’s a good tip :slight_smile: I often use an extension method for spritebatch to have a blank texture:

private static Texture2D _blankTexture;

public static Texture2D BlankTexture(this SpriteBatch s)
{
    if (_blankTexture == null)
    {
        _blankTexture = new Texture2D(s.GraphicsDevice, 1, 1);
        _blankTexture.SetData(new[] {Color.White});
    }
    return _blankTexture;
}
2 Likes

Where should this part go in code? Classes? Or the Game structure itself?

Why not use a texture for the line? Why would anyone do it a pixel at a time unless they did it in a shader?

I did a post many moons ago on line intersection in XNA. My server recently got nuked, so i think the linjs may be broken though :frowning:

You can see the post here.