resource managers

Started by
10 comments, last by billybob 21 years, 8 months ago
i want to make a class where you ask it for a texture, mesh, sound, whatever, and it returns a pointer to it. however, if you''ve already loaded that file, it returns the same pointer as the original, so i don''t load anything twice, and once it works i can ignore whether i''ve already loaded it or not, just load the file again, and if its already loaded, it will give me the same pointer as the first time. but i can''t seem to figure this out without making some ridiculously huge array of textures, vertex buffers, meshes, sounds anything that can be asked for. can anyone give me some insight on how to do this?
Advertisement
Hi

Actually I''m doing something similar right now, although at the moment i''m only using it for textures. I want to apply it to model defs (you need to load model data only once. All the models of the same kind refer to the some ModelDef) and sounds

Anyway this is how i''ve organized it:

I have a class named CGameResource. It has a member method like LoadResource(const char *filename). It also has members like Path, Name, and an integer type ID. Each resource type class (eg CTexture, CModelDef, CSound) derives from this class.

In the CResourceManager class I have a sorted list of CGameResource objects. There is a method: LoadResourceLib (const char *filename). Basically you pass it the name of a custom text file which has the information about all the resource files in the particular game level you are loading. eg. each line of the file would have the information about the file path of the particular resource, it''s name and what type of resource it is.

So the LoadResourceLib() method goes through this text file, creating and adding the appropriate CGameResource objects to the sorted list.

Finally CResourceManager has this method: CGameResource *GetResource (const char *rs_name, unsigned int type). When some part of the engine calls this method, it searches through the sorted list with a matching name, checks that the types match and returns it''s pointer. The reciever of the pointer can the cast it to the appropriate child class.


Hope that helps
maybe i should explain better. i want to make a routine each, one for textures, meshes, sounds, etc. lets say i have the texture routine. i want to be able to ask it for the filename of a texture, and it will return a pointer. then lets say i ask for the same texture again, but this time it doesn''t load it, it just returns the same pointer as before. maybe that isn''t even possible.
sorry about that last reply, thats exactly what i need, i have gotten about 50 http 500 errors, so i actually made that reply a few hours ago
What I do is call int CTextureList::Insert(char* filename); then that loads the texture, inserts it into a linked list which holds it... its very simple really...
  // CTextureListclass CTextureList{public:	CTextureList();	~CTextureList();	void Insert(char *TexName);	bool TextureLoaded(char *TexName);	IDirect3DTexture8* GetTexture(char* TexName);private:	CTextureHead *myHead;};  


CEO Platoon Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
and the linked list holds the pointers? also, how do you tell if you''ve already loaded this texture?
Since there is tons of ways to do this, I thought I might as well tell you what has worked for me.

I have a manager for Textures, Vertex Buffers, etc.
All of my objects have IDs (like handles) to the resources, so the actual model never gets a direct pointer to the resources.

Each manager holds an STL map which has a key of a DWORD ( my unique ID issued out by another class) and value of a wrapper base class for that type of resource. When loading in a new Texture, for example, you can iterate through all or the resources, and compare the file name, which I keep in each texture wrapper.
When a resource needs to be used, the ID is easily used in the Map to find a pointer to my resource wrapper. The resource wrapper handles setting up the device to use it.

I was worried about the performance of Map at first, but it has not caused any more slowdown compared to other data structures, and greatly simplyfied my code and dependability. Using your own linked list or data structure is just as good.

for whats its worth I''ve just finished writing a system which seems to work ok (still testing it)
this system is based on templates, and what it basically gives you is a different resource manager for each resource type.
One for textures, one for sounds etc.
You say to it (I''m still working on the voice recognition )
"Texture manager, give me the texture "FILENAME.BMP"
it thinks to itself,
hmm "filename.bmp" doesn''t appear to be in my database, I''ll go load it.
if it loads it it gives me back a handle to it (so I can''t do anything wacky with the texture)
if not it gives me a null handle.
if I then say later
"Texture manager, give me that texture "filename.bmp" I''ve lost it"
it wont reaload the texture instead it''ll just give it to me.
I have to explicitly release the textures myself, but if I don''t the resource manager will do it when it is itself destroyed (and if I forget to do this, it should happen automatically as well).

if you want to have a look at the source I''d be happy to send it to you, just email.
(it also uses reference counting as well to make sure we don''t release a texture which is still being used)

Toby

Gobsmacked - by Toby Murray
Instead of rolling your own linked list you should look into one of the STL containers.

Unless you don''t know how to make a linked list, then you should make one for the experience.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
linked lists are the new/delete stuff right? so if you have a:

class Ball{public:   void bounce(x, y);   void Ball(char filename);private:   some bouncy functions}then later on you can just go:Ball * it;it = new Ball("filename.ball"); 

thats the way i understand them anyway. so if you applied this to a resource managager, you could make a resource class, which holds the texture, an ID, and the filename. when it loads a resource, it would look at all the linked lists and see if it had already loaded that filename. if it had, just give it the same pointer as before. if not, load it and make a new one?

This topic is closed to new replies.

Advertisement