Sign in to follow this  
jacktang

resource manager, duplicate texture loading

Recommended Posts

Hey, I'm writing a resource manager for a 2D game engine, and I'm working on texture loading at the moment. The way I'm doing it, is I'm passing in a filename to my loadTexture function, and then I create the texture, then store a handle to that texture in a hashmap using the filename as the key. That way I can look in the hash map first to see if i've already loaded that resource and if I have then I can just return a handle to it. My question is, is there a better way of going about doing this? I don't like that I have to store all of the filenames in memory. Is there some "efficient" way of compressing the filenames down to an integer somehow so that I can still check to see if they've already been loaded, but I can just store an id for the filename in memory? I realise I could create a map of textures to identifiers and this may be the way I'll end up going, but I was just wondering if there was a slightly more dynamic approach to the problem that requires a little less manual maintenance. Thanks Edit: Forgot to mention that I'm using C++ for this. Thanks.

Share this post


Link to post
Share on other sites
If you're using a hash map, that's already being done for you. The string - your key - is being converted into (and being stored as) a hash which is used to look up the value.

But really, I really doubt that it's worthwhile to try and optimise this - how many different texture names could you possibly have? Even if you had thousands of textures, you wouldn't even break 1MB of memory with their filenames.

Share this post


Link to post
Share on other sites
Not really, other than giving it shorter filenames. You can't guarantee you won't have collisions with fewer bits, its not mathematically possible. besides, in otder to go from the short version to the long version (which you need to identify the file and load it.) the long version needs to be in memory anyhow.

A true hashmap, as opposed to a simple std::map, will take advantage of the odds that a hash will get you into the ballpark quickly, but must ultimately defer to the full key to resolve collisions. Its sort of a middle-ground, but real hash-maps weren't added to the C++ standard library until TR1, I believe -- though there's always boost.

Now, if you're using the full filepath (from C: or whatever the root is) -- which you shouldn't be doing anyhow -- a good idea would be to use the shortest possible relative path instead.

Now, something like an integer as the map key does have other benefits -- its far quicker to compare/sort an integer than it is for a string. So depending on the frequency that the key is used to look up a particular texture, the switch can be worthwhile. In that case, I would recommend having two maps: 1 that maps from integers to the filenames, and another which maps from those same integers to the corresponding image data. Then I'd define some constants (or an enumeration) with names based on the filenames that integer will represent.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this