SFML 3.1.0
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
sf::RectangleShape Class Reference
Graphics module

Specialized shape representing a rectangle. More...

`#include <SFML/Graphics/RectangleShape.hpp>`

Inheritance diagram for sf::RectangleShape:

## Public Member Functions
| RectangleShape (Vector2f size={})
| Default constructor.
void | setSize (Vector2f size)
| Set the size of the rectangle.
Vector2f | getSize () const
| Get the size of the rectangle.
std::size_t | getPointCount () const override
| Get the number of points defining the shape.
Vector2f | getPoint (std::size_t index) const override
| Get a point of the rectangle.
Vector2f | getGeometricCenter () const override
| Get the geometric center of the rectangle.
void | setTexture (const Texture *texture, bool resetRect=false)
| Change the source texture of the shape.
void | setTextureRect (const IntRect &rect)
| Set the sub-rectangle of the texture that the shape will display.
void | setFillColor (Color color)
| Set the fill color of the shape.
void | setOutlineColor (Color color)
| Set the outline color of the shape.
void | setOutlineThickness (float thickness)
| Set the thickness of the shape's outline.
void | setMiterLimit (float miterLimit)
| Set the limit on the ratio between miter length and outline thickness.
const Texture * | getTexture () const
| Get the source texture of the shape.
const IntRect & | getTextureRect () const
| Get the sub-rectangle of the texture displayed by the shape.
Color | getFillColor () const
| Get the fill color of the shape.
Color | getOutlineColor () const
| Get the outline color of the shape.
float | getOutlineThickness () const
| Get the outline thickness of the shape.
float | getMiterLimit () const
| Get the limit on the ratio between miter length and outline thickness.
FloatRect | getLocalBounds () const
| Get the local bounding rectangle of the entity.
FloatRect | getGlobalBounds () const
| Get the global (non-minimal) bounding rectangle of the entity.
void | setPosition (Vector2f position)
| set the position of the object
void | setRotation (Angle angle)
| set the orientation of the object
void | setScale (Vector2f factors)
| set the scale factors of the object
void | setOrigin (Vector2f origin)
| set the local origin of the object
Vector2f | getPosition () const
| get the position of the object
Angle | getRotation () const
| get the orientation of the object
Vector2f | getScale () const
| get the current scale of the object
Vector2f | getOrigin () const
| get the local origin of the object
void | move (Vector2f offset)
| Move the object by a given offset.
void | rotate (Angle angle)
| Rotate the object.
void | scale (Vector2f factor)
| Scale the object.
const Transform & | getTransform () const
| get the combined transform of the object
const Transform & | getInverseTransform () const
| get the inverse of the combined transform of the object

## Protected Member Functions
void | update ()
| Recompute the internal geometry of the shape.

## Detailed Description

Specialized shape representing a rectangle.

This class inherits all the functions of sf::Transformable (position, rotation, scale, bounds, ...) as well as the functions of sf::Shape (outline, color, texture, ...).

Usage example:

sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(100, 50));
rectangle.setOutlineColor(sf::Color::Red);
rectangle.setOutlineThickness(5);
rectangle.setPosition({10, 20});
...
window.draw(rectangle);
sf::Color::Red
static const Color Red
Red predefined color.
Definition Color.hpp:84
sf::RectangleShape
Specialized shape representing a rectangle.
Definition RectangleShape.hpp:46
sf::RectangleShape::setSize
void setSize(Vector2f size)
Set the size of the rectangle.
sf::Shape::setOutlineThickness
void setOutlineThickness(float thickness)
Set the thickness of the shape's outline.
sf::Shape::setOutlineColor
void setOutlineColor(Color color)
Set the outline color of the shape.
sf::Transformable::setPosition
void setPosition(Vector2f position)
set the position of the object
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:213

See also
  sf::Shape, sf::CircleShape, sf::ConvexShape

Definition at line 45 of file RectangleShape.hpp.

## Constructor & Destructor Documentation

## ◆ RectangleShape()

sf::RectangleShape::RectangleShape | ( | Vector2f | size = {} | ) |
| explicit

Default constructor.

Parameters

  size | Size of the rectangle

## Member Function Documentation

## ◆ getFillColor()

Color sf::Shape::getFillColor | ( |  | ) | const
| nodiscard inherited

Get the fill color of the shape.

Returns
  Fill color of the shape

See also
  setFillColor

## ◆ getGeometricCenter()

Vector2f sf::RectangleShape::getGeometricCenter | ( |  | ) | const
| nodiscard override virtual

Get the geometric center of the rectangle.

The returned point is in local coordinates, that is, the shape's transforms (position, rotation, scale) are not taken into account.

Returns
  The geometric center of the shape

Reimplemented from sf::Shape.

## ◆ getGlobalBounds()

FloatRect sf::Shape::getGlobalBounds | ( |  | ) | const
| nodiscard inherited

Get the global (non-minimal) bounding rectangle of the entity.

The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the shape in the global 2D world's coordinate system.

This function does not necessarily return the minimal bounding rectangle. It merely ensures that the returned rectangle covers all the vertices (but possibly more). This allows for a fast approximation of the bounds as a first check; you may want to use more precise checks on top of that.

Returns
  Global bounding rectangle of the entity

## ◆ getInverseTransform()

const Transform & sf::Transformable::getInverseTransform | ( |  | ) | const
| nodiscard inherited

get the inverse of the combined transform of the object

Returns
  Inverse of the combined transformations applied to the object

See also
  getTransform

## ◆ getLocalBounds()

FloatRect sf::Shape::getLocalBounds | ( |  | ) | const
| nodiscard inherited

Get the local bounding rectangle of the entity.

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.

Returns
  Local bounding rectangle of the entity

## ◆ getMiterLimit()

float sf::Shape::getMiterLimit | ( |  | ) | const
| nodiscard inherited

Get the limit on the ratio between miter length and outline thickness.

Returns
  Limit on the ratio between miter length and outline thickness

See also
  setMiterLimit

## ◆ getOrigin()

Vector2f sf::Transformable::getOrigin | ( |  | ) | const
| nodiscard inherited

get the local origin of the object

Returns
  Current origin

See also
  setOrigin

## ◆ getOutlineColor()

Color sf::Shape::getOutlineColor | ( |  | ) | const
| nodiscard inherited

Get the outline color of the shape.

Returns
  Outline color of the shape

See also
  setOutlineColor

## ◆ getOutlineThickness()

float sf::Shape::getOutlineThickness | ( |  | ) | const
| nodiscard inherited

Get the outline thickness of the shape.

Returns
  Outline thickness of the shape

See also
  setOutlineThickness

## ◆ getPoint()

Vector2f sf::RectangleShape::getPoint | ( | std::size_t | index | ) | const
| nodiscard override virtual

Get a point of the rectangle.

The returned point is in local coordinates, that is, the shape's transforms (position, rotation, scale) are not taken into account. The result is undefined if index is out of the valid range.

Parameters

  index | Index of the point to get, in range [0 .. 3]

Returns
  index-th point of the shape

Implements sf::Shape.

## ◆ getPointCount()

std::size_t sf::RectangleShape::getPointCount | ( |  | ) | const
| nodiscard override virtual

Get the number of points defining the shape.

Returns
  Number of points of the shape. For rectangle shapes, this number is always 4.

Implements sf::Shape.

## ◆ getPosition()

Vector2f sf::Transformable::getPosition | ( |  | ) | const
| nodiscard inherited

get the position of the object

Returns
  Current position

See also
  setPosition

## ◆ getRotation()

Angle sf::Transformable::getRotation | ( |  | ) | const
| nodiscard inherited

get the orientation of the object

The rotation is always in the range [0, 360].

Returns
  Current rotation

See also
  setRotation

## ◆ getScale()

Vector2f sf::Transformable::getScale | ( |  | ) | const
| nodiscard inherited

get the current scale of the object

Returns
  Current scale factors

See also
  setScale

## ◆ getSize()

Vector2f sf::RectangleShape::getSize | ( |  | ) | const
| nodiscard

Get the size of the rectangle.

Returns
  Size of the rectangle

See also
  setSize

## ◆ getTexture()

const Texture * sf::Shape::getTexture | ( |  | ) | const
| nodiscard inherited

Get the source texture of the shape.

If the shape has no source texture, a nullptr is returned. The returned pointer is const, which means that you can't modify the texture when you retrieve it with this function.

Returns
  Pointer to the shape's texture

See also
  setTexture

## ◆ getTextureRect()

const IntRect & sf::Shape::getTextureRect | ( |  | ) | const
| nodiscard inherited

Get the sub-rectangle of the texture displayed by the shape.

Returns
  Texture rectangle of the shape

See also
  setTextureRect

## ◆ getTransform()

const Transform & sf::Transformable::getTransform | ( |  | ) | const
| nodiscard inherited

get the combined transform of the object

Returns
  Transform combining the position/rotation/scale/origin of the object

See also
  getInverseTransform

## ◆ move()

void sf::Transformable::move | ( | Vector2f | offset | ) |
| inherited

Move the object by a given offset.

This function adds to the current position of the object, unlike setPosition which overwrites it. Thus, it is equivalent to the following code:

object.setPosition(object.getPosition() + offset);
sf::Transformable::getPosition
Vector2f getPosition() const
get the position of the object

Parameters

  offset | Offset

See also
  setPosition

## ◆ rotate()

void sf::Transformable::rotate | ( | Angle | angle | ) |
| inherited

Rotate the object.

This function adds to the current rotation of the object, unlike setRotation which overwrites it. Thus, it is equivalent to the following code:

object.setRotation(object.getRotation() + angle);
sf::Transformable::getRotation
Angle getRotation() const
get the orientation of the object

Parameters

  angle | Angle of rotation

## ◆ scale()

void sf::Transformable::scale | ( | Vector2f | factor | ) |
| inherited

Scale the object.

This function multiplies the current scale of the object, unlike setScale which overwrites it. Thus, it is equivalent to the following code:

sf::Vector2f scale = object.getScale();
object.setScale(scale.x * factor.x, scale.y * factor.y);
sf::Transformable::scale
void scale(Vector2f factor)
Scale the object.

Parameters

  factor | Scale factors

See also
  setScale

## ◆ setFillColor()

void sf::Shape::setFillColor | ( | Color | color | ) |
| inherited

Set the fill color of the shape.

This color is modulated (multiplied) with the shape's texture if any. It can be used to colorize the shape, or change its global opacity. You can use sf::Color::Transparent to make the inside of the shape transparent, and have the outline alone. By default, the shape's fill color is opaque white.

Parameters

  color | New color of the shape

See also
  getFillColor, setOutlineColor

## ◆ setMiterLimit()

void sf::Shape::setMiterLimit | ( | float | miterLimit | ) |
| inherited

Set the limit on the ratio between miter length and outline thickness.

Outline segments around each shape corner are joined either with a miter or a bevel join.

- A miter join is formed by extending outline segments until they intersect. The distance between the point of intersection and the shape's corner is the miter length.
- A bevel join is formed by connecting outline segments with a straight line perpendicular to the corner's bissector.

The miter limit is used to determine whether ouline segments around a corner are joined with a bevel or a miter. When the ratio between the miter length and outline thickness exceeds the miter limit, a bevel is used instead of a miter.

The miter limit is linked to the maximum inner angle of a corner below which a bevel is used by the following formula:

miterLimit = 1 / sin(angle / 2)

The miter limit must be greater than or equal to 1. By default, the miter limit is 10.

Parameters

  miterLimit | New miter limit

See also
  getMiterLimit

## ◆ setOrigin()

void sf::Transformable::setOrigin | ( | Vector2f | origin | ) |
| inherited

set the local origin of the object

The origin of an object defines the center point for all transformations (position, scale, rotation). The coordinates of this point must be relative to the top-left corner of the object, and ignore all transformations (position, scale, rotation). The default origin of a transformable object is (0, 0).

Parameters

  origin | New origin

See also
  getOrigin

## ◆ setOutlineColor()

void sf::Shape::setOutlineColor | ( | Color | color | ) |
| inherited

Set the outline color of the shape.

By default, the shape's outline color is opaque white.

Parameters

  color | New outline color of the shape

See also
  getOutlineColor, setFillColor

## ◆ setOutlineThickness()

void sf::Shape::setOutlineThickness | ( | float | thickness | ) |
| inherited

Set the thickness of the shape's outline.

Note that negative values are allowed (so that the outline expands towards the center of the shape), and using zero disables the outline. By default, the outline thickness is 0.

Parameters

  thickness | New outline thickness

See also
  getOutlineThickness

## ◆ setPosition()

void sf::Transformable::setPosition | ( | Vector2f | position | ) |
| inherited

set the position of the object

This function completely overwrites the previous position. See the move function to apply an offset based on the previous position instead. The default position of a transformable object is (0, 0).

Note that sf::Text may appear offset when positioned. This is because its local bounds are influenced by font metrics (e.g. tallest characters) to consistently align with the text's baseline. As such the getGlobalBounds() position may not match the position you set.

To account for this offset, the local bounds need to be considered. Either by including it in the position calculation:

text.setPosition(position - text.getLocalBounds().position);

Or by adjusting the text's origin:

text.setOrigin(text.getLocalBounds().position);
text.setPosition(position);

Parameters

  position | New position

See also
  move, getPosition

## ◆ setRotation()

void sf::Transformable::setRotation | ( | Angle | angle | ) |
| inherited

set the orientation of the object

This function completely overwrites the previous rotation. See the rotate function to add an angle based on the previous rotation instead. The default rotation of a transformable object is 0.

Parameters

  angle | New rotation

See also
  rotate, getRotation

## ◆ setScale()

void sf::Transformable::setScale | ( | Vector2f | factors | ) |
| inherited

set the scale factors of the object

This function completely overwrites the previous scale. See the scale function to add a factor based on the previous scale instead. The default scale of a transformable object is (1, 1).

Parameters

  factors | New scale factors

See also
  scale, getScale

## ◆ setSize()

void sf::RectangleShape::setSize | ( | Vector2f | size | ) |

Set the size of the rectangle.

Parameters

  size | New size of the rectangle

See also
  getSize

## ◆ setTexture()

void sf::Shape::setTexture | ( | const Texture * | texture,
|  | bool | resetRect = false )
| inherited

Change the source texture of the shape.

The texture argument refers to a texture that must exist as long as the shape uses it. Indeed, the shape doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the shape tries to use it, the behavior is undefined. texture can be a null pointer to disable texturing. If resetRect is true, the TextureRect property of the shape is automatically adjusted to the size of the new texture. If it is false, the texture rect is left unchanged.

Parameters

  texture | New texture
  resetRect | Should the texture rect be reset to the size of the new texture?

See also
  getTexture, setTextureRect

## ◆ setTextureRect()

void sf::Shape::setTextureRect | ( | const IntRect & | rect | ) |
| inherited

Set the sub-rectangle of the texture that the shape will display.

The texture rect is useful when you don't want to display the whole texture, but rather a part of it. By default, the texture rect covers the entire texture.

Parameters

  rect | Rectangle defining the region of the texture to display

See also
  getTextureRect, setTexture

## ◆ update()

void sf::Shape::update | ( |  | ) |
| protected inherited

Recompute the internal geometry of the shape.

This function must be called by the derived class every time the shape's points change (i.e. the result of either getPointCount or getPoint is different).

The documentation for this class was generated from the following file:

- RectangleShape.hpp