Drawing boxes.

I have a couple of things in my app that are causing me concern. It’s currently in XNA, I’m hoping to move it to Mono later today.

Drawing simple boxes is a complete pain, I have to create rectangles then fill them, pixel by pixel with colour every single frame. I have to do this because the boxes change size, so I can’t use a texture object. This has to be extremely cpu expensive. I thought of using a textures, then drawing a black textures over the edges where the sizes are changing, but that seems like a crazily complicated way to draw a rectangle!
Is there a better way in Mono to draw a box? Or is there a better way to draw a box?

Another thing I’m concerned about, I have a rectangle on the screen where I need to create fuzz, like the static you used to get when a tele wasn’t tuned in. I don’t want to draw this pixel by pixel for the same reasons as above, but I also really don’t want that ‘repeating’ look you get when you rotate or randomize a number of textures.
Can anyone suggest a different way to achieve this look?

Many thanks.

Hey Munty,

So, I can think of two ways you can do the rectangle thing. If the rectangle is just a solid color, perhaps make a 1x1 texture, fill it with white. Then you can do something like this:

_spriteBatch.Draw(_whitePixel, new Rectangle(0, 0, _newWidth, _newHeight), _newColor);

That should allow you to move the rectangle around, color it how you want, and resize it to your hearts content.

The second way is more complicated and requires the use of 3D quads, transform matrices, and shaders. If you want to go down this route, there are resources around to help.

WRT the noise, look into Perlin Noise. That’s a pretty common way to make static in a texture. I found this with a quick google search: https://digitalerr0r.wordpress.com/2011/05/15/xna-shader-programming-tutorial-25-perlin-noise-using-the-gpu/

For the task of drawing primitives you could use some library.

Try this one (I use it as well):

It’s a static class providing extension methods for SpriteBatch. Nice work.
As for the second thing with the noise, I’ve never done that.