So I want to code a resource management system for my (OpenGL) rendering engine.
I have many different kind of resources: images, opengl textures, vbo's, ebo's, vao's, rbo's, shaders, shader programs. (did I miss something?)
What are the coding options for building this kind of manager? I tried looking at other rendering engines' codes but they're hard to understand.
The obvious answer for resources should be something like the following code, while other resources (like shaders and images) will have their own Manager class, and not a general one.
//abstract class
class Object //I called this Object but could also be named Resource
{
public:
Object() : _id(-1) {};
virtual void create() = 0;
virtual void bind() = 0;//doesn't make sense for resources like "image"
virtual void free() = 0;
int id() const { return _id; }
protected:
int _id;
};
class VBO : public Object
{
public:
VBO() {}
void create() {};
void bind() {};
void free() {};
};
class Manager
{
public:
Manager() {}
void add(Object *obj) {
_map.insert( std::pair<int, Object*>(obj->id(), obj) );
}
void free(Object *obj) {
_map.erase(obj->id());
obj->free();
}
size_t size() const { return _map.size(); }
private:
std::map<int, Object*> _map;
Object *_obj;
};
Is this a good approach? Are there any better approaches for resource management systems?