Jump to content
  • Advertisement
Sign in to follow this  
watermelonChris

XNA: Adding SpriteBatch as service to class

This topic is 2532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:

Services.AddService(typeof(SpriteBatch), spriteBatch);


And then in my 'home brewed' class I added:

SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;


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
Advertisement
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.



public class Widget
{
SpriteBatch spriteBatch;

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

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

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.



public class Widget
{
SpriteBatch spriteBatch;

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

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



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 GraphicsDeviceManager, which is a type of IGraphicsDeviceService 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

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 GraphicsDeviceManager, which is a type of IGraphicsDeviceService 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.


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!