SFML community forums

Help => General => Topic started by: Xorlium on May 11, 2010, 04:20:47 am

Title: Adding a new sf::Vector2f constructor?
Post by: Xorlium on May 11, 2010, 04:20:47 am
Hi,

I'm working on a project using sfml, and I have a Vector2r (vector 2 real) with tons of different functions that I like. sf::Vector2f doesn't have many, so I prefer to use mine. My problem is that the sf functions all accept sf::Vector2f, but not of course Vector2r. I already made a constructor of Vector2r(sf::Vector2f), but how can I do it the other way around, so that I can cast implicitly the vectors?

My solution for now is to define a function
sf::Vector2f r2f(Vector2r v);
and use it everywhere, but that doesn't seem very clean. Is there another way?

Thanks!
Title: Adding a new sf::Vector2f constructor?
Post by: nulloid on May 11, 2010, 04:40:29 am
I would simply extend the Vector2r class with this function:
Code: [Select]
sf::Vector2f getVector2f();

and ifyou want a 2f from your 2r, just write: sf::FunctThatRequiresVector2f(v2r.getVector2f());

(I hope I understood your problem correctly...)
Title: Adding a new sf::Vector2f constructor?
Post by: Xorlium on May 11, 2010, 04:43:39 am
Yeah, that's basically what I was doing, except defining it outside the class. But yeah.

Thanks
Title: Adding a new sf::Vector2f constructor?
Post by: nulloid on May 11, 2010, 04:46:22 am
So that wasn't that much of a help... well, I have no other idea without providing a v2r version of sfml's functions :D
Title: Adding a new sf::Vector2f constructor?
Post by: Xorlium on May 11, 2010, 04:59:49 am
Heh, yeah. I was looking to see if there was a way to define a constructor for Vector2f outside the class declaration, like:
Vector2f(Vector2r vec);

or something.
Title: Adding a new sf::Vector2f constructor?
Post by: Laurent on May 11, 2010, 07:48:07 am
You can define an implicit conversion operator.
Code: [Select]
class Vector2r
{
public :

    ...

    operator sf::Vector2f() const
    {
        return sf::Vector2f(x, y);
    }
};
Title: Adding a new sf::Vector2f constructor?
Post by: Xorlium on July 12, 2010, 04:21:28 am
Oh wow, thanks! it works! I hadn't checked this thread.
Title: Adding a new sf::Vector2f constructor?
Post by: Spidyy on July 12, 2010, 02:53:25 pm
* Learned something usefull. *