Sign in to follow this  
ThrakhathZero

Resource Loading

Recommended Posts

So I'm wondering how loading resources (Textures, Meshes, sound files, maps, etc) is done 'correctly.' Currently everything I have is loaded manually, i.e. I have every resource loaded explicitly in code (C++ with MSDN by the way), but it seems to me that this would become very cumbersome very fast once the core is done and I'm mainly making stuff to put into the game (making new levels, enemies, sounds, etc). My first thought was that you have some kind of function that looks at a given directory for a kind of resource (\res\textures for example) and loads every file in that directory that matches the resource type (in this case probably .bmp, .png, et al) saving the means to access it (pointers and file info and stuff) along with the name of the file. So, I would just dump new resources in their directory and have new code refer to the resource by it's name. I'm not really sure how to properly allocate/store things this way (and I'm fuzzy on using file loading without a known filename), so I've havn't 'just done it this way' yet. The other thought I had was to have a loading function as part of the thing's class. But I'm not sure how I would handle making sure the same resource isn't loaded multiple times (I have, say, a dozen enemies with the same mesh/texture), how would I make sure it only gets loaded once, and that all the objects have quick access to it? Everything loading it's own stuff would eat up a lot of memory. Purhaps I just need to refresh my memory on dynamically allocating memory for things, but I thought I'd ask you guys and see what there is on "the right way to do it," I havn't had much luck finding tutorials on this because I'm not exactly sure what I'm looking for yet ... Thanks for your time

Share this post


Link to post
Share on other sites
One (very usual) way is to have a ResourceManager class - generally specialised, like a TextureManager class. Then, whenever you need a texture, you call TextureManager::loadTexture(...), and it will load your texture. However, it should store the texture (a pointer to the texture, whatever), so that if you ever need it again, it doesn't load the texture again, it simply returns a pointer to the already-made texture.

Further abstraction gives you classes like the interface class TextureLoader, which you can inherit from to give different functionality to different file-formats, such as TGA, GIF, JPG, etc.

Share this post


Link to post
Share on other sites
So, at load time do I have to manually call TextureManager->LoadTexture(...) for every texture, and add to that list everytime I make a new one, or is there a way to make a TextureManager->Load() that just searches a predefined location for files of a suitable type?

Share this post


Link to post
Share on other sites
Quote:
Original post by ThrakhathZero
So, at load time do I have to manually call TextureManager->LoadTexture(...) for every texture, and add to that list everytime I make a new one, or is there a way to make a TextureManager->Load() that just searches a predefined location for files of a suitable type?


The former. Every time a model needs a specific texture, you call TextureManager->loadTexture(...) - and the TextureManager should either give it a pointer to one prepared earlier, or load the texture from file (and then save it away, and store a reference to it).

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