Other Helpers


The Helpers namespace contains a few more helper classes; most of them are simple like the RandomHelper class. It is not very exciting to go through all of them, so please review the ones that are not mentioned in this chapter yourself and test them out with the included unit tests if you want to know more about them.

Before you go into the Breakout game at the end of this chapter, take a quick look at some of the remaining helper classes, which will be used more frequently in the next chapters: SpriteHelper, EnumHelper, and ColorHelper.

SpriteHelper Class

You used a lot of sprite rendering in the previous chapter and because of the unit tests you forced yourself to write an easy way to handle sprites in XNA. This approach and the fact that you should put useful code that is used more than once into reusable classes leads us to the SpriteHelper class (see Figure 3-10). Basically it provides a constructor to create new SpriteHelpers storing the texture and graphic rectangle data and a few Render methods to easily draw the sprites to the screen like you did in the last chapter with a list of SpriteToRender classes.

image from book
Figure 3-10

Most methods here don’t do much; the constructor just sets the values, Render just adds a new SpriteToRender instance to the sprites list, RenderCentered renders the sprite centered at the specified location, and DrawSprites finally draws all the sprites on the screen. Take a look at the DrawSprites method, which will look similar to the DrawSprites method from the previous chapter, but with some improvements:

  public static void DrawSprites(int width, int height) {   // No need to render if we got no sprites this frame   if (sprites.Count == 0)     return;   // Create sprite batch if we have not done it yet.   // Use device from texture to create the sprite batch.   if (spriteBatch == null)     spriteBatch = new SpriteBatch(sprites[0].texture.GraphicsDevice);   // Start rendering sprites   spriteBatch.Begin(SpriteBlendMode.AlphaBlend,     SpriteSortMode.BackToFront, SaveStateMode.None);   // Render all sprites   foreach (SpriteToRender sprite in sprites)     spriteBatch.Draw(sprite.texture,       // Rescale to fit resolution       new Rectangle(       sprite.rect.X * width / 1024,       sprite.rect.Y * height / 768,       sprite.rect.Width * width / 1024,       sprite.rect.Height * height / 768),       sprite.sourceRect, sprite.color);       // We are done, draw everything on screen.       spriteBatch.End();       // Kill list of remembered sprites       sprites.Clear();     } // DrawSprites(width, height) 

This method is called with the current width and height of the render window resolution to scale all sprites up and down depending on the resolution, which is important to support all Xbox 360 screen resolutions. In the DrawSprites method you first check if there is something to render. Then you make sure the static sprite batch was created, which will be used for all sprites you draw here. After beginning the sprite batch you go through all sprites for this frame and rescale their rectangles to fit correctly on the screen, and finally you let everything draw on the screen by calling End. The sprites list is also killed to start fresh the next frame. As an example on how this class works see the Breakout game at the end of the chapter.

EnumHelper Class

The EnumHelper class (see Figure 3-11) is useful when you want to enumerate through any enum or quickly find out the number of enum values contained in an enum. For the Pong and Breakout games you are not using any enums, but in the next chapter the Enum class becomes useful when going through the types of blocks for the game there. Please also note that the EnumHelper class uses some methods of the Enum class, which are not implemented in the .NET Compact Framework. To avoid any compilation errors the whole class is usually left out of the Xbox 360 project, but you can still use it on the Windows platform if you like.

image from book
Figure 3-11

The unit test for TestGetAllEnumNames looks like this and also explains how GetAllEnumNames works. It goes through each enum value with help of the EnumEnumerator helper class inside of EnumHelper:

  [Test] public void TestGetAllEnumNames() {   Assert.AreEqual(     "Missions, Highscore, Credits, Help, Options, Exit, Back",     EnumHelper.GetAllEnumNames(typeof(MenuButton))); } // TestGetAllEnumNames() 

And the GetAllEnumNames just uses the WriteArrayData helper method of the StringHelper class just talked about:

  public static string GetAllEnumNames(Type type) {   return StringHelper.WriteArrayData(GetEnumerator(type)); } // GetAllEnumNames(type) 

ColorHelper Class

Initially the ColorHelper class (see Figure 3-12) was a lot longer and had many methods, but because the new Color class in XNA is much more powerful than the Color class from System.Drawings that is used in Managed DirectX, many of the methods were not required anymore. It still contains some useful methods you use for color manipulation.

image from book
Figure 3-12

For example, the Color.Empty field is used to initialize shader effect parameters to unused values - 0, 0, 0, 0 is usually not a valid color; it is completely transparent. Even black has 255 for the alpha value.

  /// <summary> /// Empty color, used to mark unused color values. /// </summary> public static readonly Color Empty = new Color(0, 0, 0, 0); 




Professional XNA Game Programming
Professional XNA Programming: Building Games for Xbox 360 and Windows with XNA Game Studio 2.0
ISBN: 0470261285
EAN: 2147483647
Year: 2007
Pages: 138

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net