• entries
455
639
• views
423202

# Resource Manager Design Part 1

77 views

Disclaimer:
I don't claim to be a design guru, nor do I claim to be a C++ expert, this is just me working idea through to try and come up with a decent and comprehensive resource manager.

With that out of the way, welcome [smile]
As started above and before now, this is basically going to be me pondering things and working through a design of a resource system, hopefull I'll formalise this when finished and both this and the formal document will be some use to people.

Anyways, right now I've finished and coded up the first design; its a simple two class affair, one being the manager and the other being a 'handle' class.

One of the first issues which hit me was; is manager a good name?
After a trip to dictionary.com I decided it was and the definition helpped work out one other implimentation detail; what responciblity goes where?
Quote:
 Manager at Dictionary.comsomeone who controls resources and expenditures

Using this quote and thinking about it in real world terms means that really all a manager should do is hand out and keep a track of resources, this is important as it lets us know where we should put the functionality such as binding the texture.

Why is this important? Well, the definition gives us the 'scope' of the class and its responibilties and one of the tenets of OOP is the Single responsibility principle or SRP, which states that one class should have one responciblity, which in this case is the handing out and tracking of resources, so to add things such as binding to it would be a violation of the SRP.

This means the handle object needs functions so that we can act upon a texture, I'm reasonably happy with this idea, although the SRP is kinda cominbg back to me at this point as does a handle really need to act on a texture at all? Maybe someone else should be doing this work and is supplied a handle? hmmm, worth considering, if only because as things stand the handle would have to free the texture, does this make sense wrt SRP?

The net result of this is the following code, which as it stands it technically a very basic texture resource handler;

textureManagerLib.hpp
#ifndef TEXTUREMANAGERLIB_HPP#define TEXTUREMANAGERLIB_HPP#include #include #include namespace TextureManagerLib{	class TextureHandle	{	public:		TextureHandle(GLuint id) : id(id){};		~TextureHandle(){};		void Bind()		{			glBindTexture(GL_TEXTURE_2D,id);		}	private:		GLuint id;	};	typedef boost::shared_ptr handle_t;		class TextureManager	{	public:		TextureManager();		~TextureManager();		handle_t LoadTexture(std::string const &filename);	protected:	private:		typedef std::map texturemap_t;		texturemap_t texturemap;	};}#endif

textureManagerLib.cpp
#include #include #include "textureManagerLib.hpp"#include namespace TextureManagerLib{	TextureManager::TextureManager()	{	}	TextureManager::~TextureManager()	{	}	handle_t TextureManager::LoadTexture(const std::string &filename)	{		texturemap_t::iterator it = texturemap.find(filename);		if(it != texturemap.end())			return it->second;				GameTextureLoader::ImagePtr img(GameTextureLoader::LoadTexture(filename),GameTextureLoader::FreeTexture);		GLuint id;		glGenTextures(1,&id);		handle_t texture(new TextureHandle(id));		texture->Bind();		if(img->getFormat() == GameTextureLoader::FORMAT_RGBA)			glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,img->getWidth(),img->getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,img->getDataPtr());		else			glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,img->getWidth(),img->getHeight(),0,GL_BGR,GL_UNSIGNED_BYTE,img->getDataPtr());		texturemap.insert(std::make_pair(filename,texture));		return texture;	}}

TextureManagerLib v0.1 is done [smile]
Now, to consider the issues I raised some more...

## 1 Comment

Interesting take on the "real world" definitions and OOP principles. I'm guilty of implementing too many classes/functions that do everything - splitting them up really would be a good idea.

Anyway, back to implementing by CCool3DEngine class...

Jack

## Create an account

Register a new account

• 12
• 10
• 11
• 18
• 13