0

So I am trying to store the result from a render target into an array of Colors so I can manipulate them individually with an iterator. For some reason, the below code sets every value in the array to R=0, G=0, B=0, A=0. As though it was never initialized. Is the GetData method used incorrectly? Is it something else? It should be a very colorful image, and definitely not transparent.

The issue is NOT that the textures are not drawing properly, I executed the code with no set render target (using the default backbuffer) and the game ran perfectly fine.

            //Creating a new render target and setting it
        RenderTarget2D unprocessed = new RenderTarget2D(graphics.GraphicsDevice, Window.ClientBounds.Width, Window.ClientBounds.Height,false,SurfaceFormat.Color,DepthFormat.Depth24,4,RenderTargetUsage.PreserveContents);
        graphics.GraphicsDevice.SetRenderTarget(unprocessed);

        //Drawing background and main player
        spriteBatch.Draw(bgTex,new Rectangle(0,0,Window.ClientBounds.Width,Window.ClientBounds.Height),Color.White);
        mainPlayer.Draw(spriteBatch);

        //resetting render target
        graphics.GraphicsDevice.SetRenderTarget(null);

        //creating array of Color to copy render to
        Color[] baseImage = new Color[Window.ClientBounds.Width * Window.ClientBounds.Height];
        //I pause the program here and baseImage is an array of black transparent colors
        unprocessed.GetData<Color>(baseImage);

        spriteBatch.End();
Kartik
  • 104
  • 1
  • 2
  • 10

2 Answers2

0

Problem solved. Basically the reason why it is black is because the spritebatch needs to end first before the render target 'gets' the data. That is why during the spritebatch process the color values are still black. What I did to fix it was I made the spritebatch begin and end right before and after the drawing occurs and that fixed the problem immediately.

Kartik
  • 104
  • 1
  • 2
  • 10
0

I believe there are a couple of problems. The first of which is that you need to call spriteBatch.End() prior to acessing the render target. I also just noticed there is no call to spriteBatch.Begin().

RenderTarget2D unprocessed = new RenderTarget2D( /*...*/);
graphics.GraphicsDevice.SetRenderTarget(unprocessed);
graphics.GraphicsDevice.Clear(Color.Black);

//Drawing
spriteBatch.Begin();
spriteBatch.Draw(/*...*/);
spriteBatch.End();

//Getting
graphics.GraphicsDevice.SetRenderTarget(null);

You just answered your own question so.... Nevermind then.

Nathan Runge
  • 303
  • 2
  • 10
  • haha thanks anyways! I noticed the code was missing a spritebatch begin, but it was just because i forgot to include it in the code that I copied. – Kartik Jun 28 '12 at 03:00