A convex, colored polygon with an optional outline. More...
#include <Shape.hpp>
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 Vector2f & | GetPointPosition (unsigned int index) const |
Get the position of a point. | |
const Color & | GetPointColor (unsigned int index) const |
Get the color of a point. | |
const Color & | GetPointOutlineColor (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 Vector2f & | GetPosition () const |
Get the position of the object. | |
const Vector2f & | GetScale () const |
Get the current scale of the object. | |
const Vector2f & | GetOrigin () const |
Get the local origin of the object. | |
float | GetRotation () const |
Get the orientation of the object. | |
const Color & | GetColor () 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 ¢er, 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 Matrix3 & | GetMatrix () const |
Get the transform matrix of the object. | |
const Matrix3 & | GetInverseMatrix () const |
Get the inverse transform matrix of the object. |
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));
sf::Shape::Shape | ( | ) |
Default constructor.
Creates an empty shape (no point).
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.
x | X position of the point |
y | Y position of the point |
color | Color of the point |
outlineColor | Outline 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.
position | Position of the point |
color | Color of the point |
outlineColor | Outline 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.
sf::Shape circle = sf::Shape::Circle(0, 0, 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.
x | X coordinate of the center |
y | Y coordinate of the center |
radius | Radius of the circle |
color | Color of the shape's points |
outline | Outline thickness |
outlineColor | Outline color of the shape's points |
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.
center | Center of the circle |
radius | Radius of the circle |
color | Color of the shape's points |
outline | Outline thickness |
outlineColor | Outline color of the shape's points |
void sf::Shape::EnableFill | ( | bool | enable | ) |
Enable or disable the shape's filling.
This option is enabled by default.
enable | True to enable, false to disable |
void sf::Shape::EnableOutline | ( | bool | enable | ) |
Enable or disable the shape's outline.
This option is enabled by default.
enable | True to enable, false to disable |
Blend::Mode sf::Drawable::GetBlendMode | ( | ) | const [inherited] |
const Color& sf::Drawable::GetColor | ( | ) | const [inherited] |
const Matrix3& sf::Drawable::GetInverseMatrix | ( | ) | const [protected, inherited] |
const Matrix3& sf::Drawable::GetMatrix | ( | ) | const [protected, inherited] |
const Vector2f& sf::Drawable::GetOrigin | ( | ) | const [inherited] |
float sf::Shape::GetOutlineThickness | ( | ) | const |
Get the thickness of the shape outline.
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.
index | Index of the point |
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.
index | Index of the point |
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.
index | Index of the point |
unsigned int sf::Shape::GetPointsCount | ( | ) | const |
Get the number of points composing the shape.
const Vector2f& sf::Drawable::GetPosition | ( | ) | const [inherited] |
float sf::Drawable::GetRotation | ( | ) | const [inherited] |
Get the orientation of the object.
The rotation is always in the range [0, 360].
const Vector2f& sf::Drawable::GetScale | ( | ) | const [inherited] |
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.
start | Start point |
end | End point |
thickness | Thickness of the line |
color | Color of the shape's points |
outline | Outline thickness |
outlineColor | Outline color of the shape's points |
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.
p1x | X coordinate of the start point |
p1y | Y coordinate of the start point |
p2x | X coordinate of the end point |
p2y | Y coordinate of the end point |
thickness | Thickness of the line |
color | Color of the shape's points |
outline | Outline thickness |
outlineColor | Outline color of the shape's points |
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);
offsetX | X offset |
offsetY | Y offset |
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);
offset | Offset |
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.
left | Left coordinate of the rectangle |
top | Top coordinate of the rectangle |
width | Width of the rectangle |
height | Height of the rectangle |
color | Color of the shape's points |
outline | Outline thickness |
outlineColor | Outline color of the shape's points |
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.
rectangle | Rectangle defining the shape |
color | Color of the shape's points |
outline | Outline thickness |
outlineColor | Outline color of the shape's points |
virtual void sf::Shape::Render | ( | RenderTarget & | target, |
Renderer & | renderer | ||
) | const [protected, virtual] |
Draw the object to a render target.
target | Render target |
renderer | Renderer 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);
angle | Angle of rotation, in degrees |
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);
factor | Scale factors |
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);
factorX | Horizontal scale factor |
factorY | Vertical scale factor |
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.
mode | New blending mode |
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.
color | New color |
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).
origin | New origin |
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).
x | X coordinate of the new origin |
y | Y coordinate of the new origin |
void sf::Shape::SetOutlineThickness | ( | float | thickness | ) |
Change the thickness of the shape outline.
thickness | New thickness of the outline |
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.
index | Index of the point |
color | New color of the point |
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.
index | Index of the point |
color | New outline color of the point |
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.
index | Index of the point |
position | New position of the point |
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.
index | Index of the point |
x | New X coordinate of the point |
y | New Y coordinate of the point |
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).
position | New position |
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).
x | X coordinate of the new position |
y | Y coordinate of the new position |
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.
angle | New rotation, in degrees |
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).
factorX | New horizontal scale factor |
factorY | New vertical scale factor |
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).
factors | New scale factors |
void sf::Drawable::SetScaleX | ( | float | factor | ) | [inherited] |
void sf::Drawable::SetScaleY | ( | float | factor | ) | [inherited] |
void sf::Drawable::SetX | ( | float | x | ) | [inherited] |
Set the X position of the object.
x | New X coordinate |
void sf::Drawable::SetY | ( | float | y | ) | [inherited] |
Set the Y position of the object.
y | New Y coordinate |
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).
point | Point to transform |
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).
point | Point to transform |