A Texture2D is a 2D array of pixel information which can be used by Sprites, Text objects, and PositionedModels when drawn. All FlatRedBall platforms support textures up to a max size of 2048x2048. FlatRedBall PC uses the Reach profile by default, but if switched to HiDef, textures up to 4096x4096 can be used.
A Texture2D by itself cannot be displayed in FlatRedBall. The Texutre2D must be drawn by a FlatRedBall object. The most common object for displaying Texture2Ds are Sprite. The following code shows how to render a Texture2D using a Sprite.
The following code loads a Texture2D from a file: Add the following using statement:
Call the following sometime after FlatRedBall is initialized
For more information on the Texture2D class, see the MSDN entry.
Each pixel in a Texture2D can be fully opaque, completely transparent, or have partial transparency. There are two ways to set the transparency of pixels in a Texture:
Save your image in a format that supports transparency, such as .tga or .png. When you save your image, make sure that you have transparent pixels set as transparent in the program that you are using to create or modify the image.
Use a color key for your image. You can specify a color that should be transparent when the engine creates a Texture2D out of it. The default value is black. For more information on a color key, see the TextureLoadingColorKey page.
The Bitmap object is an object commonly used in Windows programming. It is an object that represents an image loaded in RAM. It does not necessarily need to come from a .bmp image. Its flexibility can at times make it a useful "middle-man" for loading Texture2Ds. Assuming myBitmap is a valid Bitmap
When a texture is loaded into FlatRedBall, the source image width and height should be a power of two. Non-power of two image can be loaded, but they may be up-scaled which can result in blurry textures and wasted memory. For a list of power of two values, see this table.
Filtering - Explains why textures become blurry when displayed larger than actual size.
FlatRedBall.FlatRedBallServices.AddDisposable - Can be used to add a Texture2D to a FlatRedBall Content Manager.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The easiest way to create a Texture2D is to load a graphical file and create a Texture2D using FlatRedBallServices as follows:
While this code addresses many common scenarios, you may want to create your own textures programatically. This section discusses various topics on how to create Texture2Ds in code.
The following code creates a Texture2D which is all red, then displays the Texture2D using a Sprite.
Add the following using statement: