MOBILE

Windows Phone 7 : Integration into the Game Framework (part 2) - Updates to the GameHost Class

2/22/2014 8:38:30 PM

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.

Other  
 
Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8