Okay, so this is where I am now. My game largely consists of textures drawn through spritebatch. Now I have to draw a dynamic number of triangles. I cannot say in advance where or how many I have to draw, because I have to generate their positions on the fly. The number will never top 3-400 though, and is often much lower.
When testing though, I noticed that at random moments the game became a bit laggy. I'm quite confident the vertex-drawing was the problem, if I commented it out, the lag was gone.
This is the code I have currently (I removed as much clutter as possible):
// loop through the grid
VertexPositionColor[] vertices;
int vertexcounter = 0;
RasterizerState state = new RasterizerState();
state.CullMode = CullMode.None;
for(int i = startx; i < endx; i++)
for (int j = starty; j < endy; j++)
{
//////
vertices = new VertexPositionColor[3];
vertexcounter = 3;
vertices[0] = new VertexPositionColor(Vector3.Zero, Color.Black);
vertices[1] = new VertexPositionColor(new Vector3(width / 2, 0, 0), Color.Black);
vertices[2] = new VertexPositionColor(new Vector3(0, height / 2, 0), Color.Black);
//////
// Create a buffer to use
vertexBuffer = new VertexBuffer(device, typeof(VertexPositionColor), vertexcounter, BufferUsage.WriteOnly);
vertexBuffer.SetData<VertexPositionColor>(vertices);
// Draw the triangles
basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = projection;
basicEffect.VertexColorEnabled = true;
device.SetVertexBuffer(vertexBuffer);
device.RasterizerState = state;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawPrimitives(PrimitiveType.TriangleList, 0, vertexcounter / 3);
}
vertexcounter = 0;
vertexBuffer.Dispose();
}
Again, I removed as much clutter as possible. The basicEffect and matrices are created in the constructor. The for loop doesn't really make sense probably, but the part between the ////'s used to be for retrieving the vertices from objects in a grid (which is what the coordinates were for).
As you can see I removed that part and instead draw one single triangle. The game still lags though, so I think I'm doing something else wrong. Are you not really supposed to create VertexBuffers on the fly? Or am I forgetting to dispose of certain data?
Also, maybe it is worth noting that this is done in the Update part of the object. This is because the image is drawn to a RenderTarget, and in my project it's just more convenient to do it in the Update method.
Thanks in advance :)