Things to note when Writing XNA components

From over in Mitch Walkers Blog, he notes back in September some important facts about XNA component writing and things to take into consideration.

If you’ve not read his blog I would recommend doing so.

 As an extract though it all centers around the availability of the graphics device while the component is being called, in pretty much the same way you must do in your main app, just have to remember that components are called separately to your main app and the state of the graphics device can change (I.e window resizes), this means any resources the component is using need to be re-initilised as they would no long be valid on the graphics device.

A load of words really but put simply it’s good practice to write your components so they are as portable as possible and not just rely on your implementation handling it.

Example below of code that should be added:

 public override void Start()
{
    this.graphics = this.Game.GameServices.GetService<IGraphicsDeviceService>();
   
    this.graphics.DeviceReset += new EventHandler(graphics_DeviceReset);
    this.graphics.DeviceResetting += new EventHandler(graphics_DeviceResetting);
    this.graphics.DeviceCreated += new EventHandler(graphics_DeviceCreated);
    this.graphics.DeviceDisposing += new EventHandler(graphics_DeviceDisposing);

    LoadContent();
}

%d bloggers like this: