From eac841ec71059e69789edfc74d543c568e91040d Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Tue, 26 Apr 2011 19:20:24 +0200 Subject: [PATCH] Added explicit conversions between different types of sf::Vector2, sf::Vector3 and sf::Rect (implements feature #31) --- include/SFML/Graphics/Rect.hpp | 14 ++++++++++++++ include/SFML/Graphics/Rect.inl | 12 ++++++++++++ include/SFML/System/Vector2.hpp | 14 ++++++++++++++ include/SFML/System/Vector2.inl | 10 ++++++++++ include/SFML/System/Vector3.hpp | 14 ++++++++++++++ include/SFML/System/Vector3.inl | 11 +++++++++++ 6 files changed, 75 insertions(+) diff --git a/include/SFML/Graphics/Rect.hpp b/include/SFML/Graphics/Rect.hpp index 80c941d30..9bb3f0391 100644 --- a/include/SFML/Graphics/Rect.hpp +++ b/include/SFML/Graphics/Rect.hpp @@ -78,6 +78,20 @@ public : //////////////////////////////////////////////////////////// Rect(const Vector2& position, const Vector2& size); + //////////////////////////////////////////////////////////// + /// \brief Construct the rectangle from another type of rectangle + /// + /// This constructor doesn't replace the copy constructor, + /// it's called only when U != T. + /// A call to this constructor will fail to compile if U + /// is not convertible to T. + /// + /// \param rectangle Rectangle to convert + /// + //////////////////////////////////////////////////////////// + template + explicit Rect(const Rect& rectangle); + //////////////////////////////////////////////////////////// /// \brief Check if a point is inside the rectangle's area /// diff --git a/include/SFML/Graphics/Rect.inl b/include/SFML/Graphics/Rect.inl index f2d92453d..986885119 100644 --- a/include/SFML/Graphics/Rect.inl +++ b/include/SFML/Graphics/Rect.inl @@ -59,6 +59,18 @@ Height(size.y) } +//////////////////////////////////////////////////////////// +template +template +Rect::Rect(const Rect& rectangle) : +Left (static_cast(rectangle.Left)), +Top (static_cast(rectangle.Top)), +Width (static_cast(rectangle.Width)), +Height(static_cast(rectangle.Height)) +{ +} + + //////////////////////////////////////////////////////////// template bool Rect::Contains(T x, T y) const diff --git a/include/SFML/System/Vector2.hpp b/include/SFML/System/Vector2.hpp index 63504f92d..399710e0a 100644 --- a/include/SFML/System/Vector2.hpp +++ b/include/SFML/System/Vector2.hpp @@ -55,6 +55,20 @@ public : //////////////////////////////////////////////////////////// Vector2(T X, T Y); + //////////////////////////////////////////////////////////// + /// \brief Construct the vector from another type of vector + /// + /// This constructor doesn't replace the copy constructor, + /// it's called only when U != T. + /// A call to this constructor will fail to compile if U + /// is not convertible to T. + /// + /// \param vector Vector to convert + /// + //////////////////////////////////////////////////////////// + template + explicit Vector2(const Vector2& vector); + //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// diff --git a/include/SFML/System/Vector2.inl b/include/SFML/System/Vector2.inl index 9dec638c5..a64ac4e81 100644 --- a/include/SFML/System/Vector2.inl +++ b/include/SFML/System/Vector2.inl @@ -43,6 +43,16 @@ y(Y) } +//////////////////////////////////////////////////////////// +template +template +inline Vector2::Vector2(const Vector2& vector) : +x(static_cast(vector.x)), +y(static_cast(vector.y)) +{ +} + + //////////////////////////////////////////////////////////// template inline Vector2 operator -(const Vector2& right) diff --git a/include/SFML/System/Vector3.hpp b/include/SFML/System/Vector3.hpp index c25cd4671..64042890d 100644 --- a/include/SFML/System/Vector3.hpp +++ b/include/SFML/System/Vector3.hpp @@ -56,6 +56,20 @@ public : //////////////////////////////////////////////////////////// Vector3(T X, T Y, T Z); + //////////////////////////////////////////////////////////// + /// \brief Construct the vector from another type of vector + /// + /// This constructor doesn't replace the copy constructor, + /// it's called only when U != T. + /// A call to this constructor will fail to compile if U + /// is not convertible to T. + /// + /// \param vector Vector to convert + /// + //////////////////////////////////////////////////////////// + template + explicit Vector3(const Vector3& vector); + //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// diff --git a/include/SFML/System/Vector3.inl b/include/SFML/System/Vector3.inl index 191c6b237..04df64b91 100644 --- a/include/SFML/System/Vector3.inl +++ b/include/SFML/System/Vector3.inl @@ -45,6 +45,17 @@ z(Z) } +//////////////////////////////////////////////////////////// +template +template +inline Vector3::Vector3(const Vector3& vector) : +x(static_cast(vector.x)), +y(static_cast(vector.y)), +z(static_cast(vector.z)) +{ +} + + //////////////////////////////////////////////////////////// template inline Vector3 operator -(const Vector3& left)