Jump to content
  • Advertisement
Sign in to follow this  
swinchen

What is a handle? In reference to Resource Managment

This topic is 4828 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

I have been hitting google looking for information on handles and it tends to return a lot a crap about the windows 32 API. So my question is... what is a handle and how is it typically used in resource management?

Share this post


Link to post
Share on other sites
Advertisement
It depends on how your resource management system works.

For instance, if your resource management system manages 3D models. In this case, your 3D model consists of a vertexbuffer, indexbuffer, textures, skeletal animation info.

These bits of information are wrapped up into a nice little class called Model. When you require the model of the shotgun, you basicly call ResourceManager.Get("shotgun"). This will return a handle(Or pointer) to the model object that contains all the information about the shotgun.

A handle is thus mostly a variable that will point to a specific object.

Toolmaker

Share this post


Link to post
Share on other sites
A handle is basically a glorified pointer. They are commonly used to add an extra layer of indirection to the actual resource they are pointing to. In resource management, they can be used as a pointer to data that may or may not be valid, allowing the resources to be loaded/unloaded. Also, handles are also commonly used in games in place of raw pointers to game objects, because with some minimal effort a system can be made that ensures the validity of a handle over a network. HL2 uses this extensively.

Share this post


Link to post
Share on other sites
Instead of objects having a pointer to a resource, you can give
them a 'handle'.

A handle is nothing more then a number, with which you can request
a pointer to the resource. If you need the resource you'll ask the
resourceManager to give you a pointer to the resource for the
specified id/handle.


A _very_ simple resourceManager might look like this:


typedef int ResHandle;

class ResMgr
{
vector<Resource*> resources;

ResHandle Register (const String& resname)
{ // add to resources and return index
}

void Unregister (ResHandle id)
{ // mark index as invalid
}

Resource* getResource (ResHandle handle);
{ return resources[handle]; // check for valid index!
}
};


Concerning savegames, having handles (instead of pointers
to memory locations) makes things easier.

/R

Share this post


Link to post
Share on other sites
Ok, so lets say I have a handle to a texture, how would I bind that texture?

TextureHandle Smile = ResourceMgr.Get("smile.tga");

ResourceMgr.BindTexture(Smile);

?

That seems to imply that I would need a different manager for each resource. Maybe through specialization I could get around that bit...

Share this post


Link to post
Share on other sites
The other problem that I have is .. if A resource ID is a integer (or a size_t... whatever) and I am storing my resources in an std::map by filename then I would need to have an ID->filename lookup. :/ This is really confusing.

Share this post


Link to post
Share on other sites
Quote:
The other problem that I have is .. if A resource ID is a integer (or a size_t... whatever) and I am storing my resources in an std::map by filename then I would need to have an ID->filename lookup. :/ This is really confusing.


You could use an ID -> Filename lookup, but why not just change your std::map to use ID->Resource and if you need the filename, keep that in the resource structure.

Quote:
That seems to imply that I would need a different manager for each resource. Maybe through specialization I could get around that bit...


I think you'll need a manager for each type of resource. This would make sense since each resource will be handled in different ways.

Share this post


Link to post
Share on other sites
Quote:
Original post by perfectly_dark
You could use an ID -> Filename lookup, but why not just change your std::map to use ID->Resource and if you need the filename, keep that in the resource structure.


Every time "something" is requesting a resource it needs to query it by filename. So I figured that using the filename as the key would be a good idea. Otherwise I am not sure how to search my map for the filename.

Quote:
Original post by perfectly_dark
I think you'll need a manager for each type of resource. This would make sense since each resource will be handled in different ways.



Yeah, that might be the easiest way. I was looking at Element 61 ( Resource Management System part 1 and 2 ) It looks like his Manager class is a template class. Maybe there is where I am getting confused.

Share this post


Link to post
Share on other sites
Quote:
Every time "something" is requesting a resource it needs to query it by filename. So I figured that using the filename as the key would be a good idea. Otherwise I am not sure how to search my map for the filename.


What I do in my projects is load the resource by filename and store the handle. Then any operations I need the resource for I pass the handle. While it makes sense to use a string as the key for your map, if I'm not mistaken, lookups with a string will be less efficient than using an ID/handle. You would also be able to store your resources in a vector where the ID/handle is the index into the vector. If you need to get the handle for a filename, maybe you could make a method in your manager that takes a string as an argument, searches the resources vector, and returns the proper handle.

However, the performance hit shouldn't be that great and if it makes your life alot easier you could likely get away using a string->resource map.

Share this post


Link to post
Share on other sites
Yeah, I would need a way to search by file name... if polygon A knows that it needs texture "smile.tga" it will request "smile.tga" and get a handle back. If polygon B also needs texture "smile.tga" it has no idea what handle that is... only the file name.

I think that makes sense.


Also.. you store the handle? That could work. I was storing the resource, and everytime a request for a texture was made I created a handle around the texture.

Right now, basically I have CTexture, which has a counter (inherits from CCountable). I am still working on the details of what goes in the handle. I am trying to figure out if I want a pointer to the resource (which could make my life a lot easier) or an ID/string name...

I know I am making this harder than it really is. I think I am trying to make things to generic.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!