If I wanted to fill my game screen with individually coloured pixels, how would I do this?
For example, if I wanted to write a ‘game of life‘-type game where each pixel was a cell, how would I achieve this using XNA?
I’ve tried just calling SetData() on a Texture2D object using a screen-sized array of Color values, but it complains with:
You may not call SetData on a resource while it is actively set on the GraphicsDevice.
Unset it from the device before calling SetData.
How do I do as it asks? Or better still… is there an alternative, better, efficient way to fill a screen with arbitrary pixels?
Regarding the error you’re getting, it means you’re attempting to modify the texture data while it is still bound to one of the sampler units on the device. Are you calling SetData inside of a SpriteBatch.Begin/End block? If you are, that’s the problem (update your texture before you do any drawing). Otherwise are you setting your texture directly on one of the device’s texture samplers, with GraphicsDevice.Textures[x] = yourTexture? If you are, set Textures[x] to null before you start changing the data.
As far as the efficiency of what you’re doing, it’s not ideal, but it probably doesn’t matter for your purposes. Try to keep the image resolution reasonable and realize you’re probably not going to hit 200 FPS with this approach, but I would try it and see if it’s fast enough, before you start worrying about performance.