0

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??

GameKyuubi
  • 681
  • 1
  • 10
  • 25

1 Answers1

1

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.

XerShade
  • 137
  • 9
  • Okay, but why does clearing the GraphicsDevice in my second to last line cause the screen to go from black to red with my texture drawn over it? – GameKyuubi Nov 10 '16 at 12:49
  • It's about the ordering of your code, you are clearing and filling the screen BEFORE you end your sprite batch, which is what actually draws the sprites to screen. – XerShade Nov 13 '16 at 09:08
  • Right, but in my code, the last thing I do before End() is Clear(), but somehow my Texture2D is still visible. It changes the background color but doesn't remove the Texture2D. – GameKyuubi Nov 14 '16 at 02:03
  • Because SpriteBatch does NOT draw to the device until you call End(). This is why sprite batches are faster, they essentially "store" the draw calls and then push them all to the graphics card at once when you call End(). The reason for this is that it's not the actual drawing that takes time and slows the rendering down its sending the information to the graphics card and back. Think of it like sending a bunch of letters at once instead of running each to the post box one at a time. Until you actually send them it does not matter whats done to the post box. – XerShade Nov 15 '16 at 06:06
  • What your doing by calling Clear(Color.Red) then End() is clearing a blank canvas with red colour THEN drawing the sprite batch. – XerShade Nov 15 '16 at 06:09
  • Ah, I think I understand. I was thinking that Clear() was just another draw call that would be executed in order when End() is called along with the other draw calls, but what's actually happening is it's clearing everything immediately BEFORE the drawing even starts, so when End() is called the background has already been cleared with Color.Red and it just draws on top of it. Thanks! – GameKyuubi Nov 15 '16 at 06:24
  • Yeah, SpriteBatch and Device are completely separate things and whats on SpriteBatchs isn't drawn to the Device until you call End(). So yeah, you were basically clearing an empty device. – XerShade Nov 15 '16 at 13:03