Abstract base class for objects that can be drawn to a render target. More...
#include <Drawable.hpp>
Public Member Functions | |
virtual | ~Drawable () |
Virtual destructor. | |
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. | |
Protected Member Functions | |
Drawable () | |
Default constructor. | |
const Matrix3 & | GetMatrix () const |
Get the transform matrix of the object. | |
const Matrix3 & | GetInverseMatrix () const |
Get the inverse transform matrix of the object. | |
Friends | |
class | RenderTarget |
Abstract base class for objects that can be drawn to a render target.
sf::Drawable defines the attributes and operations that are common to all the drawable classes:
Please note that all these attributes are hardware accelerated, therefore they are extremely cheap to use (unlike older libraries that perform slow transformations on the CPU, such as rotation or scale).
Usage example:
// Here we'll use a sf::Sprite to demonstrate the features of sf::Drawable sf::Sprite drawable = /* ...whatever... */; drawable.SetOrigin(10, 20); // set its origin to the local point (10, 20) drawable.SetPosition(100, 100); // set its position to (100, 100) drawable.SetRotation(45); // set its orientation to 45 degrees drawable.SetColor(sf::Color::Red); // set its global color to red drawable.SetBlendingMode(sf::Blend::Add); // set an additive blend mode window.Draw(drawable); // finally draw it (window is a sf::RenderWindow)
Deriving your own class from sf::Drawable is possible, however you have to use the sf::Renderer class instead of direct OpenGL calls, which is more limited. To create a derived drawable class, all you have to do is to override the virtual Render function.
One of the main benefits of creating your own drawable class is that you can build hierarchies of drawable objects. Indeed, when you draw a drawable inside the Render function of another drawable, the former inherits the transformations and color of the latter and combines them with its own attributes. This way, you can apply global transformations/color to a set of drawables as if it was a single entity.
Example:
class MyDrawable : public sf::Drawable { public : ... private : virtual void Render(sf::RenderTarget& target, sf::Renderer& renderer) const { // Low-level geometry rendering renderer.SetTexture(&myTexture); renderer.Begin(sf::Renderer::QuadList); renderer.AddVertex(...); renderer.AddVertex(...); renderer.AddVertex(...); renderer.AddVertex(...); renderer.End(); // High-level drawable rendering target.Draw(mySubSprite); } sf::Texture myTexture; sf::Sprite mySubSprite; };
Definition at line 62 of file Drawable.hpp.
virtual sf::Drawable::~Drawable | ( | ) | [virtual] |
Virtual destructor.
sf::Drawable::Drawable | ( | ) | [protected] |
Default constructor.
Blend::Mode sf::Drawable::GetBlendMode | ( | ) | const |
const Color& sf::Drawable::GetColor | ( | ) | const |
const Matrix3& sf::Drawable::GetInverseMatrix | ( | ) | const [protected] |
const Matrix3& sf::Drawable::GetMatrix | ( | ) | const [protected] |
const Vector2f& sf::Drawable::GetOrigin | ( | ) | const |
const Vector2f& sf::Drawable::GetPosition | ( | ) | const |
float sf::Drawable::GetRotation | ( | ) | const |
Get the orientation of the object.
The rotation is always in the range [0, 360].
const Vector2f& sf::Drawable::GetScale | ( | ) | const |
void sf::Drawable::Move | ( | float | offsetX, |
float | offsetY | ||
) |
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 | ) |
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 |
void sf::Drawable::Rotate | ( | float | angle | ) |
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 | ) |
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 | ||
) |
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 | ) |
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 | ) |
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 | ) |
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 | ||
) |
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::Drawable::SetPosition | ( | const Vector2f & | position | ) |
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 | ||
) |
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 | ) |
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 | ||
) |
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 | ) |
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 | ) |
void sf::Drawable::SetScaleY | ( | float | factor | ) |
void sf::Drawable::SetX | ( | float | x | ) |
Set the X position of the object.
x | New X coordinate |
void sf::Drawable::SetY | ( | float | y | ) |
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 |