How to draw a 2D pixel in XNA?

I am trying to draw pixel by pixel using XNA, but I have problems with resources. I thought the best way is to have 1 texture that updates every frame, but I am having problems updating it. Here is what I got so far as a test:

Texture2D canvas; Rectangle tracedSize; UInt32[] pixels; protected override void Initialize() { tracedSize = GraphicsDevice.PresentationParameters.Bounds; canvas = new Texture2D(GraphicsDevice, tracedSize.Width, tracedSize.Height, false, SurfaceFormat.Color); pixels = new UInt32[tracedSize.Width * tracedSize.Height]; base.Initialize(); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); pixels[100] = 0xFF00FF00; canvas.SetData<UInt32>(pixels, 0, tracedSize.Width * tracedSize.Height); spriteBatch.Begin(); spriteBatch.Draw(canvas, new Rectangle(0, 0, tracedSize.Width, tracedSize.Height), Color.White); spriteBatch.End(); base.Draw(gameTime); } 

When Draw () is called a second time, I get the following error:

"The operation was interrupted. You cannot change the resource that was installed on the device, or after it was used in the bracket for the tile."

If I try to create a new Texture2D in Draw (), I quickly get an error from memory. This is for Windows Phone. It looks like I'm trying to do it wrong, what other options do I need to make it work?

+6
c # xna 2d
source share
2 answers

Try setting GraphicsDevice.Textures[0] = null before calling SetData. Depending on the effect that you after it may be more effective, you may also consider Silverlight WriteableBitmap.

Edit: this is the code I tested in the emulator:

 Texture2D canvas; Rectangle tracedSize; UInt32[] pixels; protected override void Initialize() { tracedSize = GraphicsDevice.PresentationParameters.Bounds; canvas = new Texture2D(GraphicsDevice, tracedSize.Width, tracedSize.Height, false, SurfaceFormat.Color); pixels = new UInt32[tracedSize.Width * tracedSize.Height]; base.Initialize(); } Random rnd = new Random(); protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.Textures[0] = null; pixels[rnd.Next(pixels.Length)] = 0xFF00FF00; canvas.SetData<UInt32>(pixels, 0, tracedSize.Width * tracedSize.Height); spriteBatch.Begin(); spriteBatch.Draw(canvas, new Rectangle(0, 0, tracedSize.Width, tracedSize.Height), Color.White); spriteBatch.End(); base.Draw(gameTime); } 
+5
source share

You basically need to do what it asks for in the exception:

To make sure that the texture is not installed on the graphics device, put it at the end of Draw :

 GraphicsDevice.Textures[0] = null; 

To make sure that you are not drawing inside the tile bracket, do not use SetData inside Draw at all. Move the call to SetData in Update .


Bonus information: your memory error is due to the fact that you are not releasing unmanaged resources allocated by Texture2D (the garbage collector cannot track them, so it does not know that you have run out of memory). You should call Dispose on the texture. However, when creating a new texture, each frame is a bad idea (there is no way to avoid problems with fragmentation of performance and memory that it causes).

+3
source share

All Articles