programming4us
programming4us
MOBILE

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us