Frage

I would like to use a RenderTarget2D in my Windows Phone 7 XNA application. However, I am unsuccessful because switching drawing to my render target and then switching back (via using SetRenderTarget(null) ) causes my WHOLE screen to be drawn in blue and thus overwrites anything that was drawn before switching to my render target. I am not sure if this is expected behavior or not.

In fact, it is very easy to reproduce this behavior. Just create a bare bones XNA game for Windows Phone 7 and use this code:

protected override void Draw(GameTime gameTime)
{
   spriteBatch.Begin();
   spriteBatch.Draw(textureBackground, new Vector2(0,0), Color.White); // This is a 480x800 image and my resolution is set to 480x800
   spriteBatch.End(); // Testing this block of code confirms that the textureBackground is being drawn

   graphics.GraphicsDevice.SetRenderTarget(textureBuffer);

   spriteBatch.Begin();
   spriteBatch.Draw(textureSummer, new Vector2(0, 0), Color.White); // texture Summer is a 20x20 pixels texture while the screen resolution is 480 x 800
   spriteBatch.End();

   graphics.GraphicsDevice.SetRenderTarget(null); // switch back 

   spriteBatch.Begin();
   spriteBatch.Draw(textureBuffer, new Vector2(0,0), Color.White);
   spriteBatch.End(); // at this point, textureBuffer is drawn (the 20x20 pixeles image) in the upper left hand corner but the background of the whole screen is BLUE and so textureBackground texture has been overwritten by this color and is not showing anymore.

   // At this point all I see is a blue background with a 20x20 pixels image in the upper left hand corner.
}

I am creating my render target as follows:

textureSummer = Content.Load<Texture2D>("summer_picture_icon"); // the 20x20 pixels texture
textureBuffer = new RenderTarget2D(graphics.GraphicsDevice, textureSummer.Width, textureSummer.Height); // the RenderTarget2D object. Note that this RenderTarget is only 20x20.

Therefore, what am I doing wrong ?

Thanks.

War es hilfreich?

Lösung

The "problem" is that you draw the background first, then change rendertarget, then render that little square, then change rendertarget, then draw the little square again. in this order:

  • Render on Screen stuff
  • Rendertarget Change
  • Render off-screen stuff
  • Rendertarget Change
  • Render on-screen stuff

Every change of rendertarget clears it.

What you should do;

  • Rendertarget Change
  • Render off-screen stuff
  • Rendertarget Change
  • Render on-screen stuff

Like this:

GraphicsDevice.SetRenderTarget(textureBuffer);

spriteBatch.Begin();
spriteBatch.Draw(textureSummer, new Vector2(0, 0), Color.White); // texture Summer is a 20x20 pixels texture while the screen resolution is 480 x 800
spriteBatch.End();

GraphicsDevice.SetRenderTarget(null); // switch back 

spriteBatch.Begin();
spriteBatch.Draw(textureBackground, new Vector2(0,0), Color.White); // This is a 480x800 image and my resolution is set to 480x800
spriteBatch.Draw(textureBuffer, new Vector2(0,0), Color.White);
spriteBatch.End();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top