0
votes

Here's a draw block:

graphicsDevice.Clear(Color.CornFlowerBlue);
spriteBatch.Begin();
graphicsDevice.SetRenderTarget(renderTarget);
spriteBatch.Draw(texture2D, position, etc);
graphicsDevice.SetRenderTarget(null);
graphicsDevice.Clear(Color.Red);
spriteBatch.End();

This produces my Texture2D on top of a red background. Why does this draw my Texture2D to the screen? When spriteBatch.Draw is called the RenderTarget is NOT the backbuffer, and I'm not drawing the RenderTarget in this code. Shouldn't the texture2D not display since it's drawn offscreen? Also, why does nothing display (just black) unless I clear the screen right before ending spriteBatch??

1

1 Answers

1
votes

Try arranging the source code like this,

graphicsDevice.Clear(Color.CornFlowerBlue);
graphicsDevice.SetRenderTarget(renderTarget);
spriteBatch.Begin();
spriteBatch.Draw(texture2D, position, etc);
spriteBatch.End();

From my experience you need to set the render target before you actually start drawing with the sprite batch.

As to why your back buffer is black when your done, it's because .SetRenderTarget(null); clears the back buffer as well as the render target, that is why the screen is drawing black (nothing). You should do all your RenderTarget drawing first so when you switch back to the back buffer it is not erasing anything.