I am creating a game in XNA that will require thousands of tiny rectangles / squares to be drawn. As the number increases, the performance gets worse. Here is the code I am current using:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (SandBit sandBit in sandBitManager.grid)
{
Point p = sandBit.Position;
spriteBatch.Draw(square, new Rectangle(p.X, p.Y, sandBit.SIZE, sandBit.SIZE), Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
I am calling spriteBatch.Draw()
for every single square, and I am redrawing the entire screen just to add a single square. I have done a significant amount of searching and I believe a solution is to draw to one texture and then call Draw()
on that texture, but I cannot find relevant examples of this.