3dxna2dvertex-array

XNA - Drawing 2D lines


I already looked at http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF here they explain how to draw 2D lines in xna, but i get an exeption(see script)

            {
                int points = 3;//I tried different values(1,2,3,4)
                VertexPositionColor[] primitiveList = new VertexPositionColor[points];

                for (int x = 0; x < points / 2; x++)
                {
                    for (int y = 0; y < 2; y++)
                    {
                        primitiveList[(x * 2) + y] = new VertexPositionColor(
                            new Vector3(x * 100, y * 100, 0), Color.White);
                    }
                }
                // Initialize an array of indices of type short.
                short[] lineListIndices = new short[(points * 2) - 2];

                // Populate the array with references to indices in the vertex buffer
                for (int i = 0; i < points - 1; i++)
                {
                    lineListIndices[i * 2] = (short)(i);
                    lineListIndices[(i * 2) + 1] = (short)(i + 1);
                }
                GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                    PrimitiveType.LineList,
                    primitiveList,
                    0,  // vertex buffer offset to add to each element of the index buffer
                    8,  // number of vertices in pointList
                    lineListIndices,  // the index buffer
                    0,  // first index element to read
                    7   // number of primitives to draw
                    ); <---This parameter must be a valid index within the array. Parameter name: primitiveCount
            }

but i have no idea on how to fix this, since this is my first time making 2d graphics using 3d rendering

Background:

im making an 2d game engine for xna, and i want to make a class for drawing simple figures, i already know you can use a 1x1 Texture2D pixel trick for drawing, but i want to know this way as well, because else the CPU will be doing all of the calculations while the GPU can easily handle this.


Solution

  • Since you really didn't provide an error or anything, I'm just going to show you how I draw them.

    I use this extension method for drawing a single line, you will need a white 1x1 texture

    public static void DrawLine(this SpriteBatch spriteBatch, Vector2 begin, Vector2 end, Color color, int width = 1)
    {
        Rectangle r = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length()+width, width);
        Vector2 v = Vector2.Normalize(begin - end);
        float angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX));
        if (begin.Y > end.Y) angle = MathHelper.TwoPi - angle;
        spriteBatch.Draw(Pixel, r, null, color, angle, Vector2.Zero, SpriteEffects.None, 0);
    }
    

    This will also draw a shape made of points, closed defines if the shape should be closed or not

        public static void DrawPolyLine(this SpriteBatch spriteBatch, Vector2[] points, Color color, int width = 1, bool closed = false)
        {
    
    
            for (int i = 0; i < points.Length - 1; i++)
                spriteBatch.DrawLine(points[i], points[i + 1], color, width);
            if (closed)
                spriteBatch.DrawLine(points[points.Length - 1], points[0], color, width);
    
        }