I use the following method to draw a box onto the screen:
private void DrawBox(Rectangle rect, Color color, Vector2 offset)
{
Texture2D tex = new Texture2D(GraphicsDevice, rect.Width, rect.Height);
Color[] data = new Color[rect.Width * rect.Height];
for (int i = 0; i < data.Length; i++)
data[i] = color;
tex.SetData(data);
var pos = new Vector2(rect.Location.X, rect.Location.Y) + offset;
spriteBatch.Draw(tex, pos, Color.White);
tex.Dispose();
}
However, this results in a completely black image being drawn onto the screen, regardless of the colour I pass to this method. Why is this happening and how can I fix this?
SpriteBatch.draw
doesn't actually draw, that happens later. I suspect that byDisposing
of your texture immediately after the call, you are destroying the data needed to plot it correctly before it is actually being drawn. \$\endgroup\$