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:

// 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?


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).

  • \$\begingroup\$ 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. \$\endgroup\$
    – marcg11
    Nov 21 '12 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.