Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | 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::unique_ptr< Sprite >
 Unique sprite pointer. More...
 

Public Member Functions

 Sprite ()
 Construct an empty sprite. More...
 
 Sprite (const std::string &texture, const UIntRect &rectangle={})
 Construct a sprite from a texture. More...
 
 Sprite (const Texture &texture, const UIntRect &rectangle={})
 Construct a sprite from a 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...
 
Sprite::Ptr copy () const
 Create a copy of the sprite. 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 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 setOpacity (unsigned int opacity)
 Set the opacity of the sprite. More...
 
unsigned int getOpacity () const
 Get the opacity 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 (const 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 (const 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 (const 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 (const 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 (const Vector2f &offset) override
 Move the sprite by a given offset. More...
 
void draw (priv::RenderTarget &renderTarget) const override
 Draw object on a render target. More...
 
AnimatorgetAnimator ()
 Get the sprites animator. More...
 
const AnimatorgetAnimator () const
 
void updateAnimation (Time deltaTime)
 
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 tag assigned to the object. More...
 
unsigned int getObjectId () const
 Get the unique id of the object. More...
 
int onPropertyChange (const std::string &property, const Callback< Property > &callback, bool oneTime=false)
 Add an event listener to a specific property change event. More...
 
int onPropertyChange (const Callback< Property > &callback, bool oneTime=false)
 Add an event listener to any property change event. More...
 
void suspendedEventListener (int id, bool suspend)
 Pause or resume execution of an event listener. More...
 
bool isEventListenerSuspended (int id) const
 Check if an event listener is suspended or not. More...
 
bool removeEventListener (const std::string &event, int id)
 Remove an event listener from an event. More...
 
bool removeEventListener (int id)
 Remove an event listener. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool isSameObjectAs (const Object &other) const
 Check if another object is the same instance as this object. More...
 

Static Public Member Functions

static Sprite::Ptr create (const std::string &texture, const UIntRect &rectangle={})
 Create a new Sprite instance. More...
 
static Sprite::Ptr create (const Texture &texture, const UIntRect &rectangle)
 Construct the sprite from a sub-rectangle of a source texture. More...
 

Protected Member Functions

void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emitDestruction ()
 Emit a destruction event. More...
 

Protected Attributes

EventEmitter eventEmitter_
 Event dispatcher. 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::unique_ptr<Sprite>

Unique sprite pointer.

Definition at line 49 of file Sprite.h.

Constructor & Destructor Documentation

◆ Sprite() [1/5]

ime::Sprite::Sprite ( )

Construct an empty sprite.

See also
setTexture and setTextureRect

◆ Sprite() [2/5]

ime::Sprite::Sprite ( const std::string &  texture,
const UIntRect rectangle = {} 
)
explicit

Construct a sprite from a texture.

Parameters
textureFilename of the texture to construct the sprite from
rectangleA sub-rectangle of the texture to assign to the sprite
Exceptions
FileNotFoundExceptionif the specified texture cannot be loaded

The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified

◆ Sprite() [3/5]

ime::Sprite::Sprite ( const Texture texture,
const UIntRect rectangle = {} 
)
explicit

Construct a sprite from a texture.

Parameters
textureTexture to construct sprite from
rectangleA sub-rectangle of the texture to assign to the sprite

The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified

◆ Sprite() [4/5]

ime::Sprite::Sprite ( const Sprite )

Copy constructor.

◆ Sprite() [5/5]

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

Move constructor.

◆ ~Sprite()

ime::Sprite::~Sprite ( )
override

Destructor.

Member Function Documentation

◆ copy()

Sprite::Ptr ime::Sprite::copy ( ) const

Create a copy of the sprite.

Returns
A copy of the sprite
See also
create

◆ create() [1/2]

static Sprite::Ptr ime::Sprite::create ( const std::string &  texture,
const UIntRect rectangle = {} 
)
static

Create a new Sprite instance.

Parameters
textureFilename of the texture to construct the sprite from
rectangleA sub-rectangle of the texture to assign to the sprite
Returns
The created Sprite instance
Exceptions
FileNotFoundExceptionif the specified texture cannot be loaded

The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified

See also
copy

◆ create() [2/2]

static Sprite::Ptr ime::Sprite::create ( const Texture texture,
const UIntRect rectangle 
)
static

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

The texture is copied, so theres no need to keep it alive. The rectangle can be used to construct the sprite from a sub-rectangle of the whole texture. To construct the sprite from the whole texture (default), leave the rectangle argument unspecified

See also
copy

◆ draw()

void ime::Sprite::draw ( priv::RenderTarget &  renderTarget) const
overridevirtual

Draw object on a render target.

Parameters
renderTargetTarget to draw object on
Note
This function is intended for internal use only

Implements ime::Drawable.

◆ 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

◆ emitDestruction()

void ime::Object::emitDestruction ( )
protectedinherited

Emit a destruction event.

Note
This function must be the first statement in the definition of a destructor to avoid undefined behavior. In addition, note that destruction listeners are invoked once. Therefore, multiple classes in a hierarchy may call this function but the class that makes the call first will be the one that invokes the destruction listeners

◆ 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
overridevirtualinherited

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.

◆ 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 unique id of the object.

Returns
The unique id of the object

Note that each instance of ime::Object has a unique id

See also
setTag

◆ getOpacity()

unsigned int ime::Sprite::getOpacity ( ) const

Get the opacity of the sprite.

Returns
The opacity of the sprite
See also
setOpacity

◆ 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 tag assigned to the object.

Returns
The tag of the object
See also
setTag

◆ getTexture()

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

Get the texture used by the sprite.

Returns
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

◆ isEventListenerSuspended()

bool ime::Object::isEventListenerSuspended ( int  id) const
inherited

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ isSameObjectAs()

bool ime::Object::isSameObjectAs ( const Object other) const
inherited

Check if another object is the same instance as this object.

Parameters
otherThe object to compare against this object
Returns
True if other is the same instance as this object, otherwise false

◆ 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 ( const 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.

◆ move() [2/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.

◆ 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 unique id of the destruction listener

The destruction listener is called when the object reaches the end of its lifetime. Note that an object may have multiple destruction listeners registered to it

Warning
It's not advised to call virtual functions in the destruction callback as some parts of the object may have already been destroyed by the time the callback is invoked. In such an event, the behavior is undefined
See also
removeEventListener

◆ onPropertyChange() [1/2]

int ime::Object::onPropertyChange ( const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to any property change event.

Parameters
callbackThe function to be executed when any property changes
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The unique id of the event listener

When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event

See also
onPropertyChange(const std::string&, const ime::Callback<ime::Property>&)

◆ onPropertyChange() [2/2]

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

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to add an event listener to
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
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, the setTag() function, modifies the tag property of the object, thus will generate a "tag" change event each time it is called

Note that multiple event listeners may be registered to the same property change event. In addition, 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");
See also
unsubscribe and onPropertyChange(const ime::Callback<ime::Property>&)

◆ operator=() [1/2]

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

Copy assignment operator.

◆ operator=() [2/2]

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

Move assignment operator.

◆ removeEventListener() [1/2]

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

Remove an event listener from an event.

Parameters
eventThe name of the event to remove an 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 tag of the object to console every time it changes
auto tagChangeId = object.onPropertyChange("tag", [](ime::Property tag) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the tag of the object when it changes
object.removeEventListener("tag", tagChangeId);
Class that can store a value of any type.
Definition: Property.h:38

◆ removeEventListener() [2/2]

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

Remove an event listener.

Parameters
idThe id of the event listener to be removed
Returns
True if the event listener was removed or false if no such handler exists

◆ 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 ( const 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.

◆ scale() [2/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.

◆ setColour()

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

Set the colour of the sprite.

Parameters
colourNew colour opf the sprite

◆ setOpacity()

void ime::Sprite::setOpacity ( unsigned int  opacity)

Set the opacity of the sprite.

Parameters
opacityThe new opacity of the sprite in the range [0, 255]

By default the opacity is 255

See also
getOpacity

◆ setOrigin() [1/2]

void ime::Sprite::setOrigin ( const 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.

◆ setOrigin() [2/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.

◆ setPosition() [1/2]

void ime::Sprite::setPosition ( const 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.

◆ setPosition() [2/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.

◆ 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 ( const 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.

◆ setScale() [2/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.

◆ setTag()

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

Assign the object an alias.

Parameters
tagThe alias of the object

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

By default, the tag is an empty string

See also
getObjectId

◆ 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
FileNotFoundExceptionif 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 is copied

◆ 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

◆ suspendedEventListener()

void ime::Object::suspendedEventListener ( int  id,
bool  suspend 
)
inherited

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

◆ 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

Member Data Documentation

◆ eventEmitter_

EventEmitter ime::Object::eventEmitter_
protectedinherited

Event dispatcher.

Definition at line 289 of file Object.h.


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