views:

8148

answers:

4

I've read a bunch of tutorials involving XNA (and it's various versions) and I still am a little confused on drawing primitives. Everything seems to be really convoluted.

Can someone show me, using code, the simplest XNA implementation of drawing one or two lines on to the screen? Perhaps with a brief explanation (including the boilerplate)?

I'm not a games programmer and I have little XNA experience. My ultimate goal is to draw some lines onto the screen which I will eventually transform with rotations, etc (by hand). However, for this first step.. I need to simply draw the lines! I remember back in my ancient OpenGL days it was fairly straightforward when drawing a line with a few method calls. Should I simply revert to using unmanaged directx calls?

+5  A: 

When working with XNA, everything (even 2d primitives) have to be expressed in a way that a 3d card can understand, which means that a line is just a set of vertices.

MSDN has a pretty good walkthrough here:

http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF

You'll find that it takes more code to render a primitive line than it would take to just setup a textured quad and rotate that, since in essence, your doing the same thing when rendering a line.

FlySwat
@Jonathan Holland, Thanks for the link and explanation.
Simucal
+2  A: 

If you want to draw a 2d line there is a very easy approach using Texture2d and SpriteBatch described here:

http://v3ction.blogspot.com/2008/04/drawline-function-in-xna-using.html

Prensen
A: 

There is also the "round line" code that "manders" has released on CodePlex:


Here is the blog post about it:

Luke Quinane
A: 

Well, you can do it in a very simple way without getting into the 3D horrible vector stuff.

Just create a quick texture, for example:

Texture2D SimpleTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);

And then just draw a line using that texture:

this.spriteBatch.Draw(SimpleTexture, new Rectangle(100, 100, 100, 1), Color.Blue);

I hope this helps

No hay Problema