IcySnow
IcySnow

Reputation: 871

Difference between Game1.GraphicsDevice and graphics.GraphicsDevice

In XNA 4.0, what's the difference between the GraphicsDevice property of my game class and the GraphicsDevice property of the "graphics" variable? BTW, "graphics" is of type GraphicsDeviceManager.

I'm using Visual Studio 2010 if that matters.

Upvotes: 2

Views: 1110

Answers (2)

Andrew Russell
Andrew Russell

Reputation: 27215

Nothing. Try it yourself:

Console.WriteLine(ReferenceEquals(graphics.GraphicsDevice, this.GraphicsDevice));

Put that somewhere like inside LoadContent. You will find that it outputs true. They both refer to the same object.

For the sake of readability, you should generally use Game.GraphicsDevice.


The underlying behaviour is that GraphicsDeviceManager is an IGraphicsDeviceService (which provides a GraphicsDevice member). When you create GraphicsDeviceManager, it adds itself to Game.Services in its constructor (see how you pass a reference to your Game into its constructor).

Game sets its GraphicsDevice member from any IGraphicsDeviceService in its Services list. That way, you can replace GraphicsDeviceManager with your own class that implements IGraphicsDeviceService - if you happen to be crazy ;)

GraphicsDeviceManager is also a IGraphicsDeviceManager, which Game uses in a similar way (through Game.Services) to manage creating the graphics device.

Upvotes: 4

fex
fex

Reputation: 3558

GraphicsDeviceManager manage GraphicsDevice and you shouldn't use GraphicsDevice directly. It could be changed so instead of it just use GraphicsDeviceManager.GraphicsDevice property if you need.

Upvotes: 0

Related Questions