Last updated
Was this helpful?
Last updated
Was this helpful?
The RenderTargetSprite class provides a simple way to perform rendering using a RenderTarget2D. Although FlatRedBall provides support for using MonoGame render targets, the RenderTargetSprite simplifies the creation and management of render targets. RenderTargetSprite instances provide a FlatRedBall Layer for adding visual objects to the sprite.
To understand how a RenderTargetSprite works, we can compare it with a normal FlatRedBall Layer. A FlatRedBall Layer can contain any visual objects such as FlatRedBall Sprites, MapDrawableBatch (Tiled), and Gum visual objects. The purpose of the Layer is to control the sorting of objects. It cannot modify the objects being drawn such as by applying additional rotation, position offsets, or blend operations. RenderTargetSprites provide additional control over how multiple objects are drawn. For example, a RenderTargetSprite can apply a blend operation to all rendered objects to create a darkness effect. Furthermore, RenderTargetSprites provide additional sorting support by providing sorting both on the Z axis as well as by-Layer.
FlatRedBall provides a number of ways to render to a render target. The RenderTargetSprite allows you to perform rendering onto a FlatRedBall Sprite. This is useful if you would like that resulting RenderTarget (texture) to be drawn to screen using a FlatRedBall Sprite. This can make it easier to position the render target and perform standard color operations. If you would like to create a RenderTarget (texture) which you process yourself (such as by rendering using SpriteBatch) then the Layer's RenderTarget property can be used as a more lightweight option. For more information see the page.
This example shows how to create a dark overlay on your game, with light sprites providing brightness. This approach does not over-saturate bright areas, nor does it produce rendering artifacts where lights overlap. It creates five sprites, one of which can be controlled with the keyboard. Note that this assumes:
The Screen has some other visuals to be drawn under the RenderTargetSprite
The WhiteLight Texture is available to the Screen, such as by being added to the Screen in Glue
By default the RenderTargetSprite will draw itself, and it will respond to variable changes such as position and rotation. For performance reasons, the RenderTargetSprite does not automatically refresh its contents, even if contained objects are moved. The Refresh function can be called to update the visual appearance of the contents of the RenderTargetSprite. Note that calling Refresh every frame will not cause performance problems for most games. Of course if your game doesn't need it, you can improve performance by calling Refresh only as needed.