Added functions and overloads to sf::Rect
Slightly changed the sf::View interface git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1138 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
92e79b8123
commit
b86c4627ab
12 changed files with 276 additions and 162 deletions
|
@ -30,6 +30,7 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Config.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
|
@ -68,6 +69,14 @@ public :
|
|||
////////////////////////////////////////////////////////////
|
||||
void SetFromTransformations(const Vector2f& Center, const Vector2f& Translation, float Rotation, const Vector2f& Scale);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Build a matrix from a projection
|
||||
///
|
||||
/// \param Rectangle : Rectangle defining the region to project
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetFromProjection(const FloatRect& Rectangle);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Transform a point by the matrix
|
||||
///
|
||||
|
|
|
@ -54,6 +54,7 @@ inline Matrix3::Matrix3(float a00, float a01, float a02,
|
|||
////////////////////////////////////////////////////////////
|
||||
inline void Matrix3::SetFromTransformations(const Vector2f& Center, const Vector2f& Translation, float Rotation, const Vector2f& Scale)
|
||||
{
|
||||
// Combine the transformations
|
||||
float Angle = Rotation * 3.141592654f / 180.f;
|
||||
float Cos = static_cast<float>(cos(Angle));
|
||||
float Sin = static_cast<float>(sin(Angle));
|
||||
|
@ -64,6 +65,7 @@ inline void Matrix3::SetFromTransformations(const Vector2f& Center, const Vector
|
|||
float Tx = -Center.x * SxCos - Center.y * SySin + Translation.x;
|
||||
float Ty = Center.x * SxSin - Center.y * SyCos + Translation.y;
|
||||
|
||||
// Rebuild the matrix
|
||||
myData[0] = SxCos; myData[4] = SySin; myData[8] = 0.f; myData[12] = Tx;
|
||||
myData[1] = -SxSin; myData[5] = SyCos; myData[9] = 0.f; myData[13] = Ty;
|
||||
myData[2] = 0.f; myData[6] = 0.f; myData[10] = 1.f; myData[14] = 0.f;
|
||||
|
@ -71,6 +73,25 @@ inline void Matrix3::SetFromTransformations(const Vector2f& Center, const Vector
|
|||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Build a matrix from a projection
|
||||
////////////////////////////////////////////////////////////
|
||||
inline void Matrix3::SetFromProjection(const FloatRect& Rectangle)
|
||||
{
|
||||
// Projection components
|
||||
float A = 2.f / Rectangle.GetSize().x;
|
||||
float B = -2.f / Rectangle.GetSize().y;
|
||||
float C = -(Rectangle.Left + Rectangle.Right) / Rectangle.GetSize().x;
|
||||
float D = (Rectangle.Bottom + Rectangle.Top) / Rectangle.GetSize().y;
|
||||
|
||||
// Rebuild the projection matrix
|
||||
myData[0] = A; myData[4] = 0.f; myData[8] = 0.f; myData[12] = C;
|
||||
myData[1] = 0.f; myData[5] = B; myData[9] = 0.f; myData[13] = D;
|
||||
myData[2] = 0.f; myData[6] = 0.f; myData[10] = 1.f; myData[14] = 0.f;
|
||||
myData[3] = 0.f; myData[7] = 0.f; myData[11] = 0.f; myData[15] = 1.f;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Transform a point by the matrix
|
||||
////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
|
@ -60,20 +61,20 @@ public :
|
|||
Rect(T LeftCoord, T TopCoord, T RightCoord, T BottomCoord);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the width of the rectangle
|
||||
/// Get the size of the rectangle
|
||||
///
|
||||
/// \return Width of rectangle
|
||||
/// \return Size of rectangle
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
T GetWidth() const;
|
||||
Vector2<T> GetSize() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the height of the rectangle
|
||||
/// Get the center of the rectangle
|
||||
///
|
||||
/// \return Height of rectangle
|
||||
/// \return Center of rectangle
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
T GetHeight() const;
|
||||
Vector2<T> GetCenter() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the whole rectangle by the given offset
|
||||
|
@ -84,6 +85,14 @@ public :
|
|||
////////////////////////////////////////////////////////////
|
||||
void Offset(T OffsetX, T OffsetY);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the whole rectangle by the given offset
|
||||
///
|
||||
/// \param Off : Offset to apply to the current position
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Offset(const Vector2<T>& Off);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
///
|
||||
|
@ -95,16 +104,37 @@ public :
|
|||
////////////////////////////////////////////////////////////
|
||||
bool Contains(T X, T Y) const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
///
|
||||
/// \param Point : Point to test
|
||||
///
|
||||
/// \return True if the point is inside
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Contains(const Vector2<T>& Point) const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles
|
||||
///
|
||||
/// \param Rectangle : Rectangle to test
|
||||
/// \param OverlappingRect : Rectangle to be filled with overlapping rect (NULL by default)
|
||||
/// \param Rectangle : Rectangle to test
|
||||
///
|
||||
/// \return True if rectangles overlap
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Intersects(const Rect<T>& Rectangle, Rect<T>* OverlappingRect = NULL) const;
|
||||
bool Intersects(const Rect<T>& Rectangle) const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles and return the
|
||||
/// resulting rectangle
|
||||
///
|
||||
/// \param Rectangle : Rectangle to test
|
||||
/// \param OverlappingRect : Rectangle to be filled with the overlapping rect
|
||||
///
|
||||
/// \return True if rectangles overlap
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Intersects(const Rect<T>& Rectangle, Rect<T>& OverlappingRect) const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
|
|
|
@ -52,22 +52,22 @@ Bottom(BottomCoord)
|
|||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the width of the rectangle
|
||||
/// Get the size of the rectangle
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
T Rect<T>::GetWidth() const
|
||||
Vector2<T> Rect<T>::GetSize() const
|
||||
{
|
||||
return Right - Left;
|
||||
return Vector2<T>(Right - Left, Bottom - Top);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the height of the rectangle
|
||||
/// Get the center of the rectangle
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
T Rect<T>::GetHeight() const
|
||||
Vector2<T> Rect<T>::GetCenter() const
|
||||
{
|
||||
return Bottom - Top;
|
||||
return Vector2<T>((Left + Right) / 2, (Top + Bottom) / 2);
|
||||
}
|
||||
|
||||
|
||||
|
@ -84,6 +84,16 @@ void Rect<T>::Offset(T OffsetX, T OffsetY)
|
|||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the whole rectangle by the given offset
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
void Rect<T>::Offset(const Vector2<T>& Off)
|
||||
{
|
||||
Offset(Off.x, Off.y);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -94,29 +104,55 @@ bool Rect<T>::Contains(T X, T Y) const
|
|||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool Rect<T>::Contains(const Vector2<T>& Point) const
|
||||
{
|
||||
return Contains(Point.x, Point.y);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool Rect<T>::Intersects(const Rect<T>& Rectangle, Rect<T>* OverlappingRect) const
|
||||
bool Rect<T>::Intersects(const Rect<T>& Rectangle) const
|
||||
{
|
||||
// Compute overlapping rect
|
||||
Rect Overlapping(std::max(Left, Rectangle.Left),
|
||||
std::max(Top, Rectangle.Top),
|
||||
std::min(Right, Rectangle.Right),
|
||||
std::min(Bottom, Rectangle.Bottom));
|
||||
Rect<T> Overlapping(std::max(Left, Rectangle.Left),
|
||||
std::max(Top, Rectangle.Top),
|
||||
std::min(Right, Rectangle.Right),
|
||||
std::min(Bottom, Rectangle.Bottom));
|
||||
|
||||
// If overlapping rect is valid, then there is intersection
|
||||
return (Overlapping.Left < Overlapping.Right) && (Overlapping.Top < Overlapping.Bottom);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles and return the
|
||||
/// resulting rectangle
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool Rect<T>::Intersects(const Rect<T>& Rectangle, Rect<T>& OverlappingRect) const
|
||||
{
|
||||
// Compute overlapping rect
|
||||
Rect<T> Overlapping(std::max(Left, Rectangle.Left),
|
||||
std::max(Top, Rectangle.Top),
|
||||
std::min(Right, Rectangle.Right),
|
||||
std::min(Bottom, Rectangle.Bottom));
|
||||
|
||||
// If overlapping rect is valid, then there is intersection
|
||||
if ((Overlapping.Left < Overlapping.Right) && (Overlapping.Top < Overlapping.Bottom))
|
||||
{
|
||||
if (OverlappingRect)
|
||||
*OverlappingRect = Overlapping;
|
||||
OverlappingRect = Overlapping;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (OverlappingRect)
|
||||
*OverlappingRect = Rect(0, 0, 0, 0);
|
||||
OverlappingRect = Rect<T>(0, 0, 0, 0);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,10 +36,8 @@
|
|||
|
||||
namespace sf
|
||||
{
|
||||
class RenderTarget;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// This class defines a view (position, size, etc.) ;
|
||||
/// This class defines a view (position, size, etc.);
|
||||
/// you can consider it as a 2D camera
|
||||
////////////////////////////////////////////////////////////
|
||||
class SFML_API View
|
||||
|
@ -55,13 +53,30 @@ public :
|
|||
explicit View(const FloatRect& ViewRect = FloatRect(0, 0, 1000, 1000));
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the view from its center and half-size
|
||||
/// Construct the view from its center and size
|
||||
///
|
||||
/// \param Center : Center of the view
|
||||
/// \param HalfSize : Half-size of the view (from center to corner)
|
||||
/// \param HalfSize : Size of the view
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
View(const sf::Vector2f& Center, const sf::Vector2f& HalfSize);
|
||||
View(const Vector2f& Center, const Vector2f& Size);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Set the rectangle defining the view by its center and size
|
||||
///
|
||||
/// \param Center : Center of the view rectangle
|
||||
/// \param Size : Size of the view rectangle
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetRect(const Vector2f& Center, const Vector2f& Size);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Set the rectangle defining the view
|
||||
///
|
||||
/// \param Rect : Rectangle defining the center and size of the view
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetRect(const FloatRect& Rect);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change the center of the view (take 2 values)
|
||||
|
@ -78,32 +93,33 @@ public :
|
|||
/// \param Center : New center
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetCenter(const sf::Vector2f& Center);
|
||||
void SetCenter(const Vector2f& Center);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change the half-size of the view (take 2 values)
|
||||
/// Change the size of the view (take 2 values)
|
||||
///
|
||||
/// \param HalfWidth : New half-width
|
||||
/// \param HalfHeight : New half-height
|
||||
/// \param Width : New width
|
||||
/// \param Height : New height
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetHalfSize(float HalfWidth, float HalfHeight);
|
||||
void SetSize(float Width, float Height);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change the half-size of the view (take a vector)
|
||||
/// Change the size of the view (take a vector)
|
||||
///
|
||||
/// \param HalfSize : New half-size
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetHalfSize(const sf::Vector2f& HalfSize);
|
||||
void SetSize(const Vector2f& Size);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Rebuild the view from a rectangle
|
||||
/// Get the rectangle defining the view,
|
||||
/// which is the combined center and size.
|
||||
///
|
||||
/// \param ViewRect : Rectangle defining the position and size of the view
|
||||
/// \return View rectangle
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void SetFromRect(const FloatRect& ViewRect);
|
||||
FloatRect GetRect() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the center of the view
|
||||
|
@ -111,23 +127,15 @@ public :
|
|||
/// \return Center of the view
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
const sf::Vector2f& GetCenter() const;
|
||||
const Vector2f& GetCenter() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the half-size of the view
|
||||
/// Get the size of the view
|
||||
///
|
||||
/// \return Half-size of the view
|
||||
/// \return Size of the view
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
const sf::Vector2f& GetHalfSize() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the bounding rectangle of the view
|
||||
///
|
||||
/// \return Bounding rectangle of the view
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
const sf::FloatRect& GetRect() const;
|
||||
const Vector2f& GetSize() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the view (take 2 values)
|
||||
|
@ -144,7 +152,7 @@ public :
|
|||
/// \param Offset : Offset to move the view
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Move(const sf::Vector2f& Offset);
|
||||
void Move(const Vector2f& Offset);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Resize the view rectangle to simulate a zoom / unzoom effect
|
||||
|
@ -154,32 +162,33 @@ public :
|
|||
////////////////////////////////////////////////////////////
|
||||
void Zoom(float Factor);
|
||||
|
||||
private :
|
||||
|
||||
friend class RenderTarget;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the projection matrix of the view
|
||||
///
|
||||
/// \return Projection matrix containing the view settings
|
||||
/// \return Projection matrix
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
const Matrix3& GetMatrix() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Recompute the view rectangle and the projection matrix
|
||||
/// Get the inverse projection matrix of the view
|
||||
///
|
||||
/// \return Inverse projection matrix
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void RecomputeMatrix();
|
||||
const Matrix3& GetInverseMatrix() const;
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
sf::Vector2f myCenter; ///< Center of the view
|
||||
sf::Vector2f myHalfSize; ///< Half-size of the view
|
||||
FloatRect myRect; ///< Rectangle defining the bounds of the view
|
||||
Matrix3 myMatrix; ///< Precomputed projection matrix corresponding to the view
|
||||
bool myNeedUpdate; ///< Internal state telling if the matrix needs to be updated
|
||||
sf::Vector2f myCenter; ///< Center of the view
|
||||
sf::Vector2f mySize; ///< Size of the view
|
||||
mutable Matrix3 myMatrix; ///< Precomputed projection matrix corresponding to the view
|
||||
mutable Matrix3 myInverseMatrix; ///< Precomputed inverse projection matrix corresponding to the view
|
||||
mutable bool myNeedUpdate; ///< Internal state telling if the matrix needs to be updated
|
||||
mutable bool myNeedInvUpdate; ///< Internal state telling if the matrix needs to be updated
|
||||
};
|
||||
|
||||
} // namespace sf
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue