sf::Drawable Class Reference
[Graphics module]

Abstract base class for objects that can be drawn to a render target. More...

#include <Drawable.hpp>

Inheritance diagram for sf::Drawable:
sf::Shape sf::Sprite sf::Text

List of all members.

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 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.

Protected Member Functions

 Drawable ()
 Default constructor.
const Matrix3GetMatrix () const
 Get the transform matrix of the object.
const Matrix3GetInverseMatrix () const
 Get the inverse transform matrix of the object.

Friends

class RenderTarget

Detailed Description

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;
 };
See also:
sf::Shape, sf::Sprite, sf::Text

Definition at line 62 of file Drawable.hpp.


Constructor & Destructor Documentation

virtual sf::Drawable::~Drawable (  )  [virtual]

Virtual destructor.

sf::Drawable::Drawable (  )  [protected]

Default constructor.


Member Function Documentation

Blend::Mode sf::Drawable::GetBlendMode (  )  const

Get the blend mode of the object.

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

Get the color of the object.

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

Get the inverse transform matrix of the object.

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

Get the transform matrix of the object.

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

Get the local origin of the object.

Returns:
Current origin
See also:
SetOrigin
const Vector2f& sf::Drawable::GetPosition (  )  const

Get the position of the object.

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

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

Get the current scale of the object.

Returns:
Current scale factors
See also:
SetScale
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);
Parameters:
offset Offset
See also:
SetPosition
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);
Parameters:
offsetX X offset
offsetY Y offset
See also:
SetPosition
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);
Parameters:
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);
Parameters:
factor Scale factors
See also:
SetScale
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);
Parameters:
factorX Horizontal scale factor
factorY Vertical scale factor
See also:
SetScale
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.

Parameters:
mode New blending mode
See also:
GetBlendMode
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.

Parameters:
color New color
See also:
GetColor
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).

Parameters:
origin New origin
See also:
GetOrigin
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).

Parameters:
x X coordinate of the new origin
y Y coordinate of the new origin
See also:
GetOrigin
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).

Parameters:
position New position
See also:
Move, SetX, SetY, GetPosition
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).

Parameters:
x X coordinate of the new position
y Y coordinate of the new position
See also:
Move, SetX, SetY, GetPosition
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.

Parameters:
angle New rotation, in degrees
See also:
Rotate, GetRotation
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).

Parameters:
factors New scale factors
See also:
Scale, SetScaleX, SetScaleY, GetScale
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).

Parameters:
factorX New horizontal scale factor
factorY New vertical scale factor
See also:
Scale, SetScaleX, SetScaleY, GetScale
void sf::Drawable::SetScaleX ( float  factor  ) 

Set the X scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factor New horizontal scale factor
See also:
SetScaleY, SetScale, GetScale
void sf::Drawable::SetScaleY ( float  factor  ) 

Set the Y scale factor of the object.

factor must be strictly positive, otherwise it is ignored.

Parameters:
factor New vertical scale factor
See also:
SetScaleX, SetScale, GetScale
void sf::Drawable::SetX ( float  x  ) 

Set the X position of the object.

Parameters:
x New X coordinate
See also:
SetY, SetPosition, GetPosition
void sf::Drawable::SetY ( float  y  ) 

Set the Y position of the object.

Parameters:
y New Y coordinate
See also:
SetX, SetPosition, GetPosition
Vector2f sf::Drawable::TransformToGlobal ( const Vector2f point  )  const

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:
point Point to transform
Returns:
The transformed point
See also:
TransformToLocal
Vector2f sf::Drawable::TransformToLocal ( const Vector2f point  )  const

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:
point Point to transform
Returns:
The transformed point
See also:
TransformToGlobal

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