[SlimDX]Why does this code fail?

Started by
3 comments, last by Promit 15 years, 4 months ago
This below code throws an invalid operation exception when I try to write to my Vertex Buffer with DataStream.WriteRange(). The code itself doesn't produce a grid like I wanted it to but, I am not worried about that right now. I am just wondering why it is throwing an exception here:

 public Grid(Device device, int width, int height, Vector2 tileSize)
        {
            if(device == null)
                throw new InvalidOperationException("Graphics Device cannot be null");

            amountOfTiles = width * height;

            this.device = device;
            this.vertices = new VertexBuffer ( device, D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2), Usage.None, UntransformedVertex.FVFFormat, Pool.Default );

            DataStream dStream = vertices.Lock ( 0,
                                                 D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2),
                                                 LockFlags.NoSystemLock );
            
            //loop through all vertical lines
            for (float y = 0; y < height * tileSize.Y; y += tileSize.Y)
            {

                //loop through all horizontal tiles
                for (float x = 0; x < width * tileSize.X; x += tileSize.X)
                {

                    Color tileColor = Color.Blue;

                    UntransformedVertex[] tileVerts = new UntransformedVertex[4];
                    
                    //(2)---------(3)
                    //|***********/*|
                    //|*********/***|
                    //|*******/*****|
                    //|*****/*******|
                    //|***/*********|
                    //|*/***********|
                    //(0)*********(1)
                    tileVerts[0] = new UntransformedVertex ( new Vector3 ( x, y, 10 ), tileColor.ToArgb() );
                    tileVerts[1] = new UntransformedVertex ( new Vector3 ( x + tileSize.X, y, 10 ), tileColor.ToArgb() );
                    tileVerts[2] = new UntransformedVertex ( new Vector3( x, y + tileSize.Y, 10 ), tileColor.ToArgb() );
                    tileVerts[3] = new UntransformedVertex( new Vector3(x + tileSize.X, y + tileSize.Y, 10), tileColor.ToArgb() );

                    dStream.WriteRange ( tileVerts );
                }
            }

            vertices.Unlock ();
            dStream.Dispose ();
        }


when I switch it with this it doesn't throw an invalid operation exception:


        public Grid(Device device, int width, int height, Vector2 tileSize)
        {
            if(device == null)
                throw new InvalidOperationException("Graphics Device cannot be null");

            amountOfTiles = width * height;

            this.device = device;
            this.vertices = new VertexBuffer ( device, D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2), Usage.None, UntransformedVertex.FVFFormat, Pool.Default );

            
            //loop through all vertical lines
            for (float y = 0; y < height * tileSize.Y; y += tileSize.Y)
            {

                //loop through all horizontal tiles
                for (float x = 0; x < width * tileSize.X; x += tileSize.X)
                {

                    Color tileColor = Color.Blue;

                    DataStream dStream = vertices.Lock ( 0,
                                                         D3DX.GetFVFVertexSize ( UntransformedVertex.FVFFormat ) * (amountOfTiles * 2),
                                                         LockFlags.NoSystemLock );

                    UntransformedVertex[] tileVerts = new UntransformedVertex[4];
                    
                    //(2)---------(3)
                    //|***********/*|
                    //|*********/***|
                    //|*******/*****|
                    //|*****/*******|
                    //|***/*********|
                    //|*/***********|
                    //(0)*********(1)
                    tileVerts[0] = new UntransformedVertex ( new Vector3 ( x, y, 10 ), tileColor.ToArgb() );
                    tileVerts[1] = new UntransformedVertex ( new Vector3 ( x + tileSize.X, y, 10 ), tileColor.ToArgb() );
                    tileVerts[2] = new UntransformedVertex ( new Vector3( x, y + tileSize.Y, 10 ), tileColor.ToArgb() );
                    tileVerts[3] = new UntransformedVertex( new Vector3(x + tileSize.X, y + tileSize.Y, 10), tileColor.ToArgb() );
                    
                    
                    dStream.WriteRange ( tileVerts );

                    vertices.Unlock ();
                    dStream.Dispose ();
                }
            }
        }


Also, would it be better, speedwise, to put all of the vertices in a 2 dimensional array and then write it all to the vertex buffer in one big WriteRange() call? (btw this code was written with SlimDX) [Edited by - jdub on December 18, 2008 9:35:00 AM]
J.W.
Advertisement
I am still looking for an answer as to why my code doesn't work. Help is very much appreciated
J.W.
Where do you get the exception, and what do the exception details say?
Mike Popoloski | Journal | SlimDX
My problem lies on this line:
dStream.WriteRange ( tileVerts );

it throws an InvalidOperation exception, but doesn't say anything about the problem.
J.W.
You're stepping out of the bounds of the VB somehow. The second version doesn't crash because it repeatedly writes to the beginning of the vertex buffer. Check what your X and Y is at the point of crash compared to the size.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement