2. Updates to the GameHost Class
The game framework's GameHost class needs a small enhancement to support rendering matrix objects. This takes the form of the DrawObjects function.
DrawObjects performs the same task for
matrix-based objects as DrawSprites performs for sprites: it draws all
the objects in the game, potentially filtering them by a specified
texture for performance reasons. However, we have a slight additional
complexity for objects: not all of them actually have
a texture. We need to be able to distinguish between drawing all the
objects (ignoring textures completely) and drawing the objects for
which the texture is null.
We achieve this by creating two public overloads,
one that takes a texture as a parameter and draws only objects that use
that texture, and one that doesn't take a texture parameter and draws
all the objects. Internally, each of these calls into a third (private)
overload that expects to be explicitly told whether to filter on
textures or not. This allows it to look for objects whose ObjectTexture value is null and draw them alone should this be required.
The code for these functions is shown in Listing 11.
Example 11. The DrawObjects function overloads in the GameHost class
/// <summary> /// Call the Draw method on all matrix objects in the game /// </summary> public virtual void DrawObjects(GameTime gameTime, Effect effect) { DrawObjects(gameTime, effect, false, null); }
/// <summary> /// Call the Draw method on all matrix objects in the game that use /// the specified texture. Pass as null to draw only objects that do /// not have a texture specified at all. /// </summary> public virtual void DrawObjects(GameTime gameTime, Effect effect, Texture2D restrictToTexture) { DrawObjects(gameTime, effect, true, restrictToTexture); }
/// <summary> /// Draw the specified objects /// </summary> private void DrawObjects(GameTime gameTime, Effect effect, bool specifiedTextureOnly, Texture2D restrictToTexture) { GameObjectBase obj; int objectCount;
// Draw each matrix-based object objectCount = _objectArray.Length; for (int i = 0; i < objectCount; i++) { obj = _objectArray[i]; // Is this a matrix object? if (obj is MatrixObjectBase) { // Does this object use the required texture? if (specifiedTextureOnly == false || ((MatrixObjectBase)obj).ObjectTexture == restrictToTexture) { ((MatrixObjectBase)obj).Draw(gameTime, effect); } } } }
|
3. Using the Game Framework for Matrix Rendering
The game framework is now all ready to use for rendering our objects, so how is it used in a game project?
This is very easy, as can be seen in the GameFrameworkExampleGame class in the example project. All the class-level variables have been removed except for the BasicEffect variable, and the Initialize function has been reduced in size so that it simply creates and initializes this effect object.
LoadContent now loads its textures into the Textures collection that we've used throughout all the sprite examples. At the end, it calls ResetGame to create the game's objects. ResetGame, shown in Listing 12, simply adds some instances of the game project's TexturedSquareObject class to the GameObjects collection.
Example 12. Resetting the example project
private void ResetGame() { // Clear any existing objects GameObjects.Clear();
// Add some new game objects GameObjects.Add(new TexturedSquareObject(this, new Vector3(0, 0.6f, 0), Textures["Grapes"], 2.0f)); GameObjects.Add(new TexturedSquareObject(this, new Vector3(0, −0.6f, 0), Textures["Strawberry"], 2.0f)); }
|
Finally we call UpdateAll from the game's Update function and DrawObjects from the game's Draw function. That's all that is needed.