The ImageData class represents modifiable data that can be created from a Texture2D, or be used to construct a Texture2D. The ImageData class can be used to easily interact with the Texture2D class, and also provides a cross-platform way of creating and modifying textures.
Add the following using statements:
Add the following to Initialize after initializing FlatRedBall:
Note: This code uses the Color and Texture2D classes. Remember, if you are using a version of FlatRedBall other than FlatRedBall XNA, your using statements will be different. For more information, see:
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
FromTexture2D creates an ImageData object, filled with data from a Texture2D instance. It's important to note that the ImageData is a copy of the Texture2D, so making changes to the ImageData will not change the source Texture2D.
The following code shows how to create an ImageData instance from a Texture2D instance, then loop through each pixel:
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 Layer.RenderTarget 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.
ApplyColorOperation is a method which can be used to modify an ImageData using the same ColorOperation and Red,Green,Blue values that are present in the IColorable interface which is implemented by common FlatRedball objects such as Sprite and Text. ApplyColorOperation modifies the data stored in an ImageData permanently - it cannot be undone.
ApplyColorOperation can be used to modify a texture so that when rendered normally, it will appear the same as a Sprite with the same ColorOperation and Red/Green/Blue values. The following code shows a texture being rendered using a Modulate color operation:
The Fill method sets the entire ImageData to be the same color. For example, to make the entire ImageData black, use the following call:
Keep in mind that filling an ImageData is much slower than rendering to a texture. If you are working with large ImageDatas, avoid calling Fill on them frequently for performance reasons.