In my Monogame (XNA) Windowed App, compiled for Windows PC, running on Windows 7, I have
public RenderTarget2D renderTarget = null;
In the Inititate() function I have
renderTarget = new RenderTarget2D(graphicsDevice, 200, 200, false,
graphicsDevice.PresentationParameters.BackBufferFormat,
graphicsDevice.PresentationParameters.DepthStencilFormat);
My drawing is as follows
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
// POSITION A
// draw some stuff to the back buffer
graphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer,
backgroundColor, 1, 0);
SceneRoot.Draw(gameTime); // draw some 3d primitives
spriteBatch.Begin();
spriteBatch.Draw(...some things...);
spriteBatch.End();
// POSITION B
spriteBatch.Begin();
spriteBatch.Draw((Texture2D)renderTarget, new Vector2(5f, 200f),
new Rectangle(0, 0, renderTarget.Width, renderTarget.Height),
Color.White);
spriteBatch.End();
}
If I put the following code to draw to the renderTarget in POSITION A then I can see the stuff drawn between POSITION A and POSITION B, with the stuff drawn to the renderTarget on top, as expected.
If I put the following code in POSITION B then I can't see anything drawn between POSITION A and POSITION B, just the stuff drawn to the renderTarget.
graphicsDevice.SetRenderTarget(renderTarget);
graphicsDevice.Clear(Color.Red);
spriteBatch.Begin();
spriteBatch.Draw(...some things...);
spriteBatch.End();
graphicsDevice.SetRenderTarget(null);
With the above code for insertion, I can even reduce it to
graphicsDevice.SetRenderTarget(renderTarget);
graphicsDevice.SetRenderTarget(null);
and still get the same result (only see what is drawn to the renderTarget, which is now nothing, ie a blank screen). I've tested, and it's the SetRenderTarget(renderTarget)
that's causing the problem, seemingly by wiping the backbuffer.
Oh, and none of the sprites drawn cover the whole display, so it's not the sprites displaying wrong, I don't think, as I should still see the 3d stuff.
I've looked and I don't see anything in SetRenderTarget about wiping the backbuffer. Can anyone help? Is it something else, like something to do with the RenderState or the Effects? I can provide more of my code if need be.