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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - mouch

Pages: [1]
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
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.

2
Graphics / LoadFromFile with VS 2010
« on: January 07, 2013, 02:31:19 pm »
Hi,

VS don't find my resources directory.
Here is my folder hierarchy :

trunk:
-------->Graphics
--------------------->resources
-------->Project (VS files)
--------------------->Debug

I found a piece of answer on this thread http://en.sfml-dev.org/forums/index.php?topic=8781.0. But it looks like it didn't work the same with me.

Any solution ?

Pages: [1]
anything