I'm learning directx in a master's degree and they taught us to have a GraphicsLayer class which is the one connecting with the direct3d library. That way this class is completly independent from the other classes (my game classes), meaning changing the renderer to OpenGL wouldn't require much effort but only changing the graphicLayer.

This classe has it's LoadAssets, Paint methods, but I have a question, they told us to load all the assets inside this class. This means all these methods will be in the loadAssets method:

D3DXCreateTextureFromFileEx(g_pD3DDevice,"tiles.png",0,0,1,0,D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,NULL,NULL,NULL,&texTiles);
// And more resources to load
//...

texTiles as you see is a LPDIRECT3DTEXTURE9 instance which is declared in the graphicLayer.h.

So my question is, how do you manage all the resources? Do I have to declare in the .h all my game textures even if I'm not using them? How would you load only those resources there are in a scene and draw them in a code-strucured way?

1 Answer
1

The interesting thing about programming is that there is an infinite number of ways to do things. Give a problem to 10 programmers, and you get 10 completely different solutions. Just because you are told to do it that way, doesn't mean that it's the only way to do it.

Hardcoding the filenames of assets in your code is usually a bad design, because it makes the renderer a lot less flexible and prevents the separation of game engine and game content (imagine you are working with a graphic artist who has to call you whenever they want to add a new texture).

Usually, when you load a map in a game, you have that map in some kind of file. So you pass that file to your graphic engine to load it. This file usually contains a list of asset files which are used in that map. So the graphic engine will load these assets when it parses the map file.

To safe memory and reduce initial loading times, an engine could also load certain assets on demand. An example would be an enemy which is spawned on the map while playing. The engine could wait with loading the geometry and texture of that enemy until it is spawned, and free that memory when the enemy is removed.

In one game project I took part in, we managed all assets through a ResourceManager class. This class would cache all loaded resources. So when you need the texture xyz anywhere, you call ResourceManager.getTexture("xyz.png"). When it's the first call, it loads that texture from disk. At subsequent calls, it returns a cached copy. The resource manager automatically frees any cached assets which weren't requested for over a minute, except those which have a "never drop" flag set (for assets which are used infrequently, but when they are used they are time-critical).

I didn't mean hardcoding, I meant having to do D3DXCreateTextureFromFileExonly in GraphicLayer. I would personally create my object classes and instance there the LPDIRECT3DTEXTURE9. But that way if you wanted to change the renderer you'd have to change pretty much every class. That's why I'm asking, the standard way of structuring.
–
marcg11Nov 21 '12 at 12:46