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

Author Topic: Adding a new sf::Vector2f constructor?  (Read 4966 times)

0 Members and 1 Guest are viewing this topic.

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Adding a new sf::Vector2f constructor?
« 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!

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Adding a new sf::Vector2f constructor?
« Reply #1 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...)

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Adding a new sf::Vector2f constructor?
« Reply #2 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

nulloid

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Adding a new sf::Vector2f constructor?
« Reply #3 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

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Adding a new sf::Vector2f constructor?
« Reply #4 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Adding a new sf::Vector2f constructor?
« Reply #5 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);
    }
};
Laurent Gomila - SFML developer

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Adding a new sf::Vector2f constructor?
« Reply #6 on: July 12, 2010, 04:21:28 am »
Oh wow, thanks! it works! I hadn't checked this thread.

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Adding a new sf::Vector2f constructor?
« Reply #7 on: July 12, 2010, 02:53:25 pm »
* Learned something usefull. *

 

anything