• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    19
  • comments
    46
  • views
    39417

SDL_vault, an asset loading helper (Updated)

Sign in to follow this  
Followers 0
DejaimeNeto

1517 views

@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
Sign in to follow this  
Followers 0


2 Comments


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