Added explicit conversions between different types of sf::Vector2, sf::Vector3 and sf::Rect (implements feature #31)

This commit is contained in:
Laurent Gomila 2011-04-26 19:20:24 +02:00
parent ade8cb8771
commit eac841ec71
6 changed files with 75 additions and 0 deletions

View File

@ -78,6 +78,20 @@ public :
////////////////////////////////////////////////////////////
Rect(const Vector2<T>& position, const Vector2<T>& 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 <typename U>
explicit Rect(const Rect<U>& rectangle);
////////////////////////////////////////////////////////////
/// \brief Check if a point is inside the rectangle's area
///

View File

@ -59,6 +59,18 @@ Height(size.y)
}
////////////////////////////////////////////////////////////
template <typename T>
template <typename U>
Rect<T>::Rect(const Rect<U>& rectangle) :
Left (static_cast<T>(rectangle.Left)),
Top (static_cast<T>(rectangle.Top)),
Width (static_cast<T>(rectangle.Width)),
Height(static_cast<T>(rectangle.Height))
{
}
////////////////////////////////////////////////////////////
template <typename T>
bool Rect<T>::Contains(T x, T y) const

View File

@ -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 <typename U>
explicit Vector2(const Vector2<U>& vector);
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////

View File

@ -43,6 +43,16 @@ y(Y)
}
////////////////////////////////////////////////////////////
template <typename T>
template <typename U>
inline Vector2<T>::Vector2(const Vector2<U>& vector) :
x(static_cast<T>(vector.x)),
y(static_cast<T>(vector.y))
{
}
////////////////////////////////////////////////////////////
template <typename T>
inline Vector2<T> operator -(const Vector2<T>& right)

View File

@ -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 <typename U>
explicit Vector3(const Vector3<U>& vector);
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////

View File

@ -45,6 +45,17 @@ z(Z)
}
////////////////////////////////////////////////////////////
template <typename T>
template <typename U>
inline Vector3<T>::Vector3(const Vector3<U>& vector) :
x(static_cast<T>(vector.x)),
y(static_cast<T>(vector.y)),
z(static_cast<T>(vector.z))
{
}
////////////////////////////////////////////////////////////
template <typename T>
inline Vector3<T> operator -(const Vector3<T>& left)