Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL_vault, an asset loading helper (Updated)

Posted by dejaime, 28 March 2014 · 849 views

SDL2 C++11 C++ BuildAndGun GameDev dejaime

Added an automatic FreeUnused system. It can be configured with the Vault Classes' constructors (last parameter) or by calling SetAutoFree(unsigned long) manually. To use this, you need to pass SDL_INIT_TIMER to a call of SDL_Init(). 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 vault classes, 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).

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 SDL_INIT_VIDEO, TIMER and AUDIO.

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:

Hope it is useful for you!

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

"No Singletons"

Good !!!

0 user(s) viewing

0 members, 0 guests, 0 anonymous users

Latest Visitors

My Contacts' Journals

July 2014 »

20212223 24 2526

Google Shared Items