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

Storing class with a template in an array?

6 posts in this topic

I've always had a resource manager in my client software that has different resource containers.

Basically this:

 

in library:

template<class _Type>
class Container {
   // code dealing with loading resources
};

in client:

class Resources {
public:
    Container<Sprite> sprite;
    Container<Music> music;
    Container<Font> font;
};

So in the client code, you just use it like this:

Sprite* player_idle = res->sprite["Objects"]["Characters"]["Player"]("idle");

I won't get into any more detail, but you now understand how it works, I presume.

 

So what I want is to have the "Resources" class in the library.

 

I want it to be like this:

class Resources {
    std::vector<Container<?>> _containers;
public:
    template<class _Type>
    void add_type() {
        _containers.push_back(Container<_Type>());
    }
};

But that won't work, as the std::vector class only takes one specific type.

 

I've thought of making a wrapper class of every resource type, and just base them off a class, but that seems like a very unelegant solution, both for the library and client.

 

I could also just use a switch, but what would be the point - the client would not be able to create new types that way.

 

Thanks in advance! :)

0

Share this post


Link to post
Share on other sites

You can use the typeid keyword, get the hash of the type, and store multiple types (pointers to them?) in a map (c++11 unordered_map might be fastest out of standard containers).

 

So when you add a type you allocate the object of that type, and put the pointer in the map with the key as the hash of the type (from the thing returned by typeid)

 

Same with access.

 

But that adds a run time cost (if you use a hash map as the container it should be nearly O(1) though), even though you should know what types you will have at compile time.

So you need to pick between convenience and a unnecessary run time cost.

1

Share this post


Link to post
Share on other sites

Maybe I misunderstood you, but this means that I can only have one container per type, right?

What if I want two different containers of music. It sounds a bit weird with music, but there could be a different class in the future that makes sense to have two containers for.

 

Also, do you mean that the usage should look like this?

res->container("Sprite")["Objects"]["Characters"]["Player"]("idle")

I apologise if I have misunderstood you.

 

I am thinking a bit clearer about it with your post though, so +rep from me.

I might use your idea if nothing better comes up.

Edited by diventurer
0

Share this post


Link to post
Share on other sites

See this: ResourcePool.h.

 

What I did was to create a base, pure interface, resource container class (Abstract Base Class) then derive a templated class from it which the application can use.

 

Basically it would look like this:

// ResourcePool to be created by the res pool will inherit from this class
class IResourceItem
{
	...
};

class ResourcePool
{
public:

	virtual ~ResourcePool( void ) { }

	virtual IResourceItem * Create( void ) = 0;

	virtual Delete( IResourceItem *pItem ) = 0;

	...
};


template < typename T >
class ResourcePoolT : public ResourcePool
{
public:

	ResourcePoolT( void ) { }

	virtual ~ResourcePoolT( void ) { }

	virtual IResourceItem * Create( void ) { /* create resource */ }

	virtual Delete( IResourceItem *pItem ) { /* Delete resource */ }

	// Ulitily functions to do away with type casting every time we create the resource
	T * CreateResource( void ) { return (T*)Create(); }

	...
};


// Your resources manager class
class ResourcePoolManager
{
	vector<ResourcePool> m_resources;  // Or you could use a map to associate each resource to a certain type(enum, key, id...)
};

So your manager doesn't have to keep track different kind of resource container types. It just manages the base, pure-virtual, ResourcePool class.

 

I hope this helps :)

1

Share this post


Link to post
Share on other sites

Thanks a lot! 

 

This definitely helped me. :)

 

I should have been able to think of this. (but I didn't!)

 

So thanks again.

0

Share this post


Link to post
Share on other sites

If you want it to support multiple Container objects of the same type, you can always make the map point to potentially multiple objects when you access it with the hash of the type (unordered_multimap?)

 

Now, i was assuming that you wanted to be able to get your container for objects of type T from the resources class, assuming it was added in elsewhere. But i dont see how that would work with multiple containers of the same type, unless you give the containers ID's (so you would have a sprite container 1 and sprite container 2 for example)

 

Anyways, if you only need a single container per type in the resources class, the access would look like:

 

res->container<Sprite>()["blah"]["blah"]("blah")

 

where container method looks like:

template<T>

Container<T> container()

{

return static_cast<T*>(m_containerMap.find(typeid(T).hash_code())); //Might want to check if the container actually exists for that type first

}

 

hash_code() seems to be a C++11 feature though, according to the internets. If you dont have C++11 you can get the name of the type and use that instead.

0

Share this post


Link to post
Share on other sites

Yeah, I realised Waterlimon.

 

I realised BrentChua's method is a bit restricting, but I'll go with that one. It is more what I want to go for.

 

Thanks all for the help though!

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