Resource management...again
I've been recoding my entire game engine to deal with resources in a much better fashion.
However I was wondering how some of you are approaching the problem.
Here are the resources that I'm handling right now:
- Textures
- Materials
- Lights
- Shaders
- Meshes
These are basic resources. But now I also have a class called CShip which is a spaceship in my game. But you can see a problem here.
Each ship needs a mesh, materials, textures, and a shader. These are all simply DWORDs or an ID_ARRAY like this:
Code:
struct ID_ARRAY
{
DWORD *pArray;
DWORD dwNumItems;
};
The basic idea is I have managers for all those resource listed. They control the creation and destruction of the resources...in a roundabout way. Since meshes need nearly all of the resources to work correctly I decided to add meshes via a mesh manager which then also has pointers to the other various managers. Each one of the managers is then told to add materials, textures, etc from my X files. These add functions return a DWORD indicating the ID for that added object. This value is then stored in the mesh class by the manager class (the manager is a friend of the mesh class). Now each mesh has the needed information. In my rendering system each manager is responsible for rendering it's own objects and building the final render list utilizing frustum culling and so forth.
This seems very odd to me b/c meshes require other resources and it seems they should be a part of the mesh rather than sep from it. But this introduces a whole plethora of problems in itself.
How are you guys solving these issues?
In short my system is:
1. All objects/resources are managed by container classes
2. All objects/resources are created via the container class when applicable
3. Objects/Resources cannot be accessed without using the container class.
4. The object container class updates the objects, builds the final render list from the master list, and renders the objects which includes setting textures, vertex/pixel shaders, etc, etc.
So to illustrate let's say you want to load a ship model called fighter.x
Code:
//Create shader mgr
m_pShaderMgr=new CShaderMgr();
//Init with device
m_pShaderMgr->Create(m_pDevice);
//Add shaders and assign IDs - so we can easily access later
m_pShaderMgr->AddShader("AmbDifSpec.fx",BASE_SHADER);
m_pShaderMgr->AddShader("Bloom.fx",BLOOM_SHADER);
m_pShaderMgr->AddShader("Blend.fx",BLEND_SHADER);
m_pShaderMgr->AddShader("Atmosphere.fx",ATMOSPHERE_SHADER);
m_pShipMgr=new CShipMgr();
m_pShipMgr->Create(m_pDevice);
//Create the ship object and add the ship ID to the BASE_SHADER list
m_pShaderMgr->AddObject(m_pShipMgr->AddShipFromX("fighter.x",0),BASE_SHADER);
The ship container class would then open the x file, convert the Direct3D materials to CMaterial objects and add them to the material container and store the ID's in the mesh class, add any textures in the file to the texture container and add them to the mesh class, etc and return the ID of the ship.
In the final render objects with similar shaders are grouped together and drawn in batches and so on.
This has taken me a very long time to code and I'm still not sure if I like it or not.
In my system a CGameObject is a renderable object and a CResource is a non-renderable engine resource such as a texture, material, etc. (even though you can render a texture it's still not a mesh or an actual game object).
Comments? I'd be interested in how you are managing all of this Bob. I've got a headache from figuring this out on paper and then attempting to code it. Just for a release of tension I must say Resources suck