I am reading a book called "Monogame Mastery" about Monogame framework for .NET. And in the chapter-04 there is a part that explains how we scale and locate our content of game to properly position it on window. And I am confused about what RenderRectangle2D is. Can you explain what that is in simple words? Code from chapter:
protected override void Draw(GameTime gameTime)
{
// Render to the Render Target
GraphicsDevice.SetRenderTarget(_renderTarget);
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
_currentGameState.Render(spriteBatch);
spriteBatch.End();
// Now render the scaled content
graphics.GraphicsDevice.SetRenderTarget(null);
graphics.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 1.0f, 0);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
spriteBatch.Draw(_renderTarget, _renderScaleRectangle, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
I would explain it as a draft piece of paper that your draw on and only then show it on the screen. Is it a good understanding from me now? If yes, why do we need it then when we can just draw on screen directly?
Render targets act as an intermediate texture. There are some specific situations when you want to render on a RenderTarget2D first. Here are a couple of use cases
If you want a resizable game window, you can first render your entire game on top of a RenderTarget2D and then scale it to fit the entire window.
SpriteBatch has support for pixel shaders or Effects. These act as image filters and allow you to change the color of every single pixel of the texture very fast. Sometimes you need to apply an Effect to the entire screen but not to every sprite separately. In this case you first draw all sprites on a RenderTarget2D and then draw the RenderTarget2D on the screen with a specified effect (or on top of another RenderTarget2D if you want to apply another effect)
Let's say you need to render a grid of 10 000 tiles every frame (Like in Terraria or Noita for example). Instead of re-rendering it from scratch every frame, you can render it once on top of a RenderTarget2D and then re-render only the parts that have changed. This way you save a lot of FPS