Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions
sf::Shape Class Reference

A convex, colored polygon with an optional outline. More...

#include <Shape.hpp>

Inheritance diagram for sf::Shape:
sf::Drawable

List of all members.

Classes

struct  Point
 Define a simple 2D point with position, normal and colors.

Public Member Functions

 Shape ()
 Default constructor.
void AddPoint (float x, float y, const Color &color=Color(255, 255, 255), const Color &outlineColor=Color(0, 0, 0))
 Add a new point to the shape.
void AddPoint (const Vector2f &position, const Color &color=Color(255, 255, 255), const Color &outlineColor=Color(0, 0, 0))
 Add a new point to the shape.
unsigned int GetPointsCount () const
 Get the number of points composing the shape.
void EnableFill (bool enable)
 Enable or disable the shape's filling.
void EnableOutline (bool enable)
 Enable or disable the shape's outline.
void SetPointPosition (unsigned int index, const Vector2f &position)
 Change the position of a point.
void SetPointPosition (unsigned int index, float x, float y)
 Change the position of a point.
void SetPointColor (unsigned int index, const Color &color)
 Change the color of a point.
void SetPointOutlineColor (unsigned int index, const Color &color)
 Change the outline color of a point.
void SetOutlineThickness (float thickness)
 Change the thickness of the shape outline.
const Vector2fGetPointPosition (unsigned int index) const
 Get the position of a point.
const ColorGetPointColor (unsigned int index) const
 Get the color of a point.
const ColorGetPointOutlineColor (unsigned int index) const
 Get the outline color of a point.
float GetOutlineThickness () const
 Get the thickness of the shape outline.
void SetPosition (float x, float y)
 Set the position of the object.
void SetPosition (const Vector2f &position)
 Set the position of the object.
void SetX (float x)
 Set the X position of the object.
void SetY (float y)
 Set the Y position of the object.
void SetScale (float factorX, float factorY)
 Set the scale factors of the object.
void SetScale (const Vector2f &factors)
 Set the scale factors of the object.
void SetScaleX (float factor)
 Set the X scale factor of the object.
void SetScaleY (float factor)
 Set the Y scale factor of the object.
void SetOrigin (float x, float y)
 Set the local origin of the object.
void SetOrigin (const Vector2f &origin)
 Set the local origin of the object.
void SetRotation (float angle)
 Set the orientation of the object.
void SetColor (const Color &color)
 Set the global color of the object.
void SetBlendMode (Blend::Mode mode)
 Set the blending mode of the object.
const Vector2fGetPosition () const
 Get the position of the object.
const Vector2fGetScale () const
 Get the current scale of the object.
const Vector2fGetOrigin () const
 Get the local origin of the object.
float GetRotation () const
 Get the orientation of the object.
const ColorGetColor () const
 Get the color of the object.
Blend::Mode GetBlendMode () const
 Get the blend mode of the object.
void Move (float offsetX, float offsetY)
 Move the object by a given offset.
void Move (const Vector2f &offset)
 Move the object by a given offset.
void Scale (float factorX, float factorY)
 Scale the object.
void Scale (const Vector2f &factor)
 Scale the object.
void Rotate (float angle)
 Rotate the object.
Vector2f TransformToLocal (const Vector2f &point) const
 Transform a point in object local coordinates.
Vector2f TransformToGlobal (const Vector2f &point) const
 Transform a local point in global coordinates.

Static Public Member Functions

static Shape Line (float p1x, float p1y, float p2x, float p2y, float thickness, const Color &color, float outline=0.f, const Color &outlineColor=Color(0, 0, 0))
 Create a new line.
static Shape Line (const Vector2f &start, const Vector2f &end, float thickness, const Color &color, float outline=0.f, const Color &outlineColor=Color(0, 0, 0))
 Create a new line.
static Shape Rectangle (float left, float top, float width, float height, const Color &color, float outline=0.f, const Color &outlineColor=Color(0, 0, 0))
 Create a new rectangular shape.
static Shape Rectangle (const FloatRect &rectangle, const Color &color, float outline=0.f, const Color &outlineColor=Color(0, 0, 0))
 Create a new rectangular shape.
static Shape Circle (float x, float y, float radius, const Color &color, float outline=0.f, const Color &outlineColor=Color(0, 0, 0))
 Create a new circular shape.
static Shape Circle (const Vector2f &center, float radius, const Color &color, float outline=0.f, const Color &outlineColor=Color(0, 0, 0))
 Create a new circular shape.

Protected Member Functions

virtual void Render (RenderTarget &target, Renderer &renderer) const
 Draw the object to a render target.
const Matrix3GetMatrix () const
 Get the transform matrix of the object.
const Matrix3GetInverseMatrix () const
 Get the inverse transform matrix of the object.

Detailed Description

A convex, colored polygon with an optional outline.

sf::Shape is a drawable class that allows to define and display a custom convex shape on a render target.

It is important to keep in mind that shapes must always be convex, otherwise they may not be drawn correctly. Moreover, the points must be added in the right order; using a random order would also result in an incorrect shape.

A shape is made of points that have their own individual attributes:

Shapes have an outline that can be enabled or not. You can control the thickness of the outline with the SetOutlineThickness function.

They also inherits all the functions from sf::Drawable: position, rotation, scale, origin, global color and blend mode.

Some static functions are provided to directly create common shapes such as lines, rectangles and circles:

 sf::Shape line = sf::Shape::Line(start, end, thickness, color);
 sf::Shape rectangle = sf::Shape::Rectangle(rect, thickness);
 sf::Shape circle = sf::Shape::Circle(center, radius, color);

A common mistake is to mix the individual points positions / colors and the global position / color of the shape. They are completely separate attributes that are combined when the shape is drawn (positions are added, colors are multiplied).

 sf::Shape line = sf::Shape::Line(sf::Vector2f(100, 100), sf::Vector2f(200, 200), 10, sf::Color::Red);

 // --> line.GetPosition() is (0, 0), *not* (100, 100)
 // --> line.GetColor() is white, *not* red

So if you plan to change the position / color of your shape after it is created, you'd better create the points around the origin and with white color, and use only the global position / color (SetPosition, SetColor).

Usage example:

 // Create a shape
 sf::Shape shape;

 // Define its points
 shape.AddPoint(10, 10, sf::Color::White, sf::Color::Red);
 shape.AddPoint(50, 10, sf::Color::White, sf::Color::Green);
 shape.AddPoint(10, 50, sf::Color::White, sf::Color::Blue);

 // Enable outline only
 shape.EnableFill(false);
 shape.EnableOutline(true);
 shape.SetOutlineThickness(10);

 // Display it
 window.Draw(shape); // window is a sf::RenderWindow

 // Display static shapes
 window.Draw(sf::Shape::Line(0, 0, 10, 20, sf::Color::Red));
 window.Draw(sf::Shape::Rectangle(100, 1000, 50, 20, sf::Color::Green));
 window.Draw(sf::Shape::Circle(500, 500, 20, sf::Color::Blue, 5, sf::Color::Black));
See also:
sf::Sprite

Definition at line 42 of file Shape.hpp.


Constructor & Destructor Documentation

sf::Shape::Shape ( )

Default constructor.

Creates an empty shape (no point).


Member Function Documentation

void sf::Shape::AddPoint ( float  x,
float  y,
const Color color = Color(255, 255, 255),
const Color outlineColor = Color(0, 0, 0) 
)

Add a new point to the shape.

The new point is inserted at the end of the shape.

Parameters:
xX position of the point
yY position of the point
colorColor of the point
outlineColorOutline color of the point
void sf::Shape::AddPoint ( const Vector2f position,
const Color color = Color(255, 255, 255),
const Color outlineColor = Color(0, 0, 0) 
)

Add a new point to the shape.

The new point is inserted at the end of the shape.

Parameters:
positionPosition of the point
colorColor of the point
outlineColorOutline color of the point
static Shape sf::Shape::Circle ( float  x,
float  y,
float  radius,
const Color color,
float  outline = 0.f,
const Color outlineColor = Color(0, 0, 0) 
) [static]

Create a new circular shape.

This is a static function that returns a new object, don't try to call it on an existing object to modify it.

Warning: the position and color that you pass to this function are used to compute the position and color of each point, they have nothing to do with the global position and color of the shape, inherited from sf::Drawable. See the class description for more information about this.

Parameters:
xX coordinate of the center
yY coordinate of the center
radiusRadius of the circle
colorColor of the shape's points
outlineOutline thickness
outlineColorOutline color of the shape's points
See also:
Line, Rectangle
static Shape sf::Shape::Circle ( const Vector2f center,
float  radius,
const Color color,
float  outline = 0.f,
const Color outlineColor = Color(0, 0, 0) 
) [static]

Create a new circular shape.

This is a static function that returns a new object, don't try to call it on an existing object to modify it.

 sf::Vector2f center(0, 0);
 sf::Shape circle = sf::Shape::Circle(center, 7, sf::Color::Blue);

Warning: the position and color that you pass to this function are used to compute the position and color of each point, they have nothing to do with the global position and color of the shape, inherited from sf::Drawable. See the class description for more information about this.

Parameters:
centerCenter of the circle
radiusRadius of the circle
colorColor of the shape's points
outlineOutline thickness
outlineColorOutline color of the shape's points
See also:
Line, Rectangle
void sf::Shape::EnableFill ( bool  enable)

Enable or disable the shape's filling.

This option is enabled by default.

Parameters:
enableTrue to enable, false to disable
See also:
EnableOutline
void sf::Shape::EnableOutline ( bool  enable)

Enable or disable the shape's outline.

This option is enabled by default.

Parameters:
enableTrue to enable, false to disable
See also:
EnableFill
Blend::Mode sf::Drawable::GetBlendMode ( ) const [inherited]

Get the blend mode of the object.

Returns:
Current blend mode
See also:
SetBlendMode
const Color& sf::Drawable::GetColor ( ) const [inherited]

Get the color of the object.

Returns:
Current color
See also:
SetColor
const Matrix3& sf::Drawable::GetInverseMatrix ( ) const [protected, inherited]

Get the inverse transform matrix of the object.

Returns:
Inverse transform matrix
See also:
GetMatrix
const Matrix3& sf::Drawable::GetMatrix ( ) const [protected, inherited]

Get the transform matrix of the object.

Returns:
Transform matrix
See also:
GetInverseMatrix
const Vector2f& sf::Drawable::GetOrigin ( ) const [inherited]

Get the local origin of the object.

Returns:
Current origin
See also:
SetOrigin
float sf::Shape::GetOutlineThickness ( ) const

Get the thickness of the shape outline.

Returns:
Current outline thickness
See also:
SetOutlineThickness
const Color& sf::Shape::GetPointColor ( unsigned int  index) const

Get the color of a point.

Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range [0, GetPointscount() - 1]) the behaviour is undefined.

Parameters:
indexIndex of the point
Returns:
Color of the index-th point
See also:
SetPointColor, GetPointPosition, GetPointOutlineColor
const Color& sf::Shape::GetPointOutlineColor ( unsigned int  index) const

Get the outline color of a point.

Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range [0, GetPointscount() - 1]) the behaviour is undefined.

Parameters:
indexIndex of the point
Returns:
Outline color of the index-th point
See also:
SetPointOutlineColor, GetPointPosition, GetPointColor
const Vector2f& sf::Shape::GetPointPosition ( unsigned int  index) const

Get the position of a point.

Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range [0, GetPointscount() - 1]) the behaviour is undefined.

Parameters:
indexIndex of the point
Returns:
Position of the index-th point
See also:
SetPointPosition, GetPointColor, GetPointOutlineColor
unsigned int sf::Shape::GetPointsCount ( ) const

Get the number of points composing the shape.

Returns:
Total number of points
const Vector2f& sf::Drawable::GetPosition ( ) const [inherited]

Get the position of the object.

Returns:
Current position
See also:
SetPosition
float sf::Drawable::GetRotation ( ) const [inherited]

Get the orientation of the object.

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

Returns:
Current rotation, in degrees
See also:
SetRotation
const Vector2f& sf::Drawable::GetScale ( ) const [inherited]

Get the current scale of the object.

Returns:
Current scale factors
See also:
SetScale
static Shape sf::Shape::Line ( float  p1x,
float  p1y,
float  p2x,
float  p2y,
float  thickness,
const Color color,
float  outline = 0.f,
const Color outlineColor = Color(0, 0, 0) 
) [static]

Create a new line.

This is a static function that returns a new object, don't try to call it on an existing object to modify it.

 sf::Shape line = sf::Shape::Line(0, 0, 10, 20, 2.5f, sf::Color::Green);

Warning: the position and color that you pass to this function are used to compute the position and color of each point, they have nothing to do with the global position and color of the shape, inherited from sf::Drawable. See the class description for more information about this.

Parameters:
p1xX coordinate of the start point
p1yY coordinate of the start point
p2xX coordinate of the end point
p2yY coordinate of the end point
thicknessThickness of the line
colorColor of the shape's points
outlineOutline thickness
outlineColorOutline color of the shape's points
See also:
Rectangle, Circle
static Shape sf::Shape::Line ( const Vector2f start,
const Vector2f end,
float  thickness,
const Color color,
float  outline = 0.f,
const Color outlineColor = Color(0, 0, 0) 
) [static]

Create a new line.

This is a static function that returns a new object, don't try to call it on an existing object to modify it.

 sf::Vector2f start(0, 0);
 sf::Vector2f end(10, 20);
 sf::Shape line = sf::Shape::Line(start, end, 2.5f, sf::Color::Green);

Warning: the position and color that you pass to this function are used to compute the position and color of each point, they have nothing to do with the global position and color of the shape, inherited from sf::Drawable. See the class description for more information about this.

Parameters:
startStart point
endEnd point
thicknessThickness of the line
colorColor of the shape's points
outlineOutline thickness
outlineColorOutline color of the shape's points
See also:
Rectangle, Circle
void sf::Drawable::Move ( float  offsetX,
float  offsetY 
) [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:

 sf::Vector2f pos = object.GetPosition();
 object.SetPosition(pos.x + offsetX, pos.y + offsetY);
Parameters:
offsetXX offset
offsetYY offset
See also:
SetPosition
void sf::Drawable::Move ( const 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);
Parameters:
offsetOffset
See also:
SetPosition
static Shape sf::Shape::Rectangle ( float  left,
float  top,
float  width,
float  height,
const Color color,
float  outline = 0.f,
const Color outlineColor = Color(0, 0, 0) 
) [static]

Create a new rectangular shape.

This is a static function that returns a new object, don't try to call it on an existing object to modify it.

 sf::Shape rect = sf::Shape::Rectangle(10, 20, 50, 100, sf::Color::Red);

Warning: the position and color that you pass to this function are used to compute the position and color of each point, they have nothing to do with the global position and color of the shape, inherited from sf::Drawable. See the class description for more information about this.

Parameters:
leftLeft coordinate of the rectangle
topTop coordinate of the rectangle
widthWidth of the rectangle
heightHeight of the rectangle
colorColor of the shape's points
outlineOutline thickness
outlineColorOutline color of the shape's points
See also:
Line, Circle
static Shape sf::Shape::Rectangle ( const FloatRect rectangle,
const Color color,
float  outline = 0.f,
const Color outlineColor = Color(0, 0, 0) 
) [static]

Create a new rectangular shape.

This is a static function that returns a new object, don't try to call it on an existing object to modify it.

 sf::FloatRect source(10, 20, 50, 100);
 sf::Shape rect = sf::Shape::Rectangle(source, sf::Color::Red);

Warning: the position and color that you pass to this function are used to compute the position and color of each point, they have nothing to do with the global position and color of the shape, inherited from sf::Drawable. See the class description for more information about this.

Parameters:
rectangleRectangle defining the shape
colorColor of the shape's points
outlineOutline thickness
outlineColorOutline color of the shape's points
See also:
Line, Circle
virtual void sf::Shape::Render ( RenderTarget target,
Renderer renderer 
) const [protected, virtual]

Draw the object to a render target.

Parameters:
targetRender target
rendererRenderer providing low-level rendering commands

Implements sf::Drawable.

void sf::Drawable::Rotate ( float  angle) [inherited]

Rotate the object.

This function ads 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);
Parameters:
angleAngle of rotation, in degrees
void sf::Drawable::Scale ( float  factorX,
float  factorY 
) [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 * factorX, scale.y * factorY);
Parameters:
factorXHorizontal scale factor
factorYVertical scale factor
See also:
SetScale
void sf::Drawable::Scale ( const 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);
Parameters:
factorScale factors
See also:
SetScale
void sf::Drawable::SetBlendMode ( Blend::Mode  mode) [inherited]

Set the blending mode of the object.

This property defines how the pixels of an object are blended with the pixels of the render target to which it is drawn. To know more about the blending modes available, see the sf::Blend::Mode enum. The default blend mode is Blend::Alpha.

Parameters:
modeNew blending mode
See also:
GetBlendMode
void sf::Drawable::SetColor ( const Color color) [inherited]

Set the global color of the object.

This global color affects the entire object, and modulates (multiplies) its original pixels. The default color is white.

Parameters:
colorNew color
See also:
GetColor
void sf::Drawable::SetOrigin ( float  x,
float  y 
) [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 drawable object is (0, 0).

Parameters:
xX coordinate of the new origin
yY coordinate of the new origin
See also:
GetOrigin
void sf::Drawable::SetOrigin ( const 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 drawable object is (0, 0).

Parameters:
originNew origin
See also:
GetOrigin
void sf::Shape::SetOutlineThickness ( float  thickness)

Change the thickness of the shape outline.

Parameters:
thicknessNew thickness of the outline
See also:
GetOutlineThickness, EnableOutline
void sf::Shape::SetPointColor ( unsigned int  index,
const Color color 
)

Change the color of a point.

Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range [0, GetPointscount() - 1]) the behaviour is undefined.

Parameters:
indexIndex of the point
colorNew color of the point
See also:
GetPointColor, SetPointPosition, SetPointOutlineColor
void sf::Shape::SetPointOutlineColor ( unsigned int  index,
const Color color 
)

Change the outline color of a point.

Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range [0, GetPointscount() - 1]) the behaviour is undefined.

Parameters:
indexIndex of the point
colorNew outline color of the point
See also:
GetPointOutlineColor, SetPointPosition, SetPointColor
void sf::Shape::SetPointPosition ( unsigned int  index,
const Vector2f position 
)

Change the position of a point.

Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range [0, GetPointscount() - 1]) the behaviour is undefined.

Parameters:
indexIndex of the point
positionNew position of the point
See also:
GetPointPosition, SetPointColor, SetPointOutlineColor
void sf::Shape::SetPointPosition ( unsigned int  index,
float  x,
float  y 
)

Change the position of a point.

Warning: this function doesn't check the validity of index, if it is out of bounds (ie. in the range [0, GetPointscount() - 1]) the behaviour is undefined.

Parameters:
indexIndex of the point
xNew X coordinate of the point
yNew Y coordinate of the point
See also:
GetPointPosition, SetPointColor, SetPointOutlineColor
void sf::Drawable::SetPosition ( float  x,
float  y 
) [inherited]

Set the position of the object.

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

Parameters:
xX coordinate of the new position
yY coordinate of the new position
See also:
Move, SetX, SetY, GetPosition
void sf::Drawable::SetPosition ( const Vector2f position) [inherited]

Set the position of the object.

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

Parameters:
positionNew position
See also:
Move, SetX, SetY, GetPosition
void sf::Drawable::SetRotation ( float  angle) [inherited]

Set the orientation of the object.

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

Parameters:
angleNew rotation, in degrees
See also:
Rotate, GetRotation
void sf::Drawable::SetScale ( float  factorX,
float  factorY 
) [inherited]

Set the scale factors of the object.

factorX and factorY must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).

Parameters:
factorXNew horizontal scale factor
factorYNew vertical scale factor
See also:
Scale, SetScaleX, SetScaleY, GetScale
void sf::Drawable::SetScale ( const Vector2f factors) [inherited]

Set the scale factors of the object.

scale.x and scale.y must be strictly positive, otherwise they are ignored. This function completely overwrites the previous scale. See Scale to add a factor based on the previous scale instead. The default scale of a drawable object is (1, 1).

Parameters:
factorsNew scale factors
See also:
Scale, SetScaleX, SetScaleY, GetScale
void sf::Drawable::SetScaleX ( float  factor) [inherited]

Set the X scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factorNew horizontal scale factor
See also:
SetScaleY, SetScale, GetScale
void sf::Drawable::SetScaleY ( float  factor) [inherited]

Set the Y scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factorNew vertical scale factor
See also:
SetScaleX, SetScale, GetScale
void sf::Drawable::SetX ( float  x) [inherited]

Set the X position of the object.

Parameters:
xNew X coordinate
See also:
SetY, SetPosition, GetPosition
void sf::Drawable::SetY ( float  y) [inherited]

Set the Y position of the object.

Parameters:
yNew Y coordinate
See also:
SetX, SetPosition, GetPosition
Vector2f sf::Drawable::TransformToGlobal ( const Vector2f point) const [inherited]

Transform a local point in global coordinates.

This function takes a point in local coordinates, and transforms it in global coordinates. In other words, it applies the same transformations that are applied to the object (origin, translation, rotation and scale).

Parameters:
pointPoint to transform
Returns:
The transformed point
See also:
TransformToLocal
Vector2f sf::Drawable::TransformToLocal ( const Vector2f point) const [inherited]

Transform a point in object local coordinates.

This function takes a point in global coordinates, and transforms it in coordinates local to the object. In other words, it applies the inverse of all the transformations applied to the object (origin, translation, rotation and scale).

Parameters:
pointPoint to transform
Returns:
The transformed point
See also:
TransformToGlobal

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