• 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.
Sign in to follow this  
Followers 0
Vincent_M

Writing an Asset Manager

12 posts in this topic

I'd like to build an asset manager that can "own" allocated data loaded that's typically from files. That way, I can keep certain commonly-used assets that are persistent across multiple states of the game's life-cycle easily without having to write special-case code.

 

Plus, there's also no confusion of which pointer needs to release my asset from memory.

 

I'd like to somehow store all of my assets in STL maps where each different type of asset gets its own map. For example, if I add a texture to my AssetManager, a map that only holds pointers to allocated assets of type "Texture" will be created if it hasn't already. If I want to load up a SpriteCollection, I'd load and add it to the AssetManager. It'll generate another STL map to hold anything that's of type SpriteCollection if one doesn't already exist, or just add it to the one that does. Then, if I wanted to get a particular texture, I'd pass in the type and name (key) to that texture stored in the Texture's STL map. The getter method would be something along these lines:

Texture2D *texturePtr = AssetManager::GetAsset<Texture2D>("hero_character");

Another interesting thing is since I'm using multiple maps, I could have a SpriteSheet asset loaded that's also named "hero_character", and I'd be unique from the Texture2D pointer who's key is also "hero_character" because they're both different types of assets.

 

I'm new to templates, however... I'm trying to wrap my head around this, but haven't gotten it quite yet. Is this even possible in C++?

0

Share this post


Link to post
Share on other sites

Very interesting! I thought about using a factory class, or at least, a generic factory method for loading my asset. This was mainly because I wanted loading to be static that would add it to the AssetManager, or an instance of an AssetManager if I wanted to treat them like packages (AssetManager instances) from repositories (zipped archives). I do agree that in some instances, singletons are bad. I think that for lower-level foundation work, however, they can be useful if they're actually meant to always stick around for entire duration of the software's life cycle.

 

Btw, I've noticed that some programmers generally make their method parameters const references (for example GetAsset(const std::string &name)). Is this just good practice to make parameters read-only that aren't meant to yield output?

 

It's a noob-ish question, but I've always wondered.

0

Share this post


Link to post
Share on other sites

Btw, I've noticed that some programmers generally make their method parameters const references (for example GetAsset(const std::string &name)). Is this just good practice to make parameters read-only that aren't meant to yield output?

 

It's a noob-ish question, but I've always wondered.

 

For non-POD types (such as std::string or your own structs/classes), using a reference in general is preferred because you avoid the copy when passing the parameter by value (which could be expensive/unneeded). The const is there because you, as a programmer, are making a guarantee that the original object, which you are referencing inside the called function, will not change it's value or, if a struct/class, it's internal state (value of any member variables).

1

Share this post


Link to post
Share on other sites

 


Btw, I've noticed that some programmers generally make their method parameters const references (for example GetAsset(const std::string &name)). Is this just good practice to make parameters read-only that aren't meant to yield output?

 

It's a noob-ish question, but I've always wondered.

 

For non-POD types (such as std::string or your own structs/classes), using a reference in general is preferred because you avoid the copy when passing the parameter by value (which could be expensive/unneeded). The const is there because you, as a programmer, are making a guarantee that the original object, which you are referencing inside the called function, will not change it's value or, if a struct/class, it's internal state (value of any member variables).

 

Thank you! I came to this conclusion too, but wanted some one to verify. Since I'm re-working my foundation code before I start on my next project, I'll have to go through what I currently have, and make all of my input-only parameters constant references for all of my methods.

 


The other benefit is that if you pass a big class through a reference is that it doesn't need to make a stack copy of that object to pass to the function, which makes the application faster.

 

I have run into concerns about this on mobile projects where I'd pass in sub-classes of my base 'Object' class. These instances used to be anywhere from 1000 - 1600 bytes in those days. I've cut my memory footprint per object down to ~200 bytes for my new 'Transform' class, but that's still a little high. I'm also getting away from CPU-side graphics computation since mobile GPUs are picking up in speed and functionality (GPU skinning, eventually instancing via geometry shaders, etc).

0

Share this post


Link to post
Share on other sites

Sorry for the double-post, but after looking over Hodgman's code, I had some questions. For example, you add a new element to your map by using insert()'ing a make_pair(). The way I usually do this is like so:

myMap[name] = childElement; // insert/overwrite assets[name] with a new asset pointer passed in

Depending on the case, I may check if an element of that key already exists, then return early if I don't want to overwrite. For example, when storing a map of pointers, I'll want to manually release allocated data on the heap first.

0

Share this post


Link to post
Share on other sites

Ok, that changes my perspective quite a bit. This is good because I'm still getting used to pointers. I use C# with Unity exclusively at work, so we don't get to bother so much about lower-level stuff like this. It's refreshing... lol

 

I'm leaning toward the singleton approach since it acts like what I want from a use-case perspective, but then there's really no way to automatically clear all of that memory, is that correct? I mean, if I were to have a static Release() method to release all resources for all maps, is there a way to grab each template type of the instanced map to free it?

0

Share this post


Link to post
Share on other sites

I think I've finally got it! It's almost identical to your implementation, but I've learned quite a bit trying to get it to work. I needed the following classes in my implementation:

-IAssetList

-template<class T> AssetList

-template<class T> AssetLoader

-template<class T> AssetListSingleton

-Resources

 

The Resources class has been around for a while, and has used to hold the engine's default textures and shaders. I wanted to expand its use to allow programmers to load, release, flush and get resources. I provided template methods in Resources, which happened to be wrapper calls in to AssetListSingleton, and AssetLoader.

 

I wanted each type of asset to be in their own maps for a faster look-up. For example, Texture2D get its own map, Texture3D gets its own map, etc. AssetList exists as a template class to hold map of the template's type, and its constructor is also used to add itself to Resources. I wanted my Resources class to be the high-level manager that will store pointers to each of these lists in an STL vector for clean-up later on. I couldn't create a vector of type AssetList because it was a template. To get around this, I created an abstract class (interface) called IAssetList that the AssetList template could inherit from. Resources could hold a vector of IAssetList, and IAssetList's constructor would call a method in Resources, passing itself as a pointer, into Resources' AddAssetList() method to be added to the vector. Resource is also treated as a static class with a Shutdown() method that's called by the engine when the app terminates. Programmers can also use Flush() to dump all resources.

 

AssetLoader is a blank class that is specialized by classes that will be treated as assets. AssetListSingleton is a template class that's used to call its static methods to manipulate its static AssetList instance by Resources wrapper methods. As I understand it, different types of AssetListSingleton are generated whenever a different type of AssetListSingleton is referenced. This will also generate another static AssetMap instance, right? This list is accounted for thanks to its superclass' constructor.

 

By the way, are C++ templates generated at run-time when needed, or is it during compile-time?

0

Share this post


Link to post
Share on other sites

In my engine, the objects loads themself from a file stream. That was a personal design decision. I've tried to implement a ResourceTable<key, T*> with functors and creators, but ended up as a kind of unorganized code. If you want the post is here.

 

I'm not saying that you can't have a Asset/Resource Manager class to facilitate the "if-not-found, load". I'm saying that if you want a simple and flexible way of doing, the self loading can help.

Edited by irlanrobson
0

Share this post


Link to post
Share on other sites

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