Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Xna's Model class is the class that's used to draw 3D geometry. Usually in FlatRedBall this object is contained within a PositionedModel and is accessed by the PositionedModel's XnaModel property.
To set individual effects on a model, set the effects within the model's meshparts, like so:
Note that if you use the same effect in multiple places, the parameters may be changed on every use. You may want to Clone() the effect after loading it to ensure it is unique, then set the parameters.
Models store the Texture that they reference in their effect. Therefore, the Texture used on individual mesh parts can be retrieved and set through effects.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
As of XNA4 shared parameters are no longer set by the game engine.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The GraphicsDevice is the object which FlatRedBall interacts with. Since FlatRedBall does rendering automatically, most users will not need to interact with this object.
You may want to work with the GraphicsDevice in the following scenarios:
When using DrawableBatches.
When using render targets.
Microsoft.Xna.Framework.Graphics.GraphicsDevice.SetRenderTarget
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The ReferenceAlpha is a value that is used to test whether a pixel should be drawn according to its alpha value. The default value for this is 0. This is used in combination with the AlphaFunction property.
The following code results in all pixels being drawn:
In the code above, any alpha value greater than 0 will be drawn. In the following code, only alpha values greater than 128 (out of 255) will be drawn:
FlatRedBall XNA uses an alpha range of 0 to 1 (inclusive). This gets converted to a 0 to 255 range, so you may need to perform some math to convert between the two systems.
SpriteBatch allows games to render 2D graphics to screen. Unlike FlatRedBall's Sprite class, SpriteBatch performs immediate rendering - drawing with SpriteBatch results in graphics appearing for only one frame. To keep SpriteBatch graphics on screen for an extended amount of time, Draw must be called every frame. By contrast, FlatRedBall Sprites can be created one time and they will continue to appear on screen until removed from the engine. Three calls are required to render graphics using SpriteBatch:
SpriteBatch.Begin - sets initial values to be used for all Draw calls until End is called
SpriteBatch.Draw - draws a single sprite to the screen. Draw can be called multiple times between Begin and End, and performing multiple Draw calls between a Begin and End call can improve performance when compared to repeatedly calling Begin and End.
SpriteBatch.End - ends the drawing and presents the visuals on-screen or on the current RenderTarget2D.
The SpriteBatch.Draw method is used to render a Texture2D to screen. The Texture2D used can come from anywhere, including Texture2D instances loaded through Glue. The following code shows how to draw an image on screen. FINISH HERE after Entities support IDrawableBatch
The RenderState contains a variety of settings which can change now rendering is performed. FlatRedBall automatically sets RenderState values appropriately when drawing different types of objects, but users may need to modify RenderStates when using DrawableBatches.
The RenderState contains a variety of settings which can change now rendering is performed. FlatRedBall automatically sets RenderState values appropriately when drawing different types of objects, but users may need to modify RenderStates when using DrawableBatches.
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 SetRenderTarget function can be used to specify whether rendering will happen on a RenderTarget (if a non-null value is passed) or directly to the screen (if null is passed). By default no render target is set, which means all rendering will appear on-screen. Setting render targets can be used for a number of reasons:
To use a screen shot of the game at a later time
To save a screen shot to disk
To perform post processing or screen distortion
Note that SetRenderTarget must be called prior to performing any rendering. Typically this means setting the render target and either:
Using FlatRedBallServices.Draw to render everything currently in FlatRedBall to the RenderTarget
Explicitly calling Renderer.DrawCamera to render a camera to the render target.
Alternatively, RenderTarget rendering in FlatRedBall can be performed using Layers with RenderTargets. For more information, see the Layer.RenderTarget page.
Rendering to a RenderTarget enables you to use the resulting RenderTarget as a Texture2D, which can then be re-rendered to the screen. This can be done for a number of reasons:
To perform post-processing (modifications on the scene after it has been rendered such as bloom or blur)
To render to a portion of the screen then scale it up to make your game run more efficiently
Add the following at Game1's Class scope:
Add the following to Game1's Initialize:
Add the following to Game1's Draw:
This example shows how to render to a render target, then how to save it to disk when pressing the space bar:
Add the at class scope:
Add the following to CustomInitialize:
Add the following to Draw:
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: