Public Types | Public Member Functions | Protected Member Functions | List of all members
ime::Sprite Class Reference

Drawable representation of a texture. More...

#include <Sprite.h>

Inheritance diagram for ime::Sprite:
ime::ITransformable ime::Drawable ime::Object

Public Types

using Ptr = std::shared_ptr< Sprite >
 Shared sprite pointer. More...
 

Public Member Functions

 Sprite ()
 Construct an empty sprite. More...
 
 Sprite (const std::string &texture)
 Construct a sprite from a texture. More...
 
 Sprite (const Texture &texture)
 Construct a sprite from a texture. More...
 
 Sprite (const Texture &texture, const UIntRect &rectangle)
 Construct the sprite from a sub-rectangle of a source texture. More...
 
 Sprite (const Sprite &)
 Copy constructor. More...
 
Spriteoperator= (const Sprite &)
 Copy assignment operator. More...
 
 Sprite (Sprite &&) noexcept
 Move constructor. More...
 
Spriteoperator= (Sprite &&) noexcept
 Move assignment operator. More...
 
std::string getClassName () const override
 Get the name of this class. More...
 
void setTexture (const std::string &filename)
 Set the texture of the object. More...
 
void setTexture (const Texture &texture)
 Set the texture of the sprite from a source texture. More...
 
const TexturegetTexture () const
 Get the name of the texture used by the sprite. More...
 
void setTextureRect (unsigned int left, unsigned int top, unsigned int width, unsigned int height)
 Set the sub-rectangle of the texture that the sprite will display. More...
 
void setTextureRect (const UIntRect &rect)
 Set the sub-rectangle of the texture that the sprite will display. More...
 
UIntRect getTextureRect () const
 Get the sub-rectangle of the texture displayed by the sprite. More...
 
void setColour (Colour colour)
 Set the colour of the sprite. More...
 
Colour getColour () const
 Get the colour of the sprite. More...
 
void setVisible (bool visible)
 Show or hide the sprite. More...
 
bool isVisible () const
 Check whether or not the sprite is visible. More...
 
void toggleVisibility ()
 Toggle the visibility of the sprite. More...
 
FloatRect getLocalBounds () const
 Get the local bounding rectangle of the sprite. More...
 
FloatRect getGlobalBounds () const
 Get the global bounding rectangle of the sprite. More...
 
void setPosition (float x, float y) override
 Set the position of the sprite. More...
 
void setPosition (Vector2f position) override
 Set the position of the sprite. More...
 
Vector2f getPosition () const override
 Get the position of the sprite. More...
 
void setRotation (float angle) override
 Set the orientation of the sprite. More...
 
void rotate (float angle) override
 Rotate the sprite. More...
 
float getRotation () const override
 Get the orientation of the sprite. More...
 
void setScale (float factorX, float factorY) override
 Set the scale factors of the sprite. More...
 
void setScale (Vector2f scale) override
 Set the scale factor of the sprite. More...
 
void scale (float factorX, float factorY) override
 Scale the sprite by an offset. More...
 
void scale (Vector2f offset) override
 Scale the sprite by an offset. More...
 
Vector2f getScale () const override
 Get the current scale of the sprite. More...
 
void setOrigin (float x, float y) override
 Set the local origin of the sprite. More...
 
void setOrigin (Vector2f origin) override
 Set the local origin of the sprite. More...
 
Vector2f getOrigin () const override
 Get the local origin of the sprite. More...
 
void move (float offsetX, float offsetY) override
 Move the sprite by a given offset. More...
 
void move (Vector2f offset) override
 Move the sprite by a given offset. More...
 
void draw (Window &renderTarget) const override
 Draw object on a render target. More...
 
AnimatorgetAnimator ()
 Get the sprites animator. More...
 
void updateAnimation (Time deltaTime)
 Update the current animation. More...
 
void swap (Sprite &other)
 Swap this sprite with another sprite. More...
 
 ~Sprite () override
 Destructor. More...
 
std::string getClassType () const override
 Get the name of this class. More...
 
void setTag (const std::string &tag)
 Assign the object an alias. More...
 
const std::string & getTag () const
 Get the alias of the object. More...
 
unsigned int getObjectId () const
 Get the id of the object. More...
 
int onPropertyChange (const std::string &property, const Callback< Property > &callback)
 Add an event listener to a specific property change event. More...
 
void onPropertyChange (const Callback< Property > &callback)
 Add an event listener to a property change event. More...
 
int onEvent (const std::string &event, const Callback<> &callback)
 Add an event listener to an event. More...
 
bool unsubscribe (const std::string &event, int id)
 Remove an event listener from an event. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool removeDestructionListener (int id)
 Remove a destruction listener form the object. More...
 
bool operator== (const Object &rhs) const
 Check if two objects are the same object or not. More...
 
bool operator!= (const Object &rhs) const
 Check if two objects are not the same object. More...
 

Protected Member Functions

void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emit (const std::string &event)
 Dispatch an action event. More...
 

Detailed Description

Drawable representation of a texture.

The sprite can be static (display a single non changing texture) or animated via its animator (see the getAnimator function)

Definition at line 47 of file Sprite.h.

Member Typedef Documentation

◆ Ptr

using ime::Sprite::Ptr = std::shared_ptr<Sprite>

Shared sprite pointer.

Definition at line 49 of file Sprite.h.

Constructor & Destructor Documentation

◆ Sprite() [1/6]

ime::Sprite::Sprite ( )

Construct an empty sprite.

See also
setTexture and setTextureRect

◆ Sprite() [2/6]

ime::Sprite::Sprite ( const std::string &  texture)
explicit

Construct a sprite from a texture.

Parameters
textureFilename of the texture to construct the sprite from
See also
setTextureRect

◆ Sprite() [3/6]

ime::Sprite::Sprite ( const Texture texture)
explicit

Construct a sprite from a texture.

Parameters
textureTexture to construct sprite from
See also
setTextureRect

◆ Sprite() [4/6]

ime::Sprite::Sprite ( const Texture texture,
const UIntRect rectangle 
)

Construct the sprite from a sub-rectangle of a source texture.

Parameters
textureThe source texture
rectangleSub-rectangle of the texture to assign to the sprite

This function is a shortcut for:

auto sprite = Sprite();
sprite.setTexture(texture);
sprite.setTextureRect(rectangle);
See also
setTexture and setTextureRect

◆ Sprite() [5/6]

ime::Sprite::Sprite ( const Sprite )

Copy constructor.

◆ Sprite() [6/6]

ime::Sprite::Sprite ( Sprite &&  )
noexcept

Move constructor.

◆ ~Sprite()

ime::Sprite::~Sprite ( )
override

Destructor.

Member Function Documentation

◆ draw()

void ime::Sprite::draw ( Window renderTarget) const
overridevirtual

Draw object on a render target.

Parameters
renderTargetTarget to draw object on

Implements ime::Drawable.

◆ emit()

void ime::Object::emit ( const std::string &  event)
protectedinherited

Dispatch an action event.

Parameters
eventThe name of the event to be dispatched

This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)

See also
emitChange

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ getAnimator()

Animator& ime::Sprite::getAnimator ( )

Get the sprites animator.

Returns
The sprites animator

This function enables the sprite to be animated

checkout the Animator class

◆ getClassName()

std::string ime::Sprite::getClassName ( ) const
overridevirtual

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

std::string ime::Drawable::getClassType ( ) const
inlineoverridevirtualinherited

Get the name of this class.

Returns
The name of this class

Note that this function is only overridden by child classes of Object which also serve as a base class for other classes

See also
Object::getClassType and Object::getClassName

Reimplemented from ime::Object.

Reimplemented in ime::Shape.

Definition at line 48 of file Drawable.h.

◆ getColour()

Colour ime::Sprite::getColour ( ) const

Get the colour of the sprite.

Returns
The colour of the sprite

◆ getGlobalBounds()

FloatRect ime::Sprite::getGlobalBounds ( ) const

Get the global bounding rectangle of the sprite.

Returns
Global bounding rectangle of the sprite

The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, ...) that are applied to the sprite. In other words, this function returns the bounds of the sprite in the global 2D world's coordinate system

◆ getLocalBounds()

FloatRect ime::Sprite::getLocalBounds ( ) const

Get the local bounding rectangle of the sprite.

Returns
Local bounding rectangle of the sprite

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the sprite. In other words, this function returns the bounds of the sprite in the sprite's coordinate system

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the id of the object.

Returns
The id of the object

Each object has a unique id

◆ getOrigin()

Vector2f ime::Sprite::getOrigin ( ) const
overridevirtual

Get the local origin of the sprite.

Returns
Local origin of the sprite

Implements ime::ITransformable.

◆ getPosition()

Vector2f ime::Sprite::getPosition ( ) const
overridevirtual

Get the position of the sprite.

Returns
Current position of the sprite

Implements ime::ITransformable.

◆ getRotation()

float ime::Sprite::getRotation ( ) const
overridevirtual

Get the orientation of the sprite.

Returns
Current rotation, in degrees

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

Implements ime::ITransformable.

◆ getScale()

Vector2f ime::Sprite::getScale ( ) const
overridevirtual

Get the current scale of the sprite.

Returns
Current scale of the sprite

Implements ime::ITransformable.

◆ getTag()

const std::string& ime::Object::getTag ( ) const
inherited

Get the alias of the object.

Returns
The alias of the object
See also
setTag and getObjectId

◆ getTexture()

const Texture& ime::Sprite::getTexture ( ) const

Get the name of the texture used by the sprite.

Returns
Name of the texture used by the sprite

◆ getTextureRect()

UIntRect ime::Sprite::getTextureRect ( ) const

Get the sub-rectangle of the texture displayed by the sprite.

Returns
The sub-rectangle of the texture displayed by the sprite

◆ isVisible()

bool ime::Sprite::isVisible ( ) const

Check whether or not the sprite is visible.

Returns
True if visible, otherwise false

◆ move() [1/2]

void ime::Sprite::move ( float  offsetX,
float  offsetY 
)
overridevirtual

Move the sprite by a given offset.

Parameters
offsetXHorizontal offset
offsetYVertical offset

This function adds to the current position of the sprite, unlike setPosition which overwrites it

See also
setPosition

Implements ime::ITransformable.

◆ move() [2/2]

void ime::Sprite::move ( Vector2f  offset)
overridevirtual

Move the sprite by a given offset.

Parameters
offsetOffset to apply

This function adds to the current position of the sprite, unlike setPosition which overwrites it

See also
setPosition

Implements ime::ITransformable.

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The id of the destruction listener

Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time

Warning
The callback is called when the object is destroyed. Do not try to access the object in the callback. Doing so is undefined behavior
See also
removeDestructionListener

◆ onEvent()

int ime::Object::onEvent ( const std::string &  event,
const Callback<> &  callback 
)
inherited

Add an event listener to an event.

Parameters
eventThe name of the event to add an an event listener to
callbackThe function to be executed when the event takes place
Returns
The unique identification number of the event listener

Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:

// Add event listeners to the object
object.onEvent("attachRigidBody", [] {
std::cout << "Rigid body attached to object << std::endl;
});
object.onEvent("removeRigidBody", [] {
std::cout << "Rigid body removed from object << std::endl;
});
// Invokes event listener(s)
object.attachRigidBody(body);
object.removeRigidBody();
See also
onPropertyChange and unsubscribe

◆ onPropertyChange() [1/2]

void ime::Object::onPropertyChange ( const Callback< Property > &  callback)
inherited

Add an event listener to a property change event.

Parameters
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.

See also
onPropertyChange(std::string, Callback)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to listen for
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, for the setTag function, the property that the function modifies is Tag.

Note that when adding a property change event listener, the name of the property must be in lowercase:

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");

Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time

See also
unsubscribe and onPropertyChange(Callback)

◆ operator!=()

bool ime::Object::operator!= ( const Object rhs) const
inherited

Check if two objects are not the same object.

Parameters
rhsObject to compare against this object
Returns
True if rhs is NOT the same object as this object, otherwise false

Two objects are different from each other if they have different object id's

See also
getObjectId and operator==

◆ operator=() [1/2]

Sprite& ime::Sprite::operator= ( const Sprite )

Copy assignment operator.

◆ operator=() [2/2]

Sprite& ime::Sprite::operator= ( Sprite &&  )
noexcept

Move assignment operator.

◆ operator==()

bool ime::Object::operator== ( const Object rhs) const
inherited

Check if two objects are the same object or not.

Parameters
rhsObject to compare against this object
Returns
True if rhs is the same object as this object, otherwise false

Two objects are the same object if they have the same object id. Recall that each object instance has a unique id

See also
getObjectId and operator!=

◆ removeDestructionListener()

bool ime::Object::removeDestructionListener ( int  id)
inherited

Remove a destruction listener form the object.

Parameters
Theid of the destruction listener to be removed
Returns
True if the destruction listener was removed or false if the listener with the given id does not exist

◆ rotate()

void ime::Sprite::rotate ( float  angle)
overridevirtual

Rotate the sprite.

Parameters
angleAngle of rotation, in degrees

This function adds to the current rotation of the sprite, unlike setRotation which overwrites it

See also
setRotation

Implements ime::ITransformable.

◆ scale() [1/2]

void ime::Sprite::scale ( float  factorX,
float  factorY 
)
overridevirtual

Scale the sprite by an offset.

Parameters
factorXHorizontal scale factor
factorYVertical scale factor

This function multiplies the current scale of the sprite, unlike setScale which overwrites it

See also
setScale

Implements ime::ITransformable.

◆ scale() [2/2]

void ime::Sprite::scale ( Vector2f  offset)
overridevirtual

Scale the sprite by an offset.

Parameters
offsetOffset to apply

This function multiplies the current scale of the sprite, unlike setScale which overwrites it

See also
setScale

Implements ime::ITransformable.

◆ setColour()

void ime::Sprite::setColour ( Colour  colour)

Set the colour of the sprite.

Parameters
colourNew colour opf the sprite

◆ setOrigin() [1/2]

void ime::Sprite::setOrigin ( float  x,
float  y 
)
overridevirtual

Set the local origin of the sprite.

Parameters
xX coordinate of the new origin
yY coordinate of the new origin

The origin of the sprite 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 sprite, and ignore all transformations (position, scale, rotation).

The default origin of the sprite is (0, 0)

Implements ime::ITransformable.

◆ setOrigin() [2/2]

void ime::Sprite::setOrigin ( Vector2f  origin)
overridevirtual

Set the local origin of the sprite.

Parameters
originNew origin

The origin of the sprite 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 sprite, and ignore all transformations (position, scale, rotation).

The default origin of the sprite is (0, 0)

Implements ime::ITransformable.

◆ setPosition() [1/2]

void ime::Sprite::setPosition ( float  x,
float  y 
)
overridevirtual

Set the position of the sprite.

Parameters
xX coordinate of the new position
yY coordinate of the new position

This function completely overwrites the previous position. use move function to apply an offset based on the previous position instead

The default position of a the sprite is (0, 0)

See also
move

Implements ime::ITransformable.

◆ setPosition() [2/2]

void ime::Sprite::setPosition ( Vector2f  position)
overridevirtual

Set the position of the sprite.

Parameters
positionNew position

This function completely overwrites the previous position. Use the move function to apply an offset based on the previous position instead.

The default position of the sprite is (0, 0)

See also
move

Implements ime::ITransformable.

◆ setRotation()

void ime::Sprite::setRotation ( float  angle)
overridevirtual

Set the orientation of the sprite.

Parameters
angleNew rotation, in degrees

This function completely overwrites the previous rotation. See the rotate function to add an angle based on the previous rotation instead.

The default rotation of the sprite is 0

See also
rotate

Implements ime::ITransformable.

◆ setScale() [1/2]

void ime::Sprite::setScale ( float  factorX,
float  factorY 
)
overridevirtual

Set the scale factors of the sprite.

Parameters
factorXNew horizontal scale factor
factorYNew vertical scale factor

This function completely overwrites the previous scale

See also
scale

Implements ime::ITransformable.

◆ setScale() [2/2]

void ime::Sprite::setScale ( Vector2f  scale)
overridevirtual

Set the scale factor of the sprite.

Parameters
scaleNew scale

This function completely overwrites the previous scale

See also
scale

Implements ime::ITransformable.

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
nameThe alias of the object

This function is useful if you want to refer to the object by tag instead of its id. Unlike an object id, multiple objects may have the same tag.

By default, the tag is an empty string

◆ setTexture() [1/2]

void ime::Sprite::setTexture ( const std::string &  filename)

Set the texture of the object.

Parameters
filenameFilename of the texture to set
Exceptions
FileNotFoundif the specified texture cannot be found in the images path

This function will set the texture to the whole image

◆ setTexture() [2/2]

void ime::Sprite::setTexture ( const Texture texture)

Set the texture of the sprite from a source texture.

Parameters
textureThe source texture

The texture argument refers to a texture that must exist as long as the sprite uses it. Indeed, the sprite 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 sprite tries to use it, the behavior is undefined

◆ setTextureRect() [1/2]

void ime::Sprite::setTextureRect ( const UIntRect rect)

Set the sub-rectangle of the texture that the sprite will display.

Parameters
rectThe rectangle to set

This function will set the texture that the sprite displays to a portion of the entire texture.

By default, the sprite displays the entire texture

◆ setTextureRect() [2/2]

void ime::Sprite::setTextureRect ( unsigned int  left,
unsigned int  top,
unsigned int  width,
unsigned int  height 
)

Set the sub-rectangle of the texture that the sprite will display.

Parameters
lefthorizontal starting position
topVertical starting position
widthWidth of the sub-rectangle
heightHeight of the sub-rectangle

This function will set the texture that the sprite displays to a portion of the entire texture. The displaying will start at the specified starting position

By default, the sprite displays the entire texture

◆ setVisible()

void ime::Sprite::setVisible ( bool  visible)

Show or hide the sprite.

Parameters
visibleTrue to show or false to hide

When hidden the sprite will not be shown on the render target

By default, the sprite is visible

◆ swap()

void ime::Sprite::swap ( Sprite other)

Swap this sprite with another sprite.

Parameters
otherThe sprite to be swapped with this sprite

◆ toggleVisibility()

void ime::Sprite::toggleVisibility ( )

Toggle the visibility of the sprite.

This function will hide the sprite if its currently visible or show it if it is currently hidden

◆ unsubscribe()

bool ime::Object::unsubscribe ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventThe name of the event to remove event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the name of the object to console every time it changes
auto nameChangeId = object.onPropertyChange("name", [](Property name) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the name of the object when it changes
object.unsubscribe("name", id);

◆ updateAnimation()

void ime::Sprite::updateAnimation ( Time  deltaTime)

Update the current animation.

Parameters
deltaTimeTime passed since last animation update

This function need only be called if the sprite is animatable and not just displaying a single static image


The documentation for this class was generated from the following file:
ime::Sprite::Sprite
Sprite()
Construct an empty sprite.