• 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
viper110110

Template content manager in c++

6 posts in this topic

I am trying to write a content manager for my game. It will load files from the drive, or if the file has been requested before, it will return the same data without loading it from the drive again. When I think about the way the content manager works in XNA, you have to specify the type of content you want to load as a templated type. My question is what happens to the backing storage solution when I template different types of content? I just want to have to create one ContentManager instance, but have it Load<>() multiple types of data. I am confused as to how to do this.

 

My understanding of templates is that, if I just template the function, it will create multiple copies of the function for each type I use. I am just confused as to how this affects the storage as I will need to insert and get data of the type of the function.

0

Share this post


Link to post
Share on other sites

Well XNA is based on .net, so it is using generics. C# generics syntax might look similar to c++ templates, but they really are quite different.

 

.net can query the type of the generic at runtime and cast to that type (that's how XNA's ContentManager works - all content resource types are stored in the same dictionary).

 

You could probably achieve something similar If your content manager stores some type identifier along with each resource object (or use RTTI?) allowing it to validate the type before casting it and handing it back.

0

Share this post


Link to post
Share on other sites
A function is not data, you don't insert and get data of the type, you just call. You can have a class with methods like this however. If you only want one ContentManager for all your types, the that class would not be a template. In that case you would have one class that has multiple template methods.

For caching you need a way to know how to destroy resources properly, which would mean storing the type of each object instance your class, because unlike load, your ContentManager destructor cannot be a template. One way to do this is called type erasure, which effectively uses inheritance to keep track of the types, so that a virtual destructor is called on each cached resource (Google for more details). An advantage to this method it does not require you to know every type that may get loaded when you write the class. Another way is to simply have a enum or similar that's kept with each object identifying its type so that you can manually free the right amount of data and do any other cleanup.
0

Share this post


Link to post
Share on other sites


My understanding of templates is that, if I just template the function, it will create multiple copies of the function for each type I use. I am just confused as to how this affects the storage as I will need to insert and get data of the type of the function.
Your understanding is correct. In case of a name clash, the parameters will implicitly "pass" the "type of the function" to be called... and thus returned. Their type, statically determined.

Be careful with that as the compiler might infer a different type from what you expect.

If parameters are ambiguous, you'll have to explicitly "pass" the "type of the function" using angular brackets.

 

At the end of the day, I think templated managers are a promise not held. Just think at how different a static texture is to be managed WRT a dynamic one. Dynamic texture VS dynamic vertex buffer? Sure, we can have "template parameters" dealing with those differences, but at the end of the day, I think it's better for everyone to just bite the bullet and write explicitly the various implementations.

0

Share this post


Link to post
Share on other sites

This can surely be done in a simple manner.

// Base class for all resources
class Resource
{
public:

    virtual void Load(const std::string & sourcePath);
    virtual void Unload();
};

// Resource manager
class ResourceManager
{
    std::map< std::string, Resource * > m_Resources;
public:

    template< class T >
    T * GetResource(const std::string & sourcePath)
    {
        if(m_Resources[sourcePath] != 0)
        {
            return dynamic_cast< T * >(m_Resources[sourcePath]);
        }

        T * newT = new T();

        newT->Load(sourcePath);
        m_Resources[sourcePath] = newT;
        return newT;
    }
};

So what happens is when you use the resource manager to get a resource.
It will first check to see if we have already loaded the resource and try to return that one.
If we havent loaded it, load it and store it so we later on can get it.
 

void Engine::Init()
{
    ResourceManager * resourceManager = GetResourceManager();
    TextureResource * textureResource = resourceManager->GetResource< TextureResource  >( "path/to/texture.png" );
}

As you can see above, its very straight forward how to use it smile.png

What is also worth mentioning is that in this example im using std::map to store the resources. You should really experiment with different types of storage to find whats best for your needs. 

0

Share this post


Link to post
Share on other sites

This can surely be done in a simple manner.

// Base class for all resources
class Resource
{
public:

    virtual void Load(const std::string & sourcePath);
    virtual void Unload();
};

// Resource manager
class ResourceManager
{
    std::map< std::string, Resource * > m_Resources;
public:

    template< class T >
    T * GetResource(const std::string & sourcePath)
    {
        if(m_Resources[sourcePath] != 0)
        {
            return dynamic_cast< T * >(m_Resources[sourcePath]);
        }

        T * newT = new T();

        newT->Load(sourcePath);
        m_Resources[sourcePath] = newT;
        return newT;
    }
};

So what happens is when you use the resource manager to get a resource.
It will first check to see if we have already loaded the resource and try to return that one.
If we havent loaded it, load it and store it so we later on can get it.
 

void Engine::Init()
{
    ResourceManager * resourceManager = GetResourceManager();
    TextureResource * textureResource = resourceManager->GetResource< TextureResource  >( "path/to/texture.png" );
}

As you can see above, its very straight forward how to use it smile.png

What is also worth mentioning is that in this example im using std::map to store the resources. You should really experiment with different types of storage to find whats best for your needs. 

 

That dynamic cast is unnecessary IMO. If you know the type, just use a static_cast, no need to check if it's valid. If it's not then you'll get a runtime error and it's the programmer's fault, not the user's. Also you may want to consider using a std::shared_ptr rather than a raw pointer, or returning by reference and having a Release method.

1

Share this post


Link to post
Share on other sites


That dynamic cast is unnecessary IMO. If you know the type, just use a static_cast, no need to check if it's valid. If it's not then you'll get a runtime error and it's the programmer's fault, not the user's. Also you may want to consider using a std::shared_ptr rather than a raw pointer, or returning by reference and having a Release method.

 

A static_cast to the wrong type will result in (probably) a crash when the Resource object is finally accessed at a later time (or possibly worse than a crash - if the vtables match up, then just some unexpected behavior). This will be harder to debug, so it's best to offer some validation up front if you know the programmer who called the method messed up. There are probably other solutions though, if you're not using RTTI.

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