1
General / Abstraction of all kind of resources
« on: January 11, 2013, 11:02:28 am »
Hi everybody,
I'm working on a way to abstract every kind of graphical resources, and this include sf::Sprite, sf::image, sf::String, Animation (one of my classes inheriting from sf::Sprite). With this it would be possible to get an element by his type, name, id.
For that I created an interface, IResource.hh
then I have an abstract class:
then the classes inheriting from Sprite, Image and text:
Everything work fine and I have a list of IResource*. The problem I have is that I have to do a dynamic_cast each time I want to draw an element or in some other cases. Here are examples:
I'd like this to work without the casts. Does someone have an idea ?
Thanks for your time.
I'm working on a way to abstract every kind of graphical resources, and this include sf::Sprite, sf::image, sf::String, Animation (one of my classes inheriting from sf::Sprite). With this it would be possible to get an element by his type, name, id.
For that I created an interface, IResource.hh
class IResource
{
public:
virtual ~IResource() {}
virtual bool loadFromFile(const std::string &) = 0;
virtual void setScale(float, float) = 0;
virtual bool update(const float &) = 0;
virtual const ResourceType::eType & getType() const = 0;
virtual const std::string & getName() const = 0;
virtual const int & getId() const = 0;
};
{
public:
virtual ~IResource() {}
virtual bool loadFromFile(const std::string &) = 0;
virtual void setScale(float, float) = 0;
virtual bool update(const float &) = 0;
virtual const ResourceType::eType & getType() const = 0;
virtual const std::string & getName() const = 0;
virtual const int & getId() const = 0;
};
then I have an abstract class:
class AResource : public IResource
{
protected:
const ResourceType::eType _type;
const std::string & _name;
const int _id;
public:
AResource(const ResourceType::eType, const std::string & = "", const int = 0);
AResource(const AResource &);
virtual ~AResource() {}
virtual const ResourceType::eType & getType() const;
virtual const std::string & getName() const;
virtual const int & getId() const;
virtual bool loadFromFile(const std::string &);
virtual void setScale(float, float);
virtual bool update(const float &);
};
{
protected:
const ResourceType::eType _type;
const std::string & _name;
const int _id;
public:
AResource(const ResourceType::eType, const std::string & = "", const int = 0);
AResource(const AResource &);
virtual ~AResource() {}
virtual const ResourceType::eType & getType() const;
virtual const std::string & getName() const;
virtual const int & getId() const;
virtual bool loadFromFile(const std::string &);
virtual void setScale(float, float);
virtual bool update(const float &);
};
then the classes inheriting from Sprite, Image and text:
class ResourceImage : public sf::Image, public AResource
{
public:
ResourceImage(const std::string &, int);
virtual bool loadFromFile(const std::string &);
};
class ResourceSprite :public AResource, public sf::Sprite
{
public:
ResourceSprite(const sf::Image &, const std::string &, const int);
virtual void setScale(float, float);
};
class Animation : public AResource
{
public:
// blabla
};
class Text : public AResource, public sf::String
{
public:
Text(const std::string &);
virtual ~Text();
};
{
public:
ResourceImage(const std::string &, int);
virtual bool loadFromFile(const std::string &);
};
class ResourceSprite :public AResource, public sf::Sprite
{
public:
ResourceSprite(const sf::Image &, const std::string &, const int);
virtual void setScale(float, float);
};
class Animation : public AResource
{
public:
// blabla
};
class Text : public AResource, public sf::String
{
public:
Text(const std::string &);
virtual ~Text();
};
Everything work fine and I have a list of IResource*. The problem I have is that I have to do a dynamic_cast each time I want to draw an element or in some other cases. Here are examples:
this->_resources.push_back(new ResourceImage("name", id));
if (this->_resources.back()->loadFromFile(path))
{
this->_resources.push_back(new ResourceSprite(*dynamic_cast<ResourceImage*>(this->_resources.back()), name, id));
////////////////////
if (_resources[i].getType() == SPRITE)
window.Draw(*dynamic_cast<ResourceSprite*>(this->_resources[i]))
else if (...)
else if (...)
if (this->_resources.back()->loadFromFile(path))
{
this->_resources.push_back(new ResourceSprite(*dynamic_cast<ResourceImage*>(this->_resources.back()), name, id));
////////////////////
if (_resources[i].getType() == SPRITE)
window.Draw(*dynamic_cast<ResourceSprite*>(this->_resources[i]))
else if (...)
else if (...)
I'd like this to work without the casts. Does someone have an idea ?
Thanks for your time.