Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

leiavoia

Resource Management Strategies

This topic is 5293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There are two ways to go about managing resources: via a manager or by distributing it so that all objects manage their own resources. i would like to know what everyone else does and why, then on to how. Right now, i have it set so that all objects manage themselves. When i need an explosion, the explosion loads up the first explosion animation frames into a static class member so that all explosions can no use them from that point on. So it goes with all my resources: backgrounds, tilesets, animation frames, etc, all managed by different object responsible for drawing those graphics. However i want to look into setting up a resource manager because i''m eventually going to run into problems with sharing graphics / sound FX between several different objects. But i''m worried that the extra go-and-fetch routines are going to be a lot of overhead for something i don''t want to skimp on: graphics. So now, let''s say i have a manager class. Objects that need resources basically need a way to say "i need a graphic like this" and the manager to be able to say "here you go". it would either pass back a pointer or an identifier of some sort. The part i''m looking to solve is basically how does a resource manager manage? Does it keep an associative array, or a list indexed by an enum, or what? I''m not sure if speed is an issue since most objects would only make requests for the resources they need before they get off the ground, not every frame. The system i have in mind is that i load graphics based on config files. The config files tell the manager what files to load and what we are going to call those graphics (either by a string name or a number/index/enum/whatever). Then objects make requests for, say, GRAPHIC_EXPLOSION_FRAME_ONE, and the manager returns a pointer to the graphic which the object will then use until it dies. Right now, i''m leaning towards filing all my resources based on unique keys in an assocative array, then allowing objects to do something like: Graphic* graphic = manager->GimmeGraphic( "name_of_graphic" ); Any thoughts? How would/do you do it?

Share this post


Link to post
Share on other sites
Advertisement
Use a manager, store a map of your resources and handles to it. I think this would be trivial to implement. But with a manager, u can ensure that all of your resources are unloaded at exit.

To go one step further, make a generic Resource and ResourceHandler class.

Share this post


Link to post
Share on other sites
Here is another question:

Should objects make requests for resources every frame or just once at the beginning? If they do it once at the beginning, and for whatever reason, that resource ceases to exist, i''ve got possible segfaults.

The alternative is to reference all resources by a handle like already mentioned, then request the resource via it''s handle: every frame :-( Would this cause any slowdown if using something like an std::map ?

Share this post


Link to post
Share on other sites
I think you're overlooking something: reference counting combined with handles. That way, an object can request a resource once, during creation / when it's needed, and you don't worry about resources disappearing while objects still have references to them. And with handles (or a handle wrapper class, if you will), something like a resource becoming unavailable is encapsulated in the resource manager part of the engine, so the object doesn't have to worry about it.

My resource manager is basically loading raw unprocessed data from the hard drive or an archive file, then passing it to a parsing object to turn it into a texture, or a sound file. The file manager keeps an associative array (std::map< std::string, FileHandle > ). Don't worry about accessing by handle; iterating through an std::map is pretty fast, and remember: when in doubt, profile!

edit: Damn smileys. And damn my tendency to end sentences with semicolons!

[edited by - psykr on May 22, 2004 2:58:42 PM]

Share this post


Link to post
Share on other sites
I would say std::map will be fine for now but you might want to look at STL hashmap to speed up the search of a resource, its not offically in the standard just yet but i think SGI and STLPort had an implementation for one on there website, i would also say i don''t think you need a handle type at all, dish out reference and constant reference types to refer to the elements of the map for sharing because it don''t sound like you gonna be deleting any of the elements once there loaded. An example of what i mean is:


std::map< std::string, image > imgs;
// ... loads some images from a file, maybe eleminate duplicates


//Some where within the program, get a constant reference from the map for a reading ops

const image& my_img = imgs["some_name"];

//... do something with img



I would do what you where saying, have a configuration file say in XML that describes all resources your app needs, have some kind of builder type the parses the config file, loads the resources of a particular type and feeds them into a resource manager type that maintains a map or hash map (preferable) at initialization time, other types inquire a resource from the reource manager type that returns a copy of the address to one of the elements of its map using a reference or constant reference.

I see a resource manager as just a functional object, i would make it functor something like this:


#include <string>
#include <map>
#include <functional>

template<class resource_type>
class resource_mgr : public std::unary_function< const std::string&, resource_type& > {
public:
resource_type& operator()(const std::string& name);
const resource_type& operator() (const std::string& name) const;
private:
std::map< std::string, resource_type> _resources;
};


i would add a thew constructors to allow various methods of initializing the map. Other things you could also have is if some resource hasn''t been loaded initially from the config file and an object requests for it then you can add that new resource once from that point on from a file and re-use it.

Share this post


Link to post
Share on other sites
Okay so basically it's like a check-in process. Objects say "I need graphic SOME_GRAPHIC, so don't you dare delete it while i'm trying to use!"

That makes sense. If that's true i only need to load graphics and such once for the class, not look them up every frame.

My original idea was simply to have the "World Space" be responsible for the graphics. That way it can look up all required graphics before you start playing through that particular area. That way objects don't have to load resources when they are created for the first time and don't have to say "Wait, hold on... [ retrieved graphic files, makes textures ] ... okay i'm back. Sorry for dogging your framerate. Let's rock." If the world is responsible for the graphics, i can load all graphics required for that area, then dump them all when i leave the area and go some other place.



[edited by - leiavoia on May 22, 2004 5:43:39 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!