Skip to content

abstract class SF::Shape
inherits SF::Transformable #

Base class for textured shapes with outline

SF::Shape is a drawable class that allows to define and display a custom convex shape on a render target. It's only an abstract base, it needs to be specialized for concrete types of shapes (circle, rectangle, convex polygon, star, ...).

In addition to the attributes provided by the specialized shape classes, a shape always has the following attributes:

  • a texture
  • a texture rectangle
  • a fill color
  • an outline color
  • an outline thickness

Each feature is optional, and can be disabled easily:

  • the texture can be null
  • the fill/outline colors can be SF::Color::Transparent
  • the outline thickness can be zero

You can write your own derived shape class, there are only two virtual functions to override:

  • point_count must return the number of points of the shape
  • get_point must return the points of the shape

See also: SF::RectangleShape, SF::CircleShape, SF::ConvexShape, SF::Transformable

Included modules

SF::Drawable

Direct known subclasses

SF::CircleShape SF::ConvexShape SF::RectangleShape

Methods#

#fill_color : Color #

Get the fill color of the shape

Returns: Fill color of the shape

See also: fill_color=

View source

#fill_color=(color : Color) #

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.

  • color - New color of the shape

See also: fill_color, outline_color=

View source

#finalize #

Virtual destructor

View source

abstract #get_point(index : Int) : Vector2f #

Get a point of the shape

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.

  • index - Index of the point to get, in range 0 ... point_count

Returns: index-th point of the shape

See also: point_count

View source

#global_bounds : FloatRect #

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

View source

#local_bounds : FloatRect #

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

View source

#outline_color : Color #

Get the outline color of the shape

Returns: Outline color of the shape

See also: outline_color=

View source

#outline_color=(color : Color) #

Set the outline color of the shape

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

  • color - New outline color of the shape

See also: outline_color, fill_color=

View source

#outline_thickness : Float32 #

Get the outline thickness of the shape

Returns: Outline thickness of the shape

See also: outline_thickness=

View source

#outline_thickness=(thickness : Number) #

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.

  • thickness - New outline thickness

See also: outline_thickness

View source

abstract #point_count : Int32 #

Get the total number of points of the shape

Returns: Number of points of the shape

See also: point

View source

#set_texture(texture : Texture?, reset_rect : Bool = false) #

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 NULL to disable texturing. If reset_rect 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.

  • texture - New texture
  • reset_rect - Should the texture rect be reset to the size of the new texture?

See also: texture, texture_rect=

View source

#texture=(texture : Texture) #

Shorthand for set_texture

View source

#texture_rect : IntRect #

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

Returns: Texture rectangle of the shape

See also: texture_rect=

View source

#texture_rect=(rect : IntRect) #

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.

  • rect - Rectangle defining the region of the texture to display

See also: texture_rect, texture=

View source

#update #

Recompute the internal geometry of the shape

This function must be called by the derived class everytime the shape's points change (i.e. the result of either point_count or get_point is different).

View source