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]