SDL_vault, an asset loading helper (Updated)

Published March 28, 2014
Advertisement
@UpDate:

[indent=1]Added an automatic FreeUnused system. It can be configured with the Vault Classes' constructors (last parameter) or by calling [color=#0000ff]SetAutoFree([/color][color=#006400]unsigned long[/color][color=#0000ff])[/color] manually. To use this, you need to pass [color=#696969]SDL_INIT_TIMER[/color] to a call of [color=#0000ff]SDL_Init()[/color]. We also need to Initialize the Video and Audio portions of SDL, as well as initialize SDL_image and SDL_mixer. I didn't add any initialization to these classes since I control it outside of the SDL_vault; but if you want to add it, just put it anywhere you want (with error checking). I just wouldn't advise adding it at global initialization.
SDL_vault is a small library built on top of SDL2, SDL_image and SDL_mixer. Licensed under X11, use it as you wish and don't worry about it.

It is basically composed of two [color=#696969]vault classes[/color], one for textures and the other for audio.
I created it using some C++11, so you'd need to use its flag in order to compile.

It is composed of some headers and only two implementation files, really simple stuff.
You use Get functions to pick up a safe (strong) reference to the asset, by passing its file path (for the image or sound). If it is already in memory, it'll pass over a pointer, if not, it will load it.

Nice little details:
1 - No need to call "free"
It uses std::shared_ptr, so it does that automatically whenever you reassign or the objects gets deleted or goes out of scope.
2 - Unsafe References are also available.
If you ever need to get a texture for a single use and don't need to keep a strong reference, you can get a direct pointer to it without increasing the usage count. Of course, it could be freed while in use.
3 - Expiration Time (Configurable)
The library counts with an expiration time. It will only free an asset if it has been unused and unreferenced for at least X mseconds. This will avoid several unnecessary texture destructions and creations by allowing it an extra breathing time. Or you can set it to zero and it will be deleted as soon as this is detected.
4 - Manual "FreeUnused"
The allows you to call FreeUnused() manually. It will then check what assets aren't being used and which ones expired and should be freed. This guarantees you it will never steal your cycles when you can't spare them.
5 - Automatic "FreeUnused"
It also has the possibility of creating an automatic periodic call to FreeUnused. You can pass the period (in ms) to the Vault Class' constructor and it will be configured. Or you can call the SetAutoFree member function, pass the period as an argument, and it set everything up.
6 - No Singletons
It doesn't rely on any singletons, so you could have one vault for every SDL_Renderer or maybe one for each thread.
7 - No more Surface to Texture conversion
I, myself, only used Textures; rarely even touched surfaces. So the vault class responsible for the textures will abstract the loading of the surface from SDL_image. Of course, SDL_image kept the surface loading and texture creation separated for a reason, just one that didn't affect my immediate needs. I'm planning on writing another class for surfaces soon (separated from the textures).

Problems:
1 - It forces you to use std::shared_ptr. You could wrap it or modify the library yourself (really simple stuff) if that is an issue to you.
2 - A single vault is not thread safe. If you want to have more than one thread, I recommend using separate vault objects for them.
3 - A vault is tied to its SDL_Renderer. If you have, say, 3 windows, you'd need 3 different texture vaults.
4 - It doesn't initialize SDL, so you'll have to initialize everything yourself (or add a couple extra lines to these constructors). The minimum here would be to call [color=#696969]SDL_INIT_VIDEO[/color], [color=#696969]TIMER[/color] and [color=#696969]AUDIO[/color].

TODO:
Mainly create an automated build process. Since I am just including the files in my projects, I have no needs for this, but still want it done some day. Just can't guarantee it will come in the near future tho.

More Details in my blog:
http://www.buildandgun.com

Hope it is useful for you!
4 likes 2 comments

Comments

nochoke

I do not know if I will use it but thanks for sharing your work.

April 03, 2014 12:07 AM
tookie

"No Singletons"

Good !!!

April 08, 2014 08:16 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement