views:

44

answers:

1

I'm currently working on a couple of windows phone projects (Although the question may also fit iphone/android) and it got me thinking, when is the best time to load textures from the content manager.

At first, I was loading them all up, from my Game base class, and passing them along as required. Quickly getting sick of this, I have created a small resource manager class, which I pass out to anything that requires it.

So I'm thinking, that perhaps its best that I actually load the texture in, when a class requires it, and then assign it to a variable, so when I need it again - it will be all ready to go... is this the best way (efficient?, fastest?) to handle loading resources? If not, how would you recommend I go about it?

+5  A: 

Don't create any kind of "resource manager" class. Just pass the XNA ContentManager class around (the instance you get from Game.Content).

The default content manager will automatically handle reusing loaded objects for you. So you can Content.Load<Texture2D>("something") from multiple places and you will always get back the same texture instance.

So if you have a bunch of classes for your game objects, with the standard design of giving each an Update and a Draw method that you call from the respective methods in Game - just add another method: LoadContent to those classes, that accepts an argument of ContentManager.

You can call that method from your game's LoadContent method.

If, later, you want to implement some kind of system of delay-loading things (for example: loading content when changing levels), you can also call your game classes' LoadContent method from your game's Update method (but keep in mind that loading content is slow - so you might want to throw up a "loading" screen).

Unloading content is slightly trickier. Anything you create yourself you must unload. But anything loaded from a ContentManager (because the instances are shared) should only be unloaded by that content manager (Game will handle unloading its Content member when it needs to). You may find this blog post worth reading.

Andrew Russell
Would passing the contentManager class around be better than calling GameState.Instance.LoadTexture2D("Texture") ?
Tim
Am I right in thinking GameState is a singleton? It's not *terrible*, but I've found that passing a ContentManager around is more flexible in the long term. Also I wouldn't even bother with the singleton - if you want to go that route - just make it `public static` and manage initialising it in your Game class. (This is my game development answer - the proper software engineering answer is simply: "Yes!")
Andrew Russell