Drawing Rectangles with SpriteBatch

March 29, 2010 C#, Code Snippets, Xna

Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch:

public static class SpriteBatchHelper { static Texture2D pixel; private static void LoadPixel(GraphicsDevice graphicsDevice) { if(pixel == null) { pixel = new Texture2D(graphicsDevice, 1, 1); pixel.SetData<Color>(new Color[] { Color.White }); } } public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color) { LoadPixel(spriteBatch.GraphicsDevice); spriteBatch.Draw(pixel, rectangle, color); } }