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
|
@ -381,7 +381,7 @@ void Drawable::Draw(RenderTarget& Target) const
|
|||
|
||||
// We have to use glBlendFuncSeparate so that the resulting alpha is
|
||||
// not alpha², which is incorrect and would cause problems when rendering
|
||||
// alpha pixels to a RenderImage that would be in turn rendered to another render target
|
||||
// alpha pixels to a RenderImage that would be in turn be rendered to another render target
|
||||
switch (myBlendMode)
|
||||
{
|
||||
case Blend::Alpha :
|
||||
|
|
|
@ -288,7 +288,7 @@ void Image::Copy(const Image& Source, unsigned int DestX, unsigned int DestY, co
|
|||
|
||||
// Adjust the source rectangle
|
||||
IntRect SrcRect = SourceRect;
|
||||
if (SrcRect.GetWidth() == 0 || (SrcRect.GetHeight() == 0))
|
||||
if (SrcRect.GetSize().x == 0 || (SrcRect.GetSize().y == 0))
|
||||
{
|
||||
SrcRect.Left = 0;
|
||||
SrcRect.Top = 0;
|
||||
|
@ -304,8 +304,8 @@ void Image::Copy(const Image& Source, unsigned int DestX, unsigned int DestY, co
|
|||
}
|
||||
|
||||
// Then find the valid bounds of the destination rectangle
|
||||
int Width = SrcRect.GetWidth();
|
||||
int Height = SrcRect.GetHeight();
|
||||
int Width = SrcRect.GetSize().x;
|
||||
int Height = SrcRect.GetSize().y;
|
||||
if (DestX + Width > myWidth) Width = myWidth - DestX;
|
||||
if (DestY + Height > myHeight) Height = myHeight - DestY;
|
||||
|
||||
|
@ -368,7 +368,7 @@ bool Image::CopyScreen(RenderWindow& Window, const IntRect& SourceRect)
|
|||
{
|
||||
// Adjust the source rectangle
|
||||
IntRect SrcRect = SourceRect;
|
||||
if (SrcRect.GetWidth() == 0 || (SrcRect.GetHeight() == 0))
|
||||
if (SrcRect.GetSize().x == 0 || (SrcRect.GetSize().y == 0))
|
||||
{
|
||||
SrcRect.Left = 0;
|
||||
SrcRect.Top = 0;
|
||||
|
@ -384,8 +384,8 @@ bool Image::CopyScreen(RenderWindow& Window, const IntRect& SourceRect)
|
|||
}
|
||||
|
||||
// Store the texture dimensions
|
||||
myWidth = SrcRect.GetWidth();
|
||||
myHeight = SrcRect.GetHeight();
|
||||
myWidth = SrcRect.GetSize().x;
|
||||
myHeight = SrcRect.GetSize().y;
|
||||
|
||||
// We can then create the texture
|
||||
if (Window.SetActive() && CreateTexture())
|
||||
|
|
|
@ -175,7 +175,7 @@ void RenderTarget::Initialize()
|
|||
SetRenderStates();
|
||||
|
||||
// Setup the default view
|
||||
myDefaultView.SetFromRect(FloatRect(0, 0, static_cast<float>(GetWidth()), static_cast<float>(GetHeight())));
|
||||
myDefaultView.SetRect(FloatRect(0, 0, static_cast<float>(GetWidth()), static_cast<float>(GetHeight())));
|
||||
SetView(myDefaultView);
|
||||
|
||||
Activate(false);
|
||||
|
|
|
@ -142,13 +142,15 @@ sf::Vector2f RenderWindow::ConvertCoords(unsigned int WindowX, unsigned int Wind
|
|||
if (!TargetView)
|
||||
TargetView = &GetView();
|
||||
|
||||
float Left = TargetView->GetCenter().x - TargetView->GetHalfSize().x;
|
||||
float Top = TargetView->GetCenter().y - TargetView->GetHalfSize().y;
|
||||
float Right = TargetView->GetCenter().x + TargetView->GetHalfSize().x;
|
||||
float Bottom = TargetView->GetCenter().y + TargetView->GetHalfSize().y;
|
||||
// First, convert from viewport coordinates to homogeneous coordinates:
|
||||
// --> [0, Width] to [-1, 1]
|
||||
// --> [0, Height] to [1, -1]
|
||||
Vector2f Coords;
|
||||
Coords.x = -1.f + 2.f * WindowX / GetWidth();
|
||||
Coords.y = 1.f - 2.f * WindowY / GetHeight();
|
||||
|
||||
return sf::Vector2f(Left + WindowX * (Right - Left) / GetWidth(),
|
||||
Top + WindowY * (Bottom - Top) / GetHeight());
|
||||
// Then transform by the inverse of the view matrix
|
||||
return TargetView->GetInverseMatrix().Transform(Coords);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -88,8 +88,8 @@ void Sprite::SetSubRect(const IntRect& SubRect)
|
|||
////////////////////////////////////////////////////////////
|
||||
void Sprite::Resize(float Width, float Height)
|
||||
{
|
||||
int LocalWidth = mySubRect.GetWidth();
|
||||
int LocalHeight = mySubRect.GetHeight();
|
||||
int LocalWidth = mySubRect.GetSize().x;
|
||||
int LocalHeight = mySubRect.GetSize().y;
|
||||
|
||||
if ((LocalWidth > 0) && (LocalHeight > 0))
|
||||
SetScale(Width / LocalWidth, Height / LocalHeight);
|
||||
|
@ -147,7 +147,7 @@ const IntRect& Sprite::GetSubRect() const
|
|||
////////////////////////////////////////////////////////////
|
||||
Vector2f Sprite::GetSize() const
|
||||
{
|
||||
return Vector2f(mySubRect.GetWidth() * GetScale().x, mySubRect.GetHeight() * GetScale().y);
|
||||
return Vector2f(mySubRect.GetSize().x * GetScale().x, mySubRect.GetSize().y * GetScale().y);
|
||||
}
|
||||
|
||||
|
||||
|
@ -162,8 +162,8 @@ Color Sprite::GetPixel(unsigned int X, unsigned int Y) const
|
|||
unsigned int ImageX = mySubRect.Left + X;
|
||||
unsigned int ImageY = mySubRect.Top + Y;
|
||||
|
||||
if (myIsFlippedX) ImageX = mySubRect.GetWidth() - ImageX - 1;
|
||||
if (myIsFlippedY) ImageY = mySubRect.GetHeight() - ImageY - 1;
|
||||
if (myIsFlippedX) ImageX = mySubRect.GetSize().x - ImageX - 1;
|
||||
if (myIsFlippedY) ImageY = mySubRect.GetSize().y - ImageY - 1;
|
||||
|
||||
return myImage->GetPixel(ImageX, ImageY) * GetColor();
|
||||
}
|
||||
|
@ -180,8 +180,8 @@ Color Sprite::GetPixel(unsigned int X, unsigned int Y) const
|
|||
void Sprite::Render(RenderTarget&) const
|
||||
{
|
||||
// Get the sprite size
|
||||
float Width = static_cast<float>(mySubRect.GetWidth());
|
||||
float Height = static_cast<float>(mySubRect.GetHeight());
|
||||
float Width = static_cast<float>(mySubRect.GetSize().x);
|
||||
float Height = static_cast<float>(mySubRect.GetSize().y);
|
||||
|
||||
// Check if the image is valid
|
||||
if (myImage && (myImage->GetWidth() > 0) && (myImage->GetHeight() > 0))
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Graphics/View.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace sf
|
||||
|
@ -34,102 +33,116 @@ namespace sf
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Construct the view from a rectangle
|
||||
////////////////////////////////////////////////////////////
|
||||
View::View(const FloatRect& ViewRect)
|
||||
View::View(const FloatRect& Rect)
|
||||
{
|
||||
SetFromRect(ViewRect);
|
||||
SetRect(Rect);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the view from its center and half-size
|
||||
/// Construct the view from its center and size
|
||||
////////////////////////////////////////////////////////////
|
||||
View::View(const sf::Vector2f& Center, const sf::Vector2f& HalfSize) :
|
||||
myCenter (Center),
|
||||
myHalfSize (HalfSize),
|
||||
myNeedUpdate(true)
|
||||
View::View(const Vector2f& Center, const Vector2f& Size) :
|
||||
myCenter (Center),
|
||||
mySize (Size),
|
||||
myNeedUpdate (true),
|
||||
myNeedInvUpdate(true)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Set the rectangle defining the view by its center and size
|
||||
////////////////////////////////////////////////////////////
|
||||
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)
|
||||
{
|
||||
myCenter.x = X;
|
||||
myCenter.y = Y;
|
||||
myNeedUpdate = true;
|
||||
myCenter.x = X;
|
||||
myCenter.y = Y;
|
||||
myNeedUpdate = true;
|
||||
myNeedInvUpdate = true;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change the center of the view (take a vector)
|
||||
////////////////////////////////////////////////////////////
|
||||
void View::SetCenter(const sf::Vector2f& Center)
|
||||
void View::SetCenter(const Vector2f& Center)
|
||||
{
|
||||
SetCenter(Center.x, Center.y);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change the half-size of the view (take 2 values)
|
||||
/// Change the size of the view (take 2 values)
|
||||
////////////////////////////////////////////////////////////
|
||||
void View::SetHalfSize(float HalfWidth, float HalfHeight)
|
||||
void View::SetSize(float Width, float Height)
|
||||
{
|
||||
myHalfSize.x = HalfWidth;
|
||||
myHalfSize.y = HalfHeight;
|
||||
myNeedUpdate = true;
|
||||
mySize.x = Width;
|
||||
mySize.y = Height;
|
||||
myNeedUpdate = true;
|
||||
myNeedInvUpdate = true;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Change the half-size of the view (take a vector)
|
||||
/// Change the size of the view (take a vector)
|
||||
////////////////////////////////////////////////////////////
|
||||
void View::SetHalfSize(const sf::Vector2f& HalfSize)
|
||||
void View::SetSize(const Vector2f& Size)
|
||||
{
|
||||
SetHalfSize(HalfSize.x, HalfSize.y);
|
||||
SetSize(Size.x, Size.y);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Rebuild the view from a rectangle
|
||||
/// Get the rectangle defining the view,
|
||||
/// which is the combined center and size.
|
||||
////////////////////////////////////////////////////////////
|
||||
void View::SetFromRect(const FloatRect& ViewRect)
|
||||
FloatRect View::GetRect() const
|
||||
{
|
||||
SetCenter( (ViewRect.Right + ViewRect.Left) / 2, (ViewRect.Bottom + ViewRect.Top) / 2);
|
||||
SetHalfSize((ViewRect.Right - ViewRect.Left) / 2, (ViewRect.Bottom - ViewRect.Top) / 2);
|
||||
return FloatRect(myCenter.x - mySize.x / 2,
|
||||
myCenter.y - mySize.y / 2,
|
||||
myCenter.x + mySize.x / 2,
|
||||
myCenter.y + mySize.y / 2);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the center of the view
|
||||
////////////////////////////////////////////////////////////
|
||||
const sf::Vector2f& View::GetCenter() const
|
||||
const Vector2f& View::GetCenter() const
|
||||
{
|
||||
return myCenter;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the half-size of the view
|
||||
/// Get the size of the view
|
||||
////////////////////////////////////////////////////////////
|
||||
const sf::Vector2f& View::GetHalfSize() const
|
||||
const Vector2f& View::GetSize() const
|
||||
{
|
||||
return myHalfSize;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the bounding rectangle of the view
|
||||
////////////////////////////////////////////////////////////
|
||||
const sf::FloatRect& View::GetRect() const
|
||||
{
|
||||
// Recompute it if needed
|
||||
if (myNeedUpdate)
|
||||
const_cast<View*>(this)->RecomputeMatrix();
|
||||
|
||||
return myRect;
|
||||
return mySize;
|
||||
}
|
||||
|
||||
|
||||
|
@ -138,16 +151,17 @@ const sf::FloatRect& View::GetRect() const
|
|||
////////////////////////////////////////////////////////////
|
||||
void View::Move(float OffsetX, float OffsetY)
|
||||
{
|
||||
myCenter.x += OffsetX;
|
||||
myCenter.y += OffsetY;
|
||||
myNeedUpdate = true;
|
||||
myCenter.x += OffsetX;
|
||||
myCenter.y += OffsetY;
|
||||
myNeedUpdate = true;
|
||||
myNeedInvUpdate = true;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the view (take a vector)
|
||||
////////////////////////////////////////////////////////////
|
||||
void View::Move(const sf::Vector2f& Offset)
|
||||
void View::Move(const Vector2f& Offset)
|
||||
{
|
||||
Move(Offset.x, Offset.y);
|
||||
}
|
||||
|
@ -160,8 +174,9 @@ void View::Zoom(float Factor)
|
|||
{
|
||||
if (Factor != 0)
|
||||
{
|
||||
myHalfSize /= Factor;
|
||||
myNeedUpdate = true;
|
||||
mySize /= Factor;
|
||||
myNeedUpdate = true;
|
||||
myNeedInvUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -173,36 +188,28 @@ const Matrix3& View::GetMatrix() const
|
|||
{
|
||||
// Recompute the matrix if needed
|
||||
if (myNeedUpdate)
|
||||
const_cast<View*>(this)->RecomputeMatrix();
|
||||
{
|
||||
myMatrix.SetFromProjection(GetRect());
|
||||
myNeedUpdate = false;
|
||||
}
|
||||
|
||||
return myMatrix;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Recompute the view rectangle and the projection matrix
|
||||
/// Get the inverse projection matrix of the view
|
||||
////////////////////////////////////////////////////////////
|
||||
void View::RecomputeMatrix()
|
||||
const Matrix3& View::GetInverseMatrix() const
|
||||
{
|
||||
// Compute the 4 corners of the view
|
||||
float Left = myCenter.x - myHalfSize.x;
|
||||
float Top = myCenter.y - myHalfSize.y;
|
||||
float Right = myCenter.x + myHalfSize.x;
|
||||
float Bottom = myCenter.y + myHalfSize.y;
|
||||
// Recompute the matrix if needed
|
||||
if (myNeedInvUpdate)
|
||||
{
|
||||
myInverseMatrix = GetMatrix().GetInverse();
|
||||
myNeedInvUpdate = false;
|
||||
}
|
||||
|
||||
// Update the view rectangle - be careful, reversed views are allowed !
|
||||
myRect.Left = std::min(Left, Right);
|
||||
myRect.Top = std::min(Top, Bottom);
|
||||
myRect.Right = std::max(Left, Right);
|
||||
myRect.Bottom = std::max(Top, Bottom);
|
||||
|
||||
// Update the projection matrix
|
||||
myMatrix(0, 0) = 2.f / (Right - Left);
|
||||
myMatrix(1, 1) = 2.f / (Top - Bottom);
|
||||
myMatrix(0, 2) = (Left + Right) / (Left - Right);
|
||||
myMatrix(1, 2) = (Bottom + Top) / (Bottom - Top);
|
||||
|
||||
myNeedUpdate = false;
|
||||
return myInverseMatrix;
|
||||
}
|
||||
|
||||
} // namespace sf
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue