mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
*important* sf::Rect now uses Width/Height instead of Right/Bottom
Removed Offset, GetSize and GetCenter functions from sf::Rect Added a sf::Rect constructor taking two Vector2 parameters Updated the API documentation of the sf::Rect class git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1503 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
ae2ae15e12
commit
082a928555
@ -35,8 +35,8 @@
|
||||
namespace sf
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Rect is an utility class for manipulating rectangles.
|
||||
/// Template parameter defines the type of coordinates (integer, float, ...)
|
||||
/// \brief Utility class for manipulating 2D axis aligned rectangles
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
class Rect
|
||||
@ -44,94 +44,89 @@ class Rect
|
||||
public :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
/// \brief Default constructor
|
||||
///
|
||||
/// Creates an empty rectangle (it is equivalent to calling
|
||||
/// Rect(0, 0, 0, 0)).
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
Rect();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the rectangle from its coordinates
|
||||
/// \brief Construct the rectangle from its coordinates
|
||||
///
|
||||
/// \param left : Left coordinate of the rectangle
|
||||
/// \param top : Top coordinate of the rectangle
|
||||
/// \param right : Right coordinate of the rectangle
|
||||
/// \param bottom : Bottom coordinate of the rectangle
|
||||
/// Be careful, the last two parameters are the width
|
||||
/// and height, not the right and bottom coordinates!
|
||||
///
|
||||
/// \param left Left coordinate of the rectangle
|
||||
/// \param top Top coordinate of the rectangle
|
||||
/// \param width Width of the rectangle
|
||||
/// \param height Height of the rectangle
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
Rect(T left, T top, T right, T bottom);
|
||||
Rect(T left, T top, T width, T height);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the size of the rectangle
|
||||
/// \brief Construct the rectangle from position and size
|
||||
///
|
||||
/// \return Size of rectangle
|
||||
/// Be careful, the last parameter is the size,
|
||||
/// not the bottom-right corner!
|
||||
///
|
||||
/// \param position Position of the top-left corner of the rectangle
|
||||
/// \param size Size of the rectangle
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
Vector2<T> GetSize() const;
|
||||
Rect(const Vector2<T>& position, const Vector2<T>& size);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the center of the rectangle
|
||||
/// \brief Check if a point is inside the rectangle's area
|
||||
///
|
||||
/// \return Center of rectangle
|
||||
/// \param x X coordinate of the point to test
|
||||
/// \param y Y coordinate of the point to test
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
Vector2<T> GetCenter() const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the whole rectangle by the given offset
|
||||
/// \return True if the point is inside, false otherwise
|
||||
///
|
||||
/// \param offsetX : Horizontal offset
|
||||
/// \param offsetY : Vertical offset
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Offset(T offsetX, T offsetY);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the whole rectangle by the given offset
|
||||
///
|
||||
/// \param offset : Offset to apply to the current position
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Offset(const Vector2<T>& offset);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
///
|
||||
/// \param x : X coordinate of the point to test
|
||||
/// \param y : Y coordinate of the point to test
|
||||
///
|
||||
/// \return True if the point is inside
|
||||
/// \see Intersects
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Contains(T x, T y) const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
/// \brief Check if a point is inside the rectangle's area
|
||||
///
|
||||
/// \param point : Point to test
|
||||
/// \param point Point to test
|
||||
///
|
||||
/// \return True if the point is inside
|
||||
/// \return True if the point is inside, false otherwise
|
||||
///
|
||||
/// \see Intersects
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Contains(const Vector2<T>& point) const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles
|
||||
/// \brief Check the intersection between two rectangles
|
||||
///
|
||||
/// \param rectangle : Rectangle to test
|
||||
/// \param rectangle Rectangle to test
|
||||
///
|
||||
/// \return True if rectangles overlap
|
||||
/// \return True if rectangles overlap, false otherwise
|
||||
///
|
||||
/// \see Contains
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Intersects(const Rect<T>& rectangle) const;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles and return the
|
||||
/// resulting rectangle
|
||||
/// \brief Check the intersection between two rectangles
|
||||
///
|
||||
/// \param rectangle : Rectangle to test
|
||||
/// \param intersection : Rectangle to be filled with the intersection of both rectangles
|
||||
/// This overload returns the overlapped rectangle in the
|
||||
/// \a intersection parameter.
|
||||
///
|
||||
/// \return True if rectangles overlap
|
||||
/// \param rectangle Rectangle to test
|
||||
/// \param intersection Rectangle to be filled with the intersection
|
||||
///
|
||||
/// \return True if rectangles overlap, false otherwise
|
||||
///
|
||||
/// \see Contains
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Intersects(const Rect<T>& rectangle, Rect<T>& intersection) const;
|
||||
@ -141,13 +136,13 @@ public :
|
||||
////////////////////////////////////////////////////////////
|
||||
T Left; ///< Left coordinate of the rectangle
|
||||
T Top; ///< Top coordinate of the rectangle
|
||||
T Right; ///< Right coordinate of the rectangle
|
||||
T Bottom; ///< Bottom coordinate of the rectangle
|
||||
T Width; ///< Width of the rectangle
|
||||
T Height; ///< Height of the rectangle
|
||||
};
|
||||
|
||||
#include <SFML/Graphics/Rect.inl>
|
||||
|
||||
// Define the most common types
|
||||
// Create typedefs for the most common types
|
||||
typedef Rect<int> IntRect;
|
||||
typedef Rect<float> FloatRect;
|
||||
|
||||
@ -155,3 +150,54 @@ typedef Rect<float> FloatRect;
|
||||
|
||||
|
||||
#endif // SFML_RECT_HPP
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \class sf::Rect
|
||||
///
|
||||
/// A rectangle is defined by its top-left corner and its size.
|
||||
/// It is a very simple class defined for convenience, so
|
||||
/// its member variables (Left, Top, Width and Height) are public
|
||||
/// and can be accessed directly, just like the vector classes
|
||||
/// (Vector2 and Vector3).
|
||||
///
|
||||
/// To keep things simple, sf::Rect doesn't define
|
||||
/// functions to emulate the properties that are not directly
|
||||
/// members (such as Right, Bottom, Center, etc.), it rather
|
||||
/// only provides intersection functions.
|
||||
///
|
||||
/// sf::Rect uses the usual rules for its boundaries:
|
||||
/// \li The Left and Top edges are included in the rectangle's area
|
||||
/// \li The right (Left + Width) and bottom (Top + Height) edges are excluded from the rectangle's area
|
||||
///
|
||||
/// This means that sf::IntRect(0, 0, 1, 1) and sf::IntRect(1, 1, 1, 1)
|
||||
/// don't intersect.
|
||||
///
|
||||
/// sf::Rect is a template and may be used with any numeric type, but
|
||||
/// for simplicity the instanciations used by SFML are typedefed:
|
||||
/// \li sf::Rect<int> is sf::IntRect
|
||||
/// \li sf::Rect<float> is sf::FloatRect
|
||||
///
|
||||
/// So that you don't have to care about the template syntax.
|
||||
///
|
||||
/// Usage example:
|
||||
/// \code
|
||||
/// // Define a rectangle, located at (0, 0) with a size of 20x5
|
||||
/// sf::IntRect r1(0, 0, 20, 5);
|
||||
///
|
||||
/// // Define another rectangle, located at (4, 2) with a size of 18x10
|
||||
/// sf::Vector2i position(4, 2);
|
||||
/// sf::Vector2i size(18, 10);
|
||||
/// sf::IntRect r2(position, size);
|
||||
///
|
||||
/// // Test intersections with the point (3, 1)
|
||||
/// bool b1 = r1.Contains(3, 1); // true
|
||||
/// bool b2 = r2.Contains(3, 1); // false
|
||||
///
|
||||
/// // Test the intersection between r1 and r2
|
||||
/// sf::IntRect result;
|
||||
/// bool b3 = r1.Intersects(r2, result); // true
|
||||
/// // result == (4, 2, 16, 3)
|
||||
/// \endcode
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -23,89 +23,50 @@
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Default constructor
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
Rect<T>::Rect() :
|
||||
Left (0),
|
||||
Top (0),
|
||||
Right (0),
|
||||
Bottom(0)
|
||||
Width (0),
|
||||
Height(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Construct the color from its coordinates
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
Rect<T>::Rect(T left, T top, T right, T bottom) :
|
||||
Rect<T>::Rect(T left, T top, T width, T height) :
|
||||
Left (left),
|
||||
Top (top),
|
||||
Right (right),
|
||||
Bottom(bottom)
|
||||
Width (width),
|
||||
Height(height)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the size of the rectangle
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
Vector2<T> Rect<T>::GetSize() const
|
||||
Rect<T>::Rect(const Vector2<T>& position, const Vector2<T>& size) :
|
||||
Left (position.x),
|
||||
Top (position.y),
|
||||
Width (size.x),
|
||||
Height(size.y)
|
||||
{
|
||||
return Vector2<T>(Right - Left, Bottom - Top);
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Get the center of the rectangle
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
Vector2<T> Rect<T>::GetCenter() const
|
||||
{
|
||||
return Vector2<T>((Left + Right) / 2, (Top + Bottom) / 2);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the whole rectangle by the given offset
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
void Rect<T>::Offset(T offsetX, T offsetY)
|
||||
{
|
||||
Left += offsetX;
|
||||
Right += offsetX;
|
||||
Top += offsetY;
|
||||
Bottom += offsetY;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Move the whole rectangle by the given offset
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
void Rect<T>::Offset(const Vector2<T>& offset)
|
||||
{
|
||||
Offset(offset.x, offset.y);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool Rect<T>::Contains(T x, T y) const
|
||||
{
|
||||
return (x >= Left) && (x <= Right) && (y >= Top) && (y <= Bottom);
|
||||
return (x >= Left) && (x < Left + Width) && (y >= Top) && (y < Top + Height);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check if a point is inside the rectangle's area
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool Rect<T>::Contains(const Vector2<T>& point) const
|
||||
@ -114,40 +75,29 @@ bool Rect<T>::Contains(const Vector2<T>& point) const
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool Rect<T>::Intersects(const Rect<T>& rectangle) 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
|
||||
return (overlapping.Left < overlapping.Right) && (overlapping.Top < overlapping.Bottom);
|
||||
Rect<T> intersection;
|
||||
return Intersects(rectangle, intersection);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Check intersection between two rectangles and return the
|
||||
/// resulting rectangle
|
||||
////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
bool Rect<T>::Intersects(const Rect<T>& rectangle, Rect<T>& intersection) 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));
|
||||
// Compute the intersection boundaries
|
||||
T left = std::max(Left, rectangle.Left);
|
||||
T top = std::max(Top, rectangle.Top);
|
||||
T right = std::min(Left + Width, rectangle.Left + rectangle.Width);
|
||||
T bottom = std::min(Top + Height, rectangle.Top + rectangle.Height);
|
||||
|
||||
// If overlapping rect is valid, then there is intersection
|
||||
if ((overlapping.Left < overlapping.Right) && (overlapping.Top < overlapping.Bottom))
|
||||
// If the intersection is valid (positive non zero area), then there is an intersection
|
||||
if ((left < right) && (top < bottom))
|
||||
{
|
||||
intersection = overlapping;
|
||||
intersection = Rect<T>(left, top, right - left, bottom - top);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
@ -320,8 +320,8 @@ private :
|
||||
/// sf::RenderWindow window;
|
||||
/// sf::View view;
|
||||
///
|
||||
/// // Initialize the view to a rectangle going from (100, 100) to (500, 300)
|
||||
/// view.Reset(sf::FloatRect(100, 100, 500, 300));
|
||||
/// // Initialize the view to a rectangle located at (100, 100) and with a size of 400x200
|
||||
/// view.Reset(sf::FloatRect(100, 100, 400, 200));
|
||||
///
|
||||
/// // Rotate it by 45 degrees
|
||||
/// view.Rotate(45);
|
||||
|
@ -39,7 +39,7 @@ namespace sf
|
||||
class IpAddress;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \li A FTP client
|
||||
/// \brief A FTP client
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
class SFML_API Ftp : NonCopyable
|
||||
|
@ -369,8 +369,8 @@ Glyph Font::LoadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) c
|
||||
// Compute the glyph's bounding box
|
||||
glyph.Bounds.Left = bitmapGlyph->left - padding;
|
||||
glyph.Bounds.Top = -bitmapGlyph->top - padding;
|
||||
glyph.Bounds.Right = bitmapGlyph->left + width + padding;
|
||||
glyph.Bounds.Bottom = -bitmapGlyph->top + height + padding;
|
||||
glyph.Bounds.Width = width + 2 * padding;
|
||||
glyph.Bounds.Height = height + 2 * padding;
|
||||
|
||||
// Extract the glyph's pixels from the bitmap
|
||||
myPixelBuffer.resize(width * height * 4, 255);
|
||||
@ -408,8 +408,8 @@ Glyph Font::LoadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) c
|
||||
IntRect subrect = glyph.SubRect;
|
||||
subrect.Left += padding;
|
||||
subrect.Top += padding;
|
||||
subrect.Right -= padding;
|
||||
subrect.Bottom -= padding;
|
||||
subrect.Width -= 2 * padding;
|
||||
subrect.Height -= 2 * padding;
|
||||
page.Texture.UpdatePixels(&myPixelBuffer[0], subrect);
|
||||
}
|
||||
|
||||
@ -481,7 +481,7 @@ IntRect Font::FindGlyphRect(Page& page, unsigned int width, unsigned int height)
|
||||
}
|
||||
|
||||
// Find the glyph's rectangle on the selected row
|
||||
IntRect rect(row->Width, row->Top, row->Width + width, row->Top + height);
|
||||
IntRect rect(row->Width, row->Top, width, height);
|
||||
|
||||
// Update the row informations
|
||||
row->Width += width;
|
||||
|
@ -272,24 +272,24 @@ void Image::Copy(const Image& source, unsigned int destX, unsigned int destY, co
|
||||
|
||||
// Adjust the source rectangle
|
||||
IntRect srcRect = sourceRect;
|
||||
if (srcRect.GetSize().x == 0 || (srcRect.GetSize().y == 0))
|
||||
if (srcRect.Width == 0 || (srcRect.Height == 0))
|
||||
{
|
||||
srcRect.Left = 0;
|
||||
srcRect.Top = 0;
|
||||
srcRect.Right = source.myWidth;
|
||||
srcRect.Bottom = source.myHeight;
|
||||
srcRect.Width = source.myWidth;
|
||||
srcRect.Height = source.myHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (srcRect.Left < 0) srcRect.Left = 0;
|
||||
if (srcRect.Top < 0) srcRect.Top = 0;
|
||||
if (srcRect.Right > static_cast<int>(source.myWidth)) srcRect.Right = source.myWidth;
|
||||
if (srcRect.Bottom > static_cast<int>(source.myHeight)) srcRect.Bottom = source.myHeight;
|
||||
if (srcRect.Width > static_cast<int>(source.myWidth)) srcRect.Width = source.myWidth;
|
||||
if (srcRect.Height > static_cast<int>(source.myHeight)) srcRect.Height = source.myHeight;
|
||||
}
|
||||
|
||||
// Then find the valid bounds of the destination rectangle
|
||||
int width = srcRect.GetSize().x;
|
||||
int height = srcRect.GetSize().y;
|
||||
int width = srcRect.Width;
|
||||
int height = srcRect.Height;
|
||||
if (destX + width > myWidth) width = myWidth - destX;
|
||||
if (destY + height > myHeight) height = myHeight - destY;
|
||||
|
||||
@ -353,24 +353,24 @@ bool Image::CopyScreen(RenderWindow& window, const IntRect& sourceRect)
|
||||
{
|
||||
// Adjust the source rectangle
|
||||
IntRect srcRect = sourceRect;
|
||||
if (srcRect.GetSize().x == 0 || (srcRect.GetSize().y == 0))
|
||||
if (srcRect.Width == 0 || (srcRect.Height == 0))
|
||||
{
|
||||
srcRect.Left = 0;
|
||||
srcRect.Top = 0;
|
||||
srcRect.Right = window.GetWidth();
|
||||
srcRect.Bottom = window.GetHeight();
|
||||
srcRect.Width = window.GetWidth();
|
||||
srcRect.Height = window.GetHeight();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (srcRect.Left < 0) srcRect.Left = 0;
|
||||
if (srcRect.Top < 0) srcRect.Top = 0;
|
||||
if (srcRect.Right > static_cast<int>(window.GetWidth())) srcRect.Right = window.GetWidth();
|
||||
if (srcRect.Bottom > static_cast<int>(window.GetHeight())) srcRect.Bottom = window.GetHeight();
|
||||
if (srcRect.Width > static_cast<int>(window.GetWidth())) srcRect.Width = window.GetWidth();
|
||||
if (srcRect.Height > static_cast<int>(window.GetHeight())) srcRect.Height = window.GetHeight();
|
||||
}
|
||||
|
||||
// Store the texture dimensions
|
||||
myWidth = srcRect.GetSize().x;
|
||||
myHeight = srcRect.GetSize().y;
|
||||
myWidth = srcRect.Width;
|
||||
myHeight = srcRect.Height;
|
||||
|
||||
// We can then create the texture
|
||||
if (window.SetActive() && CreateTexture())
|
||||
@ -486,7 +486,7 @@ void Image::UpdatePixels(const Uint8* pixels, const IntRect& rectangle)
|
||||
|
||||
// Update the texture from the array of pixels
|
||||
GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture));
|
||||
GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, rectangle.Left, rectangle.Top, rectangle.GetSize().x, rectangle.GetSize().y, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
|
||||
GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, GL_RGBA, GL_UNSIGNED_BYTE, pixels));
|
||||
|
||||
GLCheck(glBindTexture(GL_TEXTURE_2D, previous));
|
||||
|
||||
@ -573,16 +573,16 @@ FloatRect Image::GetTexCoords(const IntRect& rect) const
|
||||
if (myPixelsFlipped)
|
||||
{
|
||||
return FloatRect(rect.Left / width,
|
||||
rect.Bottom / height,
|
||||
rect.Right / width,
|
||||
rect.Height / height,
|
||||
rect.Width / width,
|
||||
rect.Top / height);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FloatRect(rect.Left / width,
|
||||
rect.Top / height,
|
||||
rect.Right / width,
|
||||
rect.Bottom / height);
|
||||
rect.Width / width,
|
||||
rect.Height / height);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -153,8 +153,8 @@ IntRect RenderTarget::GetViewport(const View& view) const
|
||||
|
||||
return IntRect(static_cast<int>(0.5f + width * viewport.Left),
|
||||
static_cast<int>(0.5f + height * viewport.Top),
|
||||
static_cast<int>(0.5f + width * viewport.Right),
|
||||
static_cast<int>(0.5f + height * viewport.Bottom));
|
||||
static_cast<int>(width * viewport.Width),
|
||||
static_cast<int>(height * viewport.Height));
|
||||
}
|
||||
|
||||
|
||||
@ -171,8 +171,8 @@ Vector2f RenderTarget::ConvertCoords(unsigned int x, unsigned int y, const View&
|
||||
// First, convert from viewport coordinates to homogeneous coordinates
|
||||
Vector2f coords;
|
||||
IntRect viewport = GetViewport(view);
|
||||
coords.x = -1.f + 2.f * (static_cast<int>(x) - viewport.Left) / viewport.GetSize().x;
|
||||
coords.y = 1.f - 2.f * (static_cast<int>(y) - viewport.Top) / viewport.GetSize().y;
|
||||
coords.x = -1.f + 2.f * (static_cast<int>(x) - viewport.Left) / viewport.Width;
|
||||
coords.y = 1.f - 2.f * (static_cast<int>(y) - viewport.Top) / viewport.Height;
|
||||
|
||||
// Then transform by the inverse of the view matrix
|
||||
return view.GetInverseMatrix().Transform(coords);
|
||||
|
@ -173,12 +173,15 @@ void Renderer::ApplyColor(const Color& color)
|
||||
////////////////////////////////////////////////////////////
|
||||
void Renderer::SetViewport(const IntRect& viewport)
|
||||
{
|
||||
if ((viewport.Left != myViewport.Left) || (viewport.Right != myViewport.Right) ||
|
||||
(viewport.Top != myViewport.Top) || (viewport.Bottom != myViewport.Bottom) ||
|
||||
if ((viewport.Left != myViewport.Left) || (viewport.Width != myViewport.Width) ||
|
||||
(viewport.Top != myViewport.Top) || (viewport.Height != myViewport.Height) ||
|
||||
!myViewportIsValid)
|
||||
{
|
||||
// Apply the new viewport -- revert Y axis to match the OpenGL convention
|
||||
GLCheck(glViewport(viewport.Left, myTarget.GetHeight() - viewport.Bottom, viewport.GetSize().x, viewport.GetSize().y));
|
||||
// Revert the Y axis to match the OpenGL convention
|
||||
int top = myTarget.GetHeight() - (viewport.Top + viewport.Height);
|
||||
|
||||
// Apply the new viewport
|
||||
GLCheck(glViewport(viewport.Left, top, viewport.Width, viewport.Height));
|
||||
|
||||
// Store it
|
||||
myViewport = viewport;
|
||||
|
@ -93,11 +93,8 @@ void Sprite::SetSubRect(const IntRect& rectangle)
|
||||
////////////////////////////////////////////////////////////
|
||||
void Sprite::Resize(float width, float height)
|
||||
{
|
||||
int localWidth = mySubRect.GetSize().x;
|
||||
int localHeight = mySubRect.GetSize().y;
|
||||
|
||||
if ((localWidth > 0) && (localHeight > 0))
|
||||
SetScale(width / localWidth, height / localHeight);
|
||||
if ((mySubRect.Width > 0) && (mySubRect.Height > 0))
|
||||
SetScale(width / mySubRect.Width, height / mySubRect.Height);
|
||||
}
|
||||
|
||||
|
||||
@ -152,7 +149,7 @@ const IntRect& Sprite::GetSubRect() const
|
||||
////////////////////////////////////////////////////////////
|
||||
Vector2f Sprite::GetSize() const
|
||||
{
|
||||
return Vector2f(mySubRect.GetSize().x * GetScale().x, mySubRect.GetSize().y * GetScale().y);
|
||||
return Vector2f(mySubRect.Width * GetScale().x, mySubRect.Height * GetScale().y);
|
||||
}
|
||||
|
||||
|
||||
@ -167,8 +164,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.GetSize().x - imageX - 1;
|
||||
if (myIsFlippedY) imageY = mySubRect.GetSize().y - imageY - 1;
|
||||
if (myIsFlippedX) imageX = mySubRect.Width - imageX - 1;
|
||||
if (myIsFlippedY) imageY = mySubRect.Height - imageY - 1;
|
||||
|
||||
return myImage->GetPixel(imageX, imageY) * GetColor();
|
||||
}
|
||||
@ -185,27 +182,29 @@ Color Sprite::GetPixel(unsigned int x, unsigned int y) const
|
||||
void Sprite::Render(RenderTarget&, Renderer& renderer) const
|
||||
{
|
||||
// Get the sprite size
|
||||
float width = static_cast<float>(mySubRect.GetSize().x);
|
||||
float height = static_cast<float>(mySubRect.GetSize().y);
|
||||
float width = static_cast<float>(mySubRect.Width);
|
||||
float height = static_cast<float>(mySubRect.Height);
|
||||
|
||||
// Check if the image is valid, and calculate the texture coordinates
|
||||
FloatRect coords;
|
||||
if (myImage)
|
||||
{
|
||||
coords = myImage->GetTexCoords(mySubRect);
|
||||
if (myIsFlippedX) std::swap(coords.Left, coords.Right);
|
||||
if (myIsFlippedY) std::swap(coords.Top, coords.Bottom);
|
||||
if (myIsFlippedX) coords.Width = -coords.Width;
|
||||
if (myIsFlippedY) coords.Height = -coords.Height;
|
||||
}
|
||||
|
||||
// Bind the texture
|
||||
renderer.SetTexture(myImage);
|
||||
|
||||
// Draw the sprite's geometry
|
||||
float right = coords.Left + coords.Width;
|
||||
float bottom = coords.Top + coords.Height;
|
||||
renderer.Begin(Renderer::TriangleStrip);
|
||||
renderer.AddVertex(0, 0, coords.Left, coords.Top);
|
||||
renderer.AddVertex(width, 0, coords.Right, coords.Top);
|
||||
renderer.AddVertex(0, height, coords.Left, coords.Bottom);
|
||||
renderer.AddVertex(width, height, coords.Right, coords.Bottom);
|
||||
renderer.AddVertex(width, 0, right, coords.Top);
|
||||
renderer.AddVertex(0, height, coords.Left, bottom);
|
||||
renderer.AddVertex(width, height, right, bottom);
|
||||
renderer.End();
|
||||
}
|
||||
|
||||
|
@ -202,8 +202,8 @@ FloatRect Text::GetRect() const
|
||||
FloatRect rect;
|
||||
rect.Left = (myBaseRect.Left - GetOrigin().x) * GetScale().x + GetPosition().x;
|
||||
rect.Top = (myBaseRect.Top - GetOrigin().y) * GetScale().y + GetPosition().y;
|
||||
rect.Right = (myBaseRect.Right - GetOrigin().x) * GetScale().x + GetPosition().x;
|
||||
rect.Bottom = (myBaseRect.Bottom - GetOrigin().y) * GetScale().y + GetPosition().y;
|
||||
rect.Width = myBaseRect.Width * GetScale().x;
|
||||
rect.Height = myBaseRect.Height * GetScale().y;
|
||||
|
||||
return rect;
|
||||
}
|
||||
@ -229,6 +229,10 @@ void Text::Render(RenderTarget&, Renderer& renderer) const
|
||||
float underlineOffset = myCharacterSize * 0.1f;
|
||||
float underlineThickness = myCharacterSize * (bold ? 0.1f : 0.07f);
|
||||
FloatRect underlineCoords = texture.GetTexCoords(IntRect(1, 1, 1, 1));
|
||||
float underlineLeft = underlineCoords.Left;
|
||||
float underlineTop = underlineCoords.Top;
|
||||
float underlineRight = underlineCoords.Left + underlineCoords.Width;
|
||||
float underlineBottom = underlineCoords.Top + underlineCoords.Height;
|
||||
|
||||
// Initialize the rendering coordinates
|
||||
float space = static_cast<float>(myFont->GetGlyph(L' ', myCharacterSize, bold).Advance);
|
||||
@ -257,10 +261,10 @@ void Text::Render(RenderTarget&, Renderer& renderer) const
|
||||
float bottom = top + underlineThickness;
|
||||
|
||||
renderer.Begin(Renderer::QuadList);
|
||||
renderer.AddVertex(0, top, underlineCoords.Left, underlineCoords.Top);
|
||||
renderer.AddVertex(x, top, underlineCoords.Right, underlineCoords.Top);
|
||||
renderer.AddVertex(x, bottom, underlineCoords.Right, underlineCoords.Bottom);
|
||||
renderer.AddVertex(0, bottom, underlineCoords.Left, underlineCoords.Bottom);
|
||||
renderer.AddVertex(0, top, underlineLeft, underlineTop);
|
||||
renderer.AddVertex(x, top, underlineRight, underlineTop);
|
||||
renderer.AddVertex(x, bottom, underlineRight, underlineBottom);
|
||||
renderer.AddVertex(0, bottom, underlineLeft, underlineBottom);
|
||||
renderer.End();
|
||||
}
|
||||
|
||||
@ -279,12 +283,17 @@ void Text::Render(RenderTarget&, Renderer& renderer) const
|
||||
const IntRect& bounds = glyph.Bounds;
|
||||
const FloatRect& coords = texture.GetTexCoords(glyph.SubRect);
|
||||
|
||||
int boundsRight = bounds.Left + bounds.Width;
|
||||
int boundsBottom = bounds.Top + bounds.Height;
|
||||
float coordsRight = coords.Left + coords.Width;
|
||||
float coordsBottom = coords.Top + coords.Height;
|
||||
|
||||
// Draw a textured quad for the current character
|
||||
renderer.Begin(Renderer::QuadList);
|
||||
renderer.AddVertex(x + bounds.Left - italicCoeff * bounds.Top, y + bounds.Top, coords.Left, coords.Top);
|
||||
renderer.AddVertex(x + bounds.Right - italicCoeff * bounds.Top, y + bounds.Top, coords.Right, coords.Top);
|
||||
renderer.AddVertex(x + bounds.Right - italicCoeff * bounds.Bottom, y + bounds.Bottom, coords.Right, coords.Bottom);
|
||||
renderer.AddVertex(x + bounds.Left - italicCoeff * bounds.Bottom, y + bounds.Bottom, coords.Left, coords.Bottom);
|
||||
renderer.AddVertex(x + boundsRight - italicCoeff * bounds.Top, y + bounds.Top, coordsRight, coords.Top);
|
||||
renderer.AddVertex(x + boundsRight - italicCoeff * boundsBottom, y + boundsBottom, coordsRight, coordsBottom);
|
||||
renderer.AddVertex(x + bounds.Left - italicCoeff * boundsBottom, y + boundsBottom, coords.Left, coordsBottom);
|
||||
renderer.End();
|
||||
|
||||
// Advance to the next character
|
||||
@ -298,10 +307,10 @@ void Text::Render(RenderTarget&, Renderer& renderer) const
|
||||
float bottom = top + underlineThickness;
|
||||
|
||||
renderer.Begin(Renderer::QuadList);
|
||||
renderer.AddVertex(0, top, underlineCoords.Left, underlineCoords.Top);
|
||||
renderer.AddVertex(x, top, underlineCoords.Right, underlineCoords.Top);
|
||||
renderer.AddVertex(x, bottom, underlineCoords.Right, underlineCoords.Bottom);
|
||||
renderer.AddVertex(0, bottom, underlineCoords.Left, underlineCoords.Bottom);
|
||||
renderer.AddVertex(0, top, underlineLeft, underlineTop);
|
||||
renderer.AddVertex(x, top, underlineRight, underlineTop);
|
||||
renderer.AddVertex(x, bottom, underlineRight, underlineBottom);
|
||||
renderer.AddVertex(0, bottom, underlineLeft, underlineBottom);
|
||||
renderer.End();
|
||||
}
|
||||
}
|
||||
@ -375,7 +384,7 @@ void Text::UpdateRect() const
|
||||
curWidth += static_cast<float>(curGlyph.Advance);
|
||||
|
||||
// Update the maximum height
|
||||
float charHeight = charSize + curGlyph.Bounds.Bottom;
|
||||
float charHeight = charSize + curGlyph.Bounds.Top + curGlyph.Bounds.Height;
|
||||
if (charHeight > curHeight)
|
||||
curHeight = charHeight;
|
||||
}
|
||||
@ -404,8 +413,8 @@ void Text::UpdateRect() const
|
||||
// Finally update the rectangle
|
||||
myBaseRect.Left = 0;
|
||||
myBaseRect.Top = 0;
|
||||
myBaseRect.Right = width;
|
||||
myBaseRect.Bottom = height;
|
||||
myBaseRect.Width = width;
|
||||
myBaseRect.Height = height;
|
||||
}
|
||||
|
||||
} // namespace sf
|
||||
|
@ -126,8 +126,10 @@ void View::SetViewport(const FloatRect& viewport)
|
||||
////////////////////////////////////////////////////////////
|
||||
void View::Reset(const FloatRect& rectangle)
|
||||
{
|
||||
myCenter = rectangle.GetCenter();
|
||||
mySize = rectangle.GetSize();
|
||||
myCenter.x = (rectangle.Left + rectangle.Width) / 2.f;
|
||||
myCenter.y = (rectangle.Top + rectangle.Height) / 2.f;
|
||||
mySize.x = rectangle.Width;
|
||||
mySize.y = rectangle.Height;
|
||||
myRotation = 0;
|
||||
|
||||
myMatrixUpdated = false;
|
||||
|
Loading…
Reference in New Issue
Block a user