I’m cleaning up a sandbox into proper Classes, I’ve been a bit lazy and it’s time to do some housekeeping.
And I’m sat here wondering; is it better to use only one Sprite Batch and pass it to all other classes for drawing, or is it better (or at least no worse) to creating a new batch in each class.
Common sense is telling me passing the Batch to all classes that need to draw is going to be a lot better, but my common sense has been known to let me down from time to time!
Here’s how I use it, it might not be the best way but it works https://pastebin.com/Apw6x0nN
I remember in xna it was much faster to pass the spritebatch as calling begin and end takes a performance hit. I assume its the same in monogames
You may want to consider using a global static declaration for your sprite-batch if it is not going to be updated constantly.
Passing heavy objects around classes can become rather inefficient the more it is done…
The advice is to have one SpriteBatch instance, and minimise the number of Begin/End calls. This reduces memory usage and improves rendering performance.
How you work your game code around that generic advice depends on what you’re doing.
How you get the SpriteBatch reference to your rendering code is also up to you You can pass it as a parameter, keep a global static or singleton, add it as a Service and get references that way etc. Whatever. I tend to use the Service method FWIW.