Sign in to follow this  
  • entries
    455
  • comments
    639
  • views
    422454

Resource Manager Design Part 1

Sign in to follow this  
_the_phantom_

74 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.com
someone 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...
Sign in to follow this  


1 Comment


Recommended Comments

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

Share this comment


Link to comment

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