mammoo

Hi everyone, I'm just starting with XNA GSE and working on my first game. One thing that is puzzling me though is the LoadGraphicsContent. It seems in all the tutorials I've seen that everyone loads every single graphics resource inside this one function.

This seems wrong to me for two reasons, the first is that surely this isn't the most memory friendly way of doing things. What if I have ten levels on a game all with different graphics, I only need one levels worth of graphics to be loaded at any one time so I don't want to hold the other nine levels graphics in memory. The second reason is that if there are a lot of graphics resources this function is going to get very large, I would have thought it would be better to load the content inside each class as and when it is needed i.e. my "paddle" class loads the resources needed for the paddle etc.

I have the same issue with the draw and update methods. Surely it would be better to have a draw and update method inside each class and call these from the main draw and update methods to keep all the code as neat and organised as possible.

What do you guys think


Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Bill Reiss

mammoo wrote:
Hi everyone, I'm just starting with XNA GSE and working on my first game. One thing that is puzzling me though is the LoadGraphicsContent. It seems in all the tutorials I've seen that everyone loads every single graphics resource inside this one function.

This seems wrong to me for two reasons, the first is that surely this isn't the most memory friendly way of doing things. What if I have ten levels on a game all with different graphics, I only need one levels worth of graphics to be loaded at any one time so I don't want to hold the other nine levels graphics in memory. The second reason is that if there are a lot of graphics resources this function is going to get very large, I would have thought it would be better to load the content inside each class as and when it is needed i.e. my "paddle" class loads the resources needed for the paddle etc.

I have the same issue with the draw and update methods. Surely it would be better to have a draw and update method inside each class and call these from the main draw and update methods to keep all the code as neat and organised as possible.

What do you guys think

I personally like to give each of my classes related to a game object its own draw and update methods, seems a lot cleaner to me too. As for loading new content for each level, when I have seen this discussed in the past, I think the recommendation if you are using the ContentManager to manage your data is to unload all of your resources and then load the new ones, there didn't seem to be a way to selectively unload resources with the ContentManager. Of course you could load managed resources for your game-wise resources, and then manually load (without the ContentManager) your level-specific resources.

Bill






Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Darkside

Yup I agree with Bill, I usually have different component and classes for such things.

Alternatively you could have 2 content managers, one for main stuff and one for levels, then have a switch case when loading the level, clear out the content manager with an Clear call and then load the specifics for the level.

A possibility.

Darkside






Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Bill Reiss

That's a good idea! Can you have two ContentManagers




Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

mammoo

Hi Bill, thanks for the response. Would you be able to give me a skeleton example of how you implement draw and update methods in each of your classes I'm struggling to get everything needed i.e. spriteBatch, content.load to work inside my classes because all the declarations are in the main Game1.cs. I'm thinking it would be nice to pass these as a reference when calling the classes update/draw methods but I'm not sure how to do it.

Thanks




Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Jim Perry

Bill Reiss wrote:
Can you have two ContentManagers

You sure can.






Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Darkside

I thought you could since it'sjust another variale set to a class.

Jim you beat me too it while I was digging out my docs, usual makes sense but not tried it route and it onlyjust occured to me, lol

Darkside






Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Bill Reiss

Yes that's one way, you can pass the SpriteBatch into the Draw method, and if you wanted to have a LoadGraphicsContent method in your class you can pass in the ContentManager. Since these are objects and not value types, they are passed by reference by default. One thing to note on the ContentManager.Load() method is that after the first Load of a particular asset, subsequent loads of the same asset just return a reference to the already cached object, so you don't have extra loading overhead, so it's OK to call Load multiple times in your classes.

Another way would be to have a static class which holds the SpriteBatch and ContentManager objects for the application and then you could access them from each of your classes.

It's really a matter of preference, it may be a bit faster the second way, but it may be a bit harder to follow the code. If you want an example of some of the first style, you can download a sample project here: http://www.bluerosegames.com/brg/XnaTutorial15.zip 

Or you can follow through my tutorials at http://www.xna101.net to build up step by step to this example project if you prefer.






Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Jon Watte

Note that, while you can have a number of ContentManagers, there is some overhead for each. For example, each of them seems to have its own array of type demarshallers. Thus, keeping one for global assets, one for UI assets, and one for level assets would be fine. Keeping one per asset you load would not be ideal.






Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Darkside

I would most definatley agree, having more than two content managers is too much.

Although trying to find an application for more than two would be a chalenge, one for Game and one for level is the most anyone should implement.

Darkside






Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Jon Watte

The problem is that you can't unload a loaded resource without unloading all of the resources in the content manager. Thus, if you want to support a game with a large, streaming world, you would have to create one resource manager per "area" you can load/unload.

Worse, if you have a game where the user can change appearance/model/clothing during the gameplay, you'd have to have a content manager per "changeable part" to be able to unload the parts.

Once you try to create a AAA, 9 GB game on XNA, or a networked/multiplayer game, this becomes a real problem, and you'd probably end up re-writing the ContentManager on your own to support partial unload.







Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

BerW

Hi,

OK sounds good to use individual content manager for each game level and destroy resources at the end of the level.
What about loading the resources, e.g. LoadGraphicsContent() LoadGraphicsContent() normally is called from the framework,
can I also call this function from my code How di I avoid the situation where resources are loded twice (from the framework and from my code)

To clarify a little bit here my ideas:
- a base level class with its own content manager
- a lot of derived level classes implementing the level
- a level manager class, which handles level switching including calling of LoadGraphicsContent() for the new level

So I got the situation that for the first level LoadGraphicsContent() is called twice (from LevelManager and XNA framework). Is there any way
to check this situation
How do I handle level transitions without graphic glitches The prev. level resources will be unloaded and new resources are loaded, which takes time Any ideas

Thx for help

BerW





Re: XNA Game Studio Express LoadGraphicsContent - Is it right to load ALL graphics content at the start

Darkside

That should be fairly easy.

If your properly employing gamestates and have a Level Loading state in your main game, then you ca be clearing and loading up the next level content manager in the background.

Some nice eye candy for the user or even the stats from the previous level.

Always sounds fun

Darkside