Resource Loading

Started by
3 comments, last by ThrakhathZero 17 years, 10 months ago
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
Advertisement
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
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?
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).
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
I'll give that a try, thanks for the tips

This topic is closed to new replies.

Advertisement