SFML community forums
Help => Graphics => Topic started by: Razzeeyy 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
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:
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 =(
-
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...
-
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
-
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 :(
-
SFML 1.6? old 2.0? recent 2.0?
-
SFML 1.6? old 2.0? recent 2.0?
sorry, my bad
SFML 1.6
-
class Tank : public sf::Drawable
{
void Render(RenderTarget& target) const
{
target.Draw(tankSprite);
target.Draw(gunSprite);
}
};
Or, using your current design:
class Tank : public sf::Sprite
{
void Render(RenderTarget& target) const
{
sf::Sprite::Draw(target);
target.Draw(gunSprite);
}
};
-
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! :)