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

Author Topic: Which function to overload in class derived from sf::Sprite?  (Read 3094 times)

0 Members and 1 Guest are viewing this topic.

Razzeeyy

  • Newbie
  • *
  • Posts: 17
    • View Profile
Which function to overload in class derived from sf::Sprite?
« on: January 02, 2012, 05:51:49 pm »
Hi all.
Which function must I overload in my class derived from sf::Sprite to keep that drawing behavior for RenderWindow::Draw() function just like the same as for Sprite. you know...

I mean how to keep behavior of my class like that
Code: [Select]
sf::RenderWindow App(/*init there*/);
sf::Sprite MySprite(); //let's count this sprite as fully initialized

App.Draw(MySprite);

But for my derived class.
To make the discussion more objective here is my class(es) code:

Code: [Select]
class Gun : public sf::Sprite
{
 public:
    Gun(const sf::Vector2f& Position, float Rotation=0, std::string texture=Options::Gun::Texture)
    {
        Texture.LoadFromFile(texture);
        sf::Sprite::SetImage(Texture);
        sf::Sprite::SetPosition(Position);
        sf::Sprite::SetCenter(Options::Gun::Center);
        sf::Sprite::SetRotation(Rotation);
    }
    void AimAt(const sf::Vector2f& Mouse)
    {
        sf::Vector2f Position = sf::Sprite::GetPosition();
        float angle = std::atan2(Mouse.y - Position.y, Mouse.x - Position.x)*rad;  // угол наклона

        sf::Sprite::SetRotation(270 - angle);
    }
 private:
    sf::Image Texture;
    float VecLength(const sf::Vector2f& first, const sf::Vector2f& second)
    {
        return std::sqrt (std::pow( (second.x-first.x) ,2) + std::pow( (second.y-first.y) ,2));
    }
};

class Tank : public sf::Sprite
{
 public:
    Tank(const sf::Vector2f& Position, float Rotation=0, std::string texture=Options::Tank::Texture) : Cannon(Options::Tank::GunPlace, Rotation)
    {
        Texture.LoadFromFile(texture);
        sf::Sprite::SetImage(Texture);
        sf::Sprite::SetPosition(Position);
        sf::Vector2f PureCenter(Texture.GetWidth()/2 , Texture.GetHeight()/2);
        sf::Sprite::SetCenter(PureCenter);
        sf::Sprite::SetRotation(Rotation);
    }
    Gun& GetGun(void)
    {
        return Cannon;
    }
    void Simulate(const sf::Vector2f& Mouse)
    {

    }
 private:
    Gun Cannon;
    sf::Image Texture;
};


As you can see I need my Tank class to Draw both itself's sprite and the cannon's sprite at the request of drawning the Tank sprite...

So which functions and how must I overload?
I guess it's some of Sprite::Draw() or Sprite::Render() functions.

Forehand thank you. And sorry for my bad English =(

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6267
  • Thor Developer
    • View Profile
    • Bromeon
Which function to overload in class derived from sf::Sprite?
« Reply #1 on: January 02, 2012, 06:11:53 pm »
None, sf::Sprite isn't intended to be used as a base class. In order to create custom drawable entities, you should rather derive directly from sf::Drawable and override Draw().

And generally, a tank or a gun usually have a sprite (not are one). Aggregation also brings less coupling than inheritance...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Razzeeyy

  • Newbie
  • *
  • Posts: 17
    • View Profile
Which function to overload in class derived from sf::Sprite?
« Reply #2 on: January 02, 2012, 06:44:45 pm »
Quote from: "Nexus"
None, sf::Sprite isn't intended to be used as a base class. In order to create custom drawable entities, you should rather derive directly from sf::Drawable and override Draw().

And generally, a tank or a gun usually have a sprite (not are one). Aggregation also brings less coupling than inheritance...


Then is there any dirty hack for that?

Because I want the tank to manipulate both itself and the gun (i think it's rightly even for real-life :P)
And I don't want to write lots of code when deriving from sf::Drawable... :(

I think there must be solution... because the framework is very unflexible then... Or just I can't get it philosophy :D

Razzeeyy

  • Newbie
  • *
  • Posts: 17
    • View Profile
Which function to overload in class derived from sf::Sprite?
« Reply #3 on: January 02, 2012, 07:43:53 pm »
OK then when I derive from sf::Drawable how to override Draw() function correctly? Any examples? :P
I just then want to "pass-throught" execution to my two sprite objects which repserent gun and tank texture... but I dunno how :(
The SFML architecture is perfect but sometimes I can't even figure out where to look in sources for some class member-functions usage :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Which function to overload in class derived from sf::Sprite?
« Reply #4 on: January 02, 2012, 07:49:23 pm »
SFML 1.6? old 2.0? recent 2.0?
Laurent Gomila - SFML developer

Razzeeyy

  • Newbie
  • *
  • Posts: 17
    • View Profile
Which function to overload in class derived from sf::Sprite?
« Reply #5 on: January 02, 2012, 08:09:56 pm »
Quote from: "Laurent"
SFML 1.6? old 2.0? recent 2.0?

sorry, my bad

SFML 1.6

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Which function to overload in class derived from sf::Sprite?
« Reply #6 on: January 02, 2012, 08:40:36 pm »
Code: [Select]
class Tank : public sf::Drawable
{
    void Render(RenderTarget& target) const
    {
        target.Draw(tankSprite);
        target.Draw(gunSprite);
    }
};


Or, using your current design:
Code: [Select]
class Tank : public sf::Sprite
{
    void Render(RenderTarget& target) const
    {
        sf::Sprite::Draw(target);
        target.Draw(gunSprite);
    }
};
Laurent Gomila - SFML developer

Razzeeyy

  • Newbie
  • *
  • Posts: 17
    • View Profile
Which function to overload in class derived from sf::Sprite?
« Reply #7 on: January 02, 2012, 08:51:02 pm »
THANK YOU very much!

My desing is wrong (that snippet is not working) so I will adopt it for inheritance from sf::Drawable, that will be more corret.

P.S. Sadly that I'm poor man and have no extra money. Otherwise I would donate to you for SFML. Great Job, Laurent. Keep it up! :)

 

anything