FS#10 - Add rotation to sf::View

Slightly changed the public interface of sf::View

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1152 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2009-06-21 21:23:30 +00:00
parent 5978d015eb
commit 1bb96087ad
7 changed files with 141 additions and 98 deletions

View File

@ -72,10 +72,12 @@ public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Build a matrix from a projection /// Build a matrix from a projection
/// ///
/// \param Rectangle : Rectangle defining the region to project /// \param Center : Center of the view
/// \param Size : Size of the view
/// \param Rotation : Angle of rotation of the view rectangle, in degrees
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void SetFromProjection(const FloatRect& Rectangle); void SetFromProjection(const Vector2f& Center, const Vector2f& Size, float Rotation);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Transform a point by the matrix /// Transform a point by the matrix

View File

@ -76,17 +76,24 @@ inline void Matrix3::SetFromTransformations(const Vector2f& Center, const Vector
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Build a matrix from a projection /// Build a matrix from a projection
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
inline void Matrix3::SetFromProjection(const FloatRect& Rectangle) inline void Matrix3::SetFromProjection(const Vector2f& Center, const Vector2f& Size, float Rotation)
{ {
// Rotation components
float Angle = Rotation * 3.141592654f / 180.f;
float Cos = static_cast<float>(cos(Angle));
float Sin = static_cast<float>(sin(Angle));
float Tx = -Center.x * Cos - Center.y * Sin + Center.x;
float Ty = Center.x * Sin - Center.y * Cos + Center.y;
// Projection components // Projection components
float A = 2.f / Rectangle.GetSize().x; float A = 2.f / Size.x;
float B = -2.f / Rectangle.GetSize().y; float B = -2.f / Size.y;
float C = -(Rectangle.Left + Rectangle.Right) / Rectangle.GetSize().x; float C = -A * Center.x;
float D = (Rectangle.Bottom + Rectangle.Top) / Rectangle.GetSize().y; float D = -B * Center.y;
// Rebuild the projection matrix // Rebuild the projection matrix
myData[0] = A; myData[4] = 0.f; myData[8] = 0.f; myData[12] = C; myData[0] = A * Cos; myData[4] = A * Sin; myData[8] = 0.f; myData[12] = A * Tx + C;
myData[1] = 0.f; myData[5] = B; myData[9] = 0.f; myData[13] = D; myData[1] = -B * Sin; myData[5] = B * Cos; myData[9] = 0.f; myData[13] = B * Ty + D;
myData[2] = 0.f; myData[6] = 0.f; myData[10] = 1.f; myData[14] = 0.f; 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; myData[3] = 0.f; myData[7] = 0.f; myData[11] = 0.f; myData[15] = 1.f;
} }

View File

@ -37,20 +37,33 @@
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// This class defines a view (position, size, etc.); /// This class defines a 2D view. A view is like a camera,
/// you can consider it as a 2D camera /// which defines what part of the scene is shown and
/// where it is shown.
/// A sf::View is defined by the following components:
/// its source rectangle in the scene, an optional rotation
/// to apply to the source rectangle, and the destination
/// rectangle where it will be displayed, in the render target
/// that it is attached to.
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
class SFML_API View class SFML_API View
{ {
public : public :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Construct the view from a rectangle /// Default constructor
/// /// Constructs a default view of (0, 0, 1000, 1000)
/// \param ViewRect : Rectangle defining the position and size of the view (1000x1000 by default)
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
explicit View(const FloatRect& ViewRect = FloatRect(0, 0, 1000, 1000)); View();
////////////////////////////////////////////////////////////
/// Construct the view from a rectangle
///
/// \param Rectangle : Rectangle defining the view
///
////////////////////////////////////////////////////////////
explicit View(const FloatRect& Rectangle);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Construct the view from its center and size /// Construct the view from its center and size
@ -62,24 +75,7 @@ public :
View(const Vector2f& Center, const Vector2f& Size); View(const Vector2f& Center, const Vector2f& Size);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Set the rectangle defining the view by its center and size /// Change the center of the view
///
/// \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)
/// ///
/// \param X : X coordinate of the new center /// \param X : X coordinate of the new center
/// \param Y : Y coordinate of the new center /// \param Y : Y coordinate of the new center
@ -88,7 +84,7 @@ public :
void SetCenter(float X, float Y); void SetCenter(float X, float Y);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Change the center of the view (take a vector) /// Change the center of the view
/// ///
/// \param Center : New center /// \param Center : New center
/// ///
@ -96,7 +92,7 @@ public :
void SetCenter(const Vector2f& Center); void SetCenter(const Vector2f& Center);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Change the size of the view (take 2 values) /// Change the size of the view
/// ///
/// \param Width : New width /// \param Width : New width
/// \param Height : New height /// \param Height : New height
@ -105,7 +101,7 @@ public :
void SetSize(float Width, float Height); void SetSize(float Width, float Height);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Change the size of the view (take a vector) /// Change the size of the view
/// ///
/// \param HalfSize : New half-size /// \param HalfSize : New half-size
/// ///
@ -113,13 +109,21 @@ public :
void SetSize(const Vector2f& Size); void SetSize(const Vector2f& Size);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Get the rectangle defining the view, /// Set the angle of rotation of the view
/// which is the combined center and size.
/// ///
/// \return View rectangle /// \param Angle : New angle, in degrees
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
FloatRect GetRect() const; void SetRotation(float Angle);
////////////////////////////////////////////////////////////
/// Reset the view to the given rectangle.
/// Note: this function resets the rotation angle to 0.
///
/// \param Rectangle : Rectangle defining the view
///
////////////////////////////////////////////////////////////
void Reset(const FloatRect& Rectangle);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Get the center of the view /// Get the center of the view
@ -138,7 +142,7 @@ public :
const Vector2f& GetSize() const; const Vector2f& GetSize() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Move the view (take 2 values) /// Move the view
/// ///
/// \param OffsetX : Offset to move the view, on X axis /// \param OffsetX : Offset to move the view, on X axis
/// \param OffsetY : Offset to move the view, on Y axis /// \param OffsetY : Offset to move the view, on Y axis
@ -147,17 +151,25 @@ public :
void Move(float OffsetX, float OffsetY); void Move(float OffsetX, float OffsetY);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Move the view (take a vector) /// Move the view
/// ///
/// \param Offset : Offset to move the view /// \param Offset : Offset to move the view
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Move(const Vector2f& Offset); void Move(const Vector2f& Offset);
////////////////////////////////////////////////////////////
/// Rotate the view
///
/// \param Angle : Angle to rotate, in degrees
///
////////////////////////////////////////////////////////////
void Rotate(float Angle);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Resize the view rectangle to simulate a zoom / unzoom effect /// Resize the view rectangle to simulate a zoom / unzoom effect
/// ///
/// \param Factor : Zoom factor to apply, relative to the current zoom /// \param Factor : Zoom factor to apply, relative to the current size
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Zoom(float Factor); void Zoom(float Factor);
@ -183,8 +195,9 @@ private :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
sf::Vector2f myCenter; ///< Center of the view Vector2f myCenter; ///< Center of the view, in scene coordinates
sf::Vector2f mySize; ///< Size of the view Vector2f mySize; ///< Size of the view, in scene coordinates
float myRotation; ///< Angle of rotation of the view rectangle, in degrees
mutable Matrix3 myMatrix; ///< Precomputed projection matrix corresponding to the view mutable Matrix3 myMatrix; ///< Precomputed projection matrix corresponding to the view
mutable Matrix3 myInverseMatrix; ///< Precomputed inverse 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 myNeedUpdate; ///< Internal state telling if the matrix needs to be updated

View File

@ -60,7 +60,7 @@ private :
// Adjust the size of the default view when the widget is resized // Adjust the size of the default view when the widget is resized
if (Event.Type == sf::Event::Resized) if (Event.Type == sf::Event::Resized)
{ {
GetDefaultView().SetRect(sf::FloatRect(0, 0, Event.Size.Width, Event.Size.Height)); GetDefaultView().Reset(sf::FloatRect(0, 0, Event.Size.Width, Event.Size.Height));
} }
} }

View File

@ -303,13 +303,16 @@ void PostFX::Render(RenderTarget& Target) const
IntRect FrameBufferRect(0, 0, myFrameBuffer.GetWidth(), myFrameBuffer.GetHeight()); IntRect FrameBufferRect(0, 0, myFrameBuffer.GetWidth(), myFrameBuffer.GetHeight());
FloatRect TexCoords = myFrameBuffer.GetTexCoords(FrameBufferRect); FloatRect TexCoords = myFrameBuffer.GetTexCoords(FrameBufferRect);
// Set the projection matrix to the identity so that the screen coordinates are in the range [-1, 1]
GLCheck(glMatrixMode(GL_PROJECTION));
GLCheck(glLoadIdentity());
// Render a fullscreen quad using the effect on our framebuffer // Render a fullscreen quad using the effect on our framebuffer
FloatRect Screen = Target.GetView().GetRect();
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(TexCoords.Left, TexCoords.Top); glVertex2f(Screen.Left, Screen.Top); glTexCoord2f(TexCoords.Left, TexCoords.Top); glVertex2f(-1, 1);
glTexCoord2f(TexCoords.Right, TexCoords.Top); glVertex2f(Screen.Right, Screen.Top); glTexCoord2f(TexCoords.Right, TexCoords.Top); glVertex2f( 1, 1);
glTexCoord2f(TexCoords.Right, TexCoords.Bottom); glVertex2f(Screen.Right, Screen.Bottom); glTexCoord2f(TexCoords.Right, TexCoords.Bottom); glVertex2f( 1, -1);
glTexCoord2f(TexCoords.Left, TexCoords.Bottom); glVertex2f(Screen.Left, Screen.Bottom); glTexCoord2f(TexCoords.Left, TexCoords.Bottom); glVertex2f(-1, -1);
glEnd(); glEnd();
// Disable program // Disable program

View File

@ -175,7 +175,7 @@ void RenderTarget::Initialize()
SetRenderStates(); SetRenderStates();
// Setup the default view // Setup the default view
myDefaultView.SetRect(FloatRect(0, 0, static_cast<float>(GetWidth()), static_cast<float>(GetHeight()))); myDefaultView.Reset(FloatRect(0, 0, static_cast<float>(GetWidth()), static_cast<float>(GetHeight())));
SetView(myDefaultView); SetView(myDefaultView);
Activate(false); Activate(false);

View File

@ -30,12 +30,32 @@
namespace sf namespace sf
{ {
////////////////////////////////////////////////////////////
/// Default constructor
/// Constructs a default view of (0, 0, 1000, 1000)
////////////////////////////////////////////////////////////
View::View() :
myCenter (),
mySize (),
myRotation (0),
myNeedUpdate (true),
myNeedInvUpdate(true)
{
Reset(FloatRect(0, 0, 1000, 1000));
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Construct the view from a rectangle /// Construct the view from a rectangle
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
View::View(const FloatRect& Rect) View::View(const FloatRect& Rectangle) :
myCenter (),
mySize (),
myRotation (0),
myNeedUpdate (true),
myNeedInvUpdate(true)
{ {
SetRect(Rect); Reset(Rectangle);
} }
@ -45,36 +65,15 @@ View::View(const FloatRect& Rect)
View::View(const Vector2f& Center, const Vector2f& Size) : View::View(const Vector2f& Center, const Vector2f& Size) :
myCenter (Center), myCenter (Center),
mySize (Size), mySize (Size),
myRotation (0),
myNeedUpdate (true), myNeedUpdate (true),
myNeedInvUpdate(true) myNeedInvUpdate(true)
{ {
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Set the rectangle defining the view by its center and size /// Change the center of the view
////////////////////////////////////////////////////////////
void View::SetRect(const Vector2f& Center, const Vector2f& Size)
{
myCenter = Center;
mySize = Size;
myNeedUpdate = true;
myNeedInvUpdate = true;
}
////////////////////////////////////////////////////////////
/// Set the rectangle defining the view
////////////////////////////////////////////////////////////
void View::SetRect(const FloatRect& Rect)
{
SetRect(Rect.GetCenter(), Rect.GetSize());
}
////////////////////////////////////////////////////////////
/// Change the center of the view (take 2 values)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::SetCenter(float X, float Y) void View::SetCenter(float X, float Y)
{ {
@ -86,7 +85,7 @@ void View::SetCenter(float X, float Y)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Change the center of the view (take a vector) /// Change the center of the view
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::SetCenter(const Vector2f& Center) void View::SetCenter(const Vector2f& Center)
{ {
@ -95,7 +94,7 @@ void View::SetCenter(const Vector2f& Center)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Change the size of the view (take 2 values) /// Change the size of the view
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::SetSize(float Width, float Height) void View::SetSize(float Width, float Height)
{ {
@ -107,7 +106,7 @@ void View::SetSize(float Width, float Height)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Change the size of the view (take a vector) /// Change the size of the view
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::SetSize(const Vector2f& Size) void View::SetSize(const Vector2f& Size)
{ {
@ -116,15 +115,25 @@ void View::SetSize(const Vector2f& Size)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Get the rectangle defining the view, /// Set the angle of rotation of the view
/// which is the combined center and size.
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
FloatRect View::GetRect() const void View::SetRotation(float Angle)
{ {
return FloatRect(myCenter.x - mySize.x / 2, myRotation = Angle;
myCenter.y - mySize.y / 2, myNeedUpdate = true;
myCenter.x + mySize.x / 2, myNeedInvUpdate = true;
myCenter.y + mySize.y / 2); }
////////////////////////////////////////////////////////////
/// Reset the view to the given rectangle
////////////////////////////////////////////////////////////
void View::Reset(const FloatRect& Rectangle)
{
myCenter = Rectangle.GetCenter();
mySize = Rectangle.GetSize();
myNeedUpdate = true;
myNeedInvUpdate = true;
} }
@ -147,7 +156,7 @@ const Vector2f& View::GetSize() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Move the view (take 2 values) /// Move the view
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::Move(float OffsetX, float OffsetY) void View::Move(float OffsetX, float OffsetY)
{ {
@ -159,7 +168,7 @@ void View::Move(float OffsetX, float OffsetY)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Move the view (take a vector) /// Move the view
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::Move(const Vector2f& Offset) void View::Move(const Vector2f& Offset)
{ {
@ -167,18 +176,27 @@ void View::Move(const Vector2f& Offset)
} }
////////////////////////////////////////////////////////////
/// Rotate the view
////////////////////////////////////////////////////////////
void View::Rotate(float Angle)
{
myRotation += Angle;
myNeedUpdate = true;
myNeedInvUpdate = true;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Resize the view rectangle to simulate a zoom / unzoom effect /// Resize the view rectangle to simulate a zoom / unzoom effect
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void View::Zoom(float Factor) void View::Zoom(float Factor)
{ {
if (Factor != 0) mySize.x *= Factor;
{ mySize.y *= Factor;
mySize /= Factor;
myNeedUpdate = true; myNeedUpdate = true;
myNeedInvUpdate = true; myNeedInvUpdate = true;
} }
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -189,7 +207,7 @@ const Matrix3& View::GetMatrix() const
// Recompute the matrix if needed // Recompute the matrix if needed
if (myNeedUpdate) if (myNeedUpdate)
{ {
myMatrix.SetFromProjection(GetRect()); myMatrix.SetFromProjection(myCenter, mySize, myRotation);
myNeedUpdate = false; myNeedUpdate = false;
} }