Search

Advertising

Home Downloads Components

Components

Folder Path: \ Components \

File: GameComponent without Game Dependency

file.png
Uploaded:
November.06.09
Modified:
November.06.09
File Size:
19 KB
Downloads:
724
Version
1.0

Two classes re-implementing XNA's GameComponent and DrawableGameComponent without being dependent on the Game class. This makes it possible to use your self-written components in applications not based on the XNA Game class, such as level editors and dedicated servers.

Details

While I'd recommend looking into using an Inversion of Control container instead of the GameComponent class provided by XNA, if you want to write components for other XNA developers, it might still be a good idea to just turn them into a GameComponent. It's easier to understand and you don't have to ship your favorite IoC container with your code.

There's one big shortcoming in the XNA GameComponent and DrawableGameComponent classes, however: they require an instance of the XNA Game class, for no particular reason. If you ever decide to create an XNA application that doesn't use the Game class (for example, a WinForms-based level editor), this will come down on you because, suddenly, you cannot use your GameComponents anymore missing the Game instance.

Luckily, the Game.Components collection doesn't depend on the actual GameComponent and DrawableGameComponent classes. All it needs is a class implementing the IUpdateable and optionally IDrawable interfaces. So you can implement these interfaces in any of your own classes and - surprise - add them into your Game's Components collection without any issues.

To avoid having to implement those interfaces over and over again, I just rolled my own two replacement classes, named Component and DrawableComponent. Yes, I left the 'Game' part out of the name to signify that this variant doesn't have a dependency on the Game class. As usual, all code comes with unit tests and has 100% test coverage ;)

Example

/// <summary>Drawable game component that displays an FPS counter</summary>
public class FpsComponent : DrawableComponent {

  /// <summary>Initializes a new FPS counter</summary>
  /// <param name="serviceProvider">
  ///   Game service provider containing the graphics device service
  /// </param>
  public FpsComponent(GameServiceContainer serviceContainer) :
    base(serviceContainer) { /* ... */ }

  /// <summary>Gives the game component a chance to initialize itself</summary>
  public override void Initialize() {
    base.Initialize();

    /* ... */
  }

  /// <summary>Called when the game component should draw itself</summary>
  /// <param name="gameTime">Provides a snapshot of timing values.</param>
  public override void Draw(GameTime gameTime) {
    /* ... */
  }

}

Features

  • Fully compatible to XNA's GameComponent and DrawableGameComponent classes
  • Do not depend on the Game class, there usable in WinForms applications
  • 100% unit test coverage for the whole code

These classes are part of the Nuclex.Game and Nuclex.Graphics libraries from the Nuclex Framework. You can find the most recent release of the code on the framework's CodePlex site:
http://nuclexframework.codeplex.com/



Joomla Template by Joomlashack