Sign in to follow this  
watermelonChris

XNA: Adding SpriteBatch as service to class

Recommended Posts

Hey guys, I've made a class (not a game component).

And I'm trying to use Sprite Batch as a service... So I added this to my Game's LoadContent method:
[code]
Services.AddService(typeof(SpriteBatch), spriteBatch);
[/code]

And then in my 'home brewed' class I added:
[code]
SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
[/code]

But visual studio gives me this error: Error 1 An object reference is required for the non-static field, method, or property 'Microsoft.Xna.Framework.Game.Services.get'

Any ideas? I've tried changing it to Game1.Services.Get...... But that doesn't work either so...

Share this post


Link to post
Share on other sites
Any reason why you need to add it as a service? Just pass a SpriteBatch in the contructor and you can use it in the class's draw code.


[code]
public class Widget
{
SpriteBatch spriteBatch;

public Widget(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
}

public void Draw()
{
spriteBatch.Begin()
/// draw stuff...
}
}[/code]

Share this post


Link to post
Share on other sites
[quote name='EJH' timestamp='1313083348' post='4847777']
Any reason why you need to add it as a service? Just pass a SpriteBatch in the contructor and you can use it in the class's draw code.


[code]
public class Widget
{
SpriteBatch spriteBatch;

public Widget(SpriteBatch spriteBatch)
{
this.spriteBatch = spriteBatch;
}

public void Draw()
{
spriteBatch.Begin()
/// draw stuff...
}
}[/code]
[/quote]

Thank you !!

Share this post


Link to post
Share on other sites
Also another thing - you're not using the service pattern right, since SpriteBatch is not a "service" per say.

You may have a "SpriteBatchManager" of type "ISpriteBatchService" that manages a SpriteBatch and exposes it to clients (like XNA's [url="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphicsdevicemanager.aspx"]GraphicsDeviceManager[/url], which is a type of [url="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.igraphicsdeviceservice_members.aspx"]IGraphicsDeviceService[/url] and exposes a GraphicsDevice). So the interface is what you're querying from the service locator (Game.Services), not the implementation and definitely not the content of the service (e.g. a SpriteBatch).

Services are a means of decoupling the concrete implementation of a service/manager object from clients, because a service is added and queried to the service locator by its interface. Client code only is aware of the interface; it doesn't know nor need to know the actual implementation, allowing the service to vary in its implementation without requiring client code to be changed. So having a sprite batch as a service diminishes the point of the pattern, since you're working with a concrete object whose implementation likely won't vary/can't vary nor has its accessed managed (since you're working with it directly), making constructor injection like EJH said the more attractive solution.

Share this post


Link to post
Share on other sites
[quote name='Starnick' timestamp='1313161578' post='4848245']
Also another thing - you're not using the service pattern right, since SpriteBatch is not a "service" per say.

You may have a "SpriteBatchManager" of type "ISpriteBatchService" that manages a SpriteBatch and exposes it to clients (like XNA's [url="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphicsdevicemanager.aspx"]GraphicsDeviceManager[/url], which is a type of [url="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.igraphicsdeviceservice_members.aspx"]IGraphicsDeviceService[/url] and exposes a GraphicsDevice). So the interface is what you're querying from the service locator (Game.Services), not the implementation and definitely not the content of the service (e.g. a SpriteBatch).

Services are a means of decoupling the concrete implementation of a service/manager object from clients, because a service is added and queried to the service locator by its interface. Client code only is aware of the interface; it doesn't know nor need to know the actual implementation, allowing the service to vary in its implementation without requiring client code to be changed. So having a sprite batch as a service diminishes the point of the pattern, since you're working with a concrete object whose implementation likely won't vary/can't vary nor has its accessed managed (since you're working with it directly), making constructor injection like EJH said the more attractive solution.
[/quote]

Wow thank you, I didn't quite get all of what your saying though, guessing I should read up on C# OOP?

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