Resource Management... Howto?

Samuel Winchenbach swinchen at
Fri Jul 6 13:03:15 PDT 2007

Hi there.

Lets assume I have a number of resources (audio, textures, fonts, etc) 
that I am trying to keep track of.   I want a number of things to happen:

1.) When something tries to load a resource that isn't already loaded 
the resource will be loaded.  The resource will be stored in an 
associative data structure so resources will be easy to find. 
"Something" will be handed back to the requesting code... maybe an int 
handle, or an object...  I don't know yet.

2.) If an resource is already loaded then it will be found in the 
associative data structure and "something" will be handed back to the 
calling code.

3.) The resource will automatically be freed (calling any appropriate GL 
call to free GPU resources) when code is no longer referencing the 

I have been thinking about this a lot, and I might have an idea how to 
do this..

I could have something like:
auto h = texture.loadTexture(...);
... use the texture ...

and texture keeps a reference count and when it drops to zero it is 
removed from the data structure and appropriate actions are taken to 
release any system resources.

What I would like is to not have a texture.releaseTexture().  I would 
like that to be automatic.  Perhaps "h" could be an object with a 
destructor that automatically calls "releaseTexture()"?   Not sure of 
the structure of this....

and what would be best is to have one resource manager that keeps track 
of everything loaded...

auto h = resource.loadResource();

but this gets tricky because how do you know what type of resource you 
are loading, and how do you set the parameters of the resource.

If anyone has any comments, questions, suggestions, sample code, etc. it 
would be greatly appreciated.


More information about the Digitalmars-d-learn mailing list