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 - Razzeeyy

Pages: 1 [2]
16
Graphics / Which function to overload in class derived from sf::Sprite?
« 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

17
Graphics / 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 =(

Pages: 1 [2]
anything