Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Abstraction of all kind of resources  (Read 982 times)

0 Members and 1 Guest are viewing this topic.

mouch

  • Newbie
  • *
  • Posts: 2
    • View Profile
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
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;

};
 

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 &);
};
 

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();
};
 

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 (...)
 

I'd like this to work without the casts. Does someone have an idea ?
Thanks for your time.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Abstraction of all kind of resources
« Reply #1 on: January 11, 2013, 11:37:13 am »
Dynamic polymorphism looks like the wrong approach here. Most SFML classes (like sf::Sprite or sf::Image) are not designed as base classes. The attempt to create virtual getType() functions and to cast with dynamic_cast is a good indicator that the abstraction is badly designed.

You should use static polymorphism with templates. This is type-safe, and you won't put everything into a single container (which makes no sense anyway, since sprites, images, strings and animations are completely different things).

If you are interested, you can take a look at how I abstracted from resources (but only true resources, no sprites or anything) in Thor. I use static polymorphism. Here are the links to tutorial and API documentation, you can also have a look at the source code on GitHub.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: