I’m going to start a new project with Monogame with Monogame.Extended extension and I have a question about the good way to retrieve the SpriteBatch.
I’m currently experimenting the Screen class, and looking to the Demo.Screens project of the Monogame.Extended solution, I saw that the instance of the Game’s GameServiceContainer (the service provider) is given at the instantiation of each screens. With the GameServiceContainer, it’s possible to create a new SpriteBatch object, it’s done like this in the MenuScreen class (all screen classes of this example inherit from this MenuScreen class):
var graphicsDeviceService = (IGraphicsDeviceService)_serviceProvider.GetService(typeof(IGraphicsDeviceService)); _spriteBatch = new SpriteBatch(graphicsDeviceService.GraphicsDevice);
Does it have any sense to create a screen without SpriteBatch? Is there a reason why this code is not in the Screen class directly?
But do we really want to have a new SpriteBatch for every screen? Why don’t we use the instance of SpriteBatch that already exists in our Game object? Is this really a problem to make this SpriteBatch public and to give the Game object to each screen?
I would really appreciate to have your opinion on this.
Thank you in advance.