Lately, I've been working a lot on the blog's pet project, the SpaceRock Miners game. I have already completed a memory pool, and a basic actor-component system. But something really bugged me. I was using a map to store every sound and texture I loaded, and I searched it whenever I needed to check if something was already cached. So I decided to take this a tad further and separate this part of the game engine into a minimalistic helper library, that I'm calling SDL_vault. Named it SDL_vault because I always call this concept asset vault. But why asset vault? Probably because I'm nuts!
This mini library will be based on SDL2, SDL_image and SDL_mixer. It only has two classes, AudioVault and TextureVault; they have their own asset list and take care them. The TextureVault, given the necessity of a renderer to create a texture, has a SDL_Renderer that you need to set up before using (constructor, preferably), otherwise it will fail.
The only thing you need to do is call the FreeUnused function and it will then free any unused assets. If you set an expiration time, it will only free unused assets if their expiration time has passed. The expiration time only starts to count after a FreeUnused call while the asset is not being used anymore (last user dumped it). Sometimes, setting an expiration period of some seconds can be a good idea; something between 5s and a minute is just fine, depending on the case, even more. There's definitely no need to call FreeUnused on every frame.
It was supposed to be finished by now, but when I migrated to SDL 2.0.3 my build system broke somehow. And now I feel like a newbie with linkage problems again... Anyway, guess I'll just clean my SDL installation and rebuild it and... we'll see.
Update: It is finished, tested and ready to use. No warranties though, check the license.
GitHubOf course, its license will be X11 (MIT). I'll be using a GIT repository:
You can instance multiple audio/texture vaults, with different or the same renderer for textures. Each instance will be independent from each other, so it is possible to have multiple copies of the same texture/audio if you load the same file on different instances. But that also means you can destroy any of them without having to worry about breaking any other instances.
Every asset has an ID, and they are unique even between vault instances. Since this is halting my project on the main engine, I'll finish this asap. Maybe with some basic doxy documentation.
Just to clarify, this is separated from the main engine simply because it will probably be useful outside of it as well.
Hope you guys use it, even if just as an study material on what not-to-do!
Over and Out.