Sign in to follow this  
maldakkak

XNA Game Service question

Recommended Posts

Hi I was trying this code from a book: in one file I had the following code : GraphicsDeviceManager graphics=(GraphicsDeviceManager)Game.Service.GetService(Typeof(GraphicsDeviceManager); but for this to work then graphics must be added to Services somewhere else in another file: Services.Add(Typeof(GraphicsDeviceManager),graphics); In another book I found that you really don't need to add graphics to the GameServices if you replace the first line of code with the following: GraphicsDeviceManager graphics=(GraphicsDeviceManager)Game.Service.GetService(Typeof(IGraphicsDeviceManager); So my question is, how does this work? What is the difference between GraphicsDeviceManager and IGraphicsDeviceManager? I hope my question was clear. Thanks

Share this post


Link to post
Share on other sites
As with all C# coding, I as a prefix generally always represents that it is an interface or an inheritable foundation object. So GraphicsDeviceManager inherits from IGraphicsDeviceManager. Basically IGraphicsDeviceManager is there to allow you to create your own custom GraphicsDeviceManager object if you so choose. Therefore, sometimes a function will ask for an IGraphicsDeviceManager instead of GraphicsDeviceManager in case you have built your own GraphicsDeviceManager.

Remember, google is your friend :)

IGraphicsDeviceManager

GraphicsDeviceManager

Share this post


Link to post
Share on other sites
Zach covered the difference between the two. As for why you don't manually add that service, it's because the default GraphicsDeviceManager constructor (which your game invokes in its constructor) adds itself to the game's Services collection under the IGraphicsDeviceManager type. By it doing this for you, your game automatically has that service to leverage without you having to do anything.

Generally speaking services are added and retrieved based on interfaces. You define the interface that exposes just the functionality you need from a particular object that way you are free to have components rely on IGraphicsDeviceManager or IInputManager or whatever and have the freedom to change what exact class you use to implement the service from game to game. It gives you an easier time when you try making a new game using components from a previous game.

Share this post


Link to post
Share on other sites
Thanks for the answers.

I did not know that the default GraphicsDeviceManager constructor adds itself to the game's Services collection, and that is what I was looking for.

Thank you again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this