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

Sprite class forward declaration. More...

#include <SpriteSheet.h>

Inheritance diagram for ime::SpriteSheet:
ime::SpriteImage ime::Object

Public Types

using Ptr = std::shared_ptr< SpriteSheet >
 shared spritesheet pointer More...
 
using Frame = UIntRect
 A frame in the spritesheet. More...
 

Public Member Functions

 SpriteSheet (const std::string &sourceTexture, Vector2u frameSize, Vector2u spacing={0, 0}, UIntRect area={})
 Create the spritesheet. More...
 
 SpriteSheet (const SpriteSheet &)=default
 Copy constructor. More...
 
SpriteSheetoperator= (const SpriteSheet &)=default
 Copy assignment operator. More...
 
 SpriteSheet (SpriteSheet &&) noexcept=default
 Move constructor. More...
 
SpriteSheetoperator= (SpriteSheet &&) noexcept=default
 Move assignment operator. More...
 
std::string getClassName () const override
 Get the name of this class. More...
 
Vector2u getFrameSize () const
 Get the size of each frame in the spritesheet. More...
 
Vector2u getSpaceBetweenFrames () const
 Get the space between frames in the spritesheet. More...
 
std::size_t getFramesCount () const
 Get the number of frames in the spritesheet. More...
 
std::optional< FramegetFrame (Index index) const
 Get the frame at a given index. More...
 
std::optional< FramegetFrame (const std::string &alias) const
 Get the frame by its assigned alias. More...
 
std::vector< FramegetFramesOnRow (unsigned int row) const
 Get all the frames in a given row. More...
 
std::vector< FramegetFramesOnColumn (unsigned int column) const
 Get all the frames in a given column. More...
 
std::vector< FramegetFramesInRange (Index start, Index end) const
 Get all the frames in a range. More...
 
std::vector< FramegetAllFrames () const
 Get all the frames in the spritesheet. More...
 
Vector2u getSizeInFrames () const
 Get the size of the spritesheet in frames. More...
 
unsigned int getRowCount () const
 Get the number of rows in the spritesheet. More...
 
unsigned int getColumnCount () const
 Get the number of columns in the spritesheet. More...
 
Sprite getSprite (Index index) const
 Get a sprite from an index. More...
 
Sprite getSprite (const std::string &alias) const
 Get a sprite from the index with a given alias. More...
 
std::vector< SpritegetSpritesOnRow (unsigned int row) const
 Get all the sprites in a row. More...
 
std::vector< SpritegetSpritesOnColumn (unsigned int column) const
 Get all the sprites in a column. More...
 
std::vector< SpritegetSpritesInRange (Index start, Index end) const
 Get all the sprites in a range. More...
 
std::vector< SpritegetAllSprites () const
 Get all the sprites in the spritesheet. More...
 
bool hasFrame (Index index) const
 Check if an index has a frame or not. More...
 
bool hasFrame (const std::string &alias) const
 Check if there is a frame with a specified alias. More...
 
bool assignAlias (Index index, const std::string &alias)
 Assign an alias to a frame. More...
 
 ~SpriteSheet () override
 Destructor. More...
 
Vector2u getSize () const
 Get the size of the sprite image in pixels. More...
 
unsigned int getWidth () const
 Get the width of the sprite image in pixels. More...
 
unsigned int getHeight () const
 Get the height of the sprite image in pixels. More...
 
std::string getClassType () const override
 Get the name of this class. More...
 
const TexturegetTexture () const
 Get the source texture of the sprite image. More...
 
Vector2u getRelativePosition () const
 Get the top-left position of the sprite image relative to the sprite image source texture. 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

bool contains (Vector2u point) const
 Check if the sprite image contains a point or not. More...
 
void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emit (const std::string &event)
 Dispatch an action event. More...
 

Detailed Description

Sprite class forward declaration.

An ImageSprite which contains images of the same size arranged in a grid

This class only works with a SpriteImage that has contiguous textures (perfectly aligned vertically or horizontally) that are the same size. Use the TextureAtlas for textures that are different sizes and have no specific arrangement in the source texture. Usually a spritesheet holds animation frames of a single entity such as (but not limited to) a characters animation in an idle, walking, running or jumping state

Definition at line 52 of file SpriteSheet.h.

Member Typedef Documentation

◆ Frame

A frame in the spritesheet.

Definition at line 55 of file SpriteSheet.h.

◆ Ptr

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

shared spritesheet pointer

Definition at line 54 of file SpriteSheet.h.

Constructor & Destructor Documentation

◆ SpriteSheet() [1/3]

ime::SpriteSheet::SpriteSheet ( const std::string &  sourceTexture,
Vector2u  frameSize,
Vector2u  spacing = {0, 0},
UIntRect  area = {} 
)

Create the spritesheet.

Parameters
sourceTextureFilename of the image used by the spritesheet
frameSizeThe size of each frame in the source texture
spacingThe space between frames in the source texture
areaSub-rectangle to construct spritesheet from
Exceptions
FileNotFountif the the source image cannot be found on the disk

The area can be used to construct the spritesheet from a sub-rectangle of the whole spritesheet image. To construct the spritesheet from the whole image (default), leave the area argument unspecified. If the area rectangle crosses the bounds of the image, it is adjusted to fit the image size

Note
This class assumes that if the spacing between frames in non- zero then the frame has spacing on both sides. For example if the spacing is {1, 0}, it is assumed that each individual frame has a space of 1 pixel to its left and a space of 1 pixel to its right (The assumption applies for first frames)

◆ SpriteSheet() [2/3]

ime::SpriteSheet::SpriteSheet ( const SpriteSheet )
default

Copy constructor.

◆ SpriteSheet() [3/3]

ime::SpriteSheet::SpriteSheet ( SpriteSheet &&  )
defaultnoexcept

Move constructor.

◆ ~SpriteSheet()

ime::SpriteSheet::~SpriteSheet ( )
override

Destructor.

Member Function Documentation

◆ assignAlias()

bool ime::SpriteSheet::assignAlias ( Index  index,
const std::string &  alias 
)

Assign an alias to a frame.

Parameters
indexIndex of the frame to assign an alias
aliasThe alias to be assigned to the frame
Returns
True if the frame was assigned an alias

This function allows a frame or a sprite to be accessed by its name instead of its index

sprite image.assignAlias("blank_frame", Index{4, 0});
sprite image.getFrame("blank_frame"); //Returns frame at index 4, 0
//Returns a sprite displaying the image at index 4, 0
sprite image.getSprite("blank_frame");
See also
getFrame(const std::string&)

◆ contains()

bool ime::SpriteImage::contains ( Vector2u  point) const
protectedinherited

Check if the sprite image contains a point or not.

Parameters
pointThe point to be checked
Returns
True if the sprite image contains the point otherwise false

◆ 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

◆ getAllFrames()

std::vector<Frame> ime::SpriteSheet::getAllFrames ( ) const

Get all the frames in the spritesheet.

Returns
All the frames in the spritesheet

◆ getAllSprites()

std::vector<Sprite> ime::SpriteSheet::getAllSprites ( ) const

Get all the sprites in the spritesheet.

Returns
All the sprites in the spritesheet

◆ getClassName()

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

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

std::string ime::SpriteImage::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.

◆ getColumnCount()

unsigned int ime::SpriteSheet::getColumnCount ( ) const

Get the number of columns in the spritesheet.

Returns
The number of columns in the spritesheet

◆ getFrame() [1/2]

std::optional<Frame> ime::SpriteSheet::getFrame ( const std::string &  alias) const

Get the frame by its assigned alias.

Parameters
aliasThe alias assigned to a frame
Returns
The frame at the specified index if it is within bounds otherwise returns no value

◆ getFrame() [2/2]

std::optional<Frame> ime::SpriteSheet::getFrame ( Index  index) const

Get the frame at a given index.

Parameters
indexThe index to get the frame of
Returns
The frame at the specified index if it is within bounds otherwise returns no value

Note that index starts at {0, 0}

◆ getFramesCount()

std::size_t ime::SpriteSheet::getFramesCount ( ) const

Get the number of frames in the spritesheet.

Returns
The total number of frames in the spritesheet

Note that the number of sprites in the spritesheet is the same as the number of frames

◆ getFramesInRange()

std::vector<Frame> ime::SpriteSheet::getFramesInRange ( Index  start,
Index  end 
) const

Get all the frames in a range.

Parameters
startThe start of the range (inclusive)
endThe end of the range (inclusive)
Returns
All the frames in the specified range or an empty vector if the range is invalid

The range must either be on a row or column. For rows the x components of the start and end arguments must be the same. Similarly, for columns, the y components of the start and end must be the same otherwise an empty vector will be returned. In addition for components that varies (row or column), the start component must be less than the end component. An empty vector will also be returned if the either the start or end index is out of bounds

Note that start and end start at {0, 0}

//Returns all the frames in row 1 from column 2 to column 5
spritesheet.getFramesInRange(Index{1, 2}, Index{1, 5});
//Returns all the frames in column 4 from row 0 to row 5
spritesheet.getFramesInRange(Index{0, 4}, Index{5, 4});

◆ getFrameSize()

Vector2u ime::SpriteSheet::getFrameSize ( ) const

Get the size of each frame in the spritesheet.

Returns
The size of each frame in the spritesheet

◆ getFramesOnColumn()

std::vector<Frame> ime::SpriteSheet::getFramesOnColumn ( unsigned int  column) const

Get all the frames in a given column.

Parameters
columnThe column to get the frames from
Returns
All the frames in the specified column or an empty vector if the column is out of bounds

Note that column starts at 0

◆ getFramesOnRow()

std::vector<Frame> ime::SpriteSheet::getFramesOnRow ( unsigned int  row) const

Get all the frames in a given row.

Parameters
rowThe row to get the frames from
Returns
All the frames in the specified row or an empty vector if the row is out of bounds

Note that row starts at 0

◆ getHeight()

unsigned int ime::SpriteImage::getHeight ( ) const
inherited

Get the height of the sprite image in pixels.

Returns
The height of the sprite image in pixels

◆ 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

◆ getRelativePosition()

Vector2u ime::SpriteImage::getRelativePosition ( ) const
inherited

Get the top-left position of the sprite image relative to the sprite image source texture.

Returns
The relative top-left position

If the sprite image was created from the whole source texture, this function returns {0, 0}

See also
SpriteImage

◆ getRowCount()

unsigned int ime::SpriteSheet::getRowCount ( ) const

Get the number of rows in the spritesheet.

Returns
The number of rows in the spritesheet

◆ getSize()

Vector2u ime::SpriteImage::getSize ( ) const
inherited

Get the size of the sprite image in pixels.

Returns
The size of the sprite image in pixels

The x component is width whilst the y component is the height of the sprite image

◆ getSizeInFrames()

Vector2u ime::SpriteSheet::getSizeInFrames ( ) const

Get the size of the spritesheet in frames.

Returns
The size of the spritesheet in frames
Warning
The x component is the number of columns and the y component is the number of rows

◆ getSpaceBetweenFrames()

Vector2u ime::SpriteSheet::getSpaceBetweenFrames ( ) const

Get the space between frames in the spritesheet.

Returns
The space between frames in the spritesheet

◆ getSprite() [1/2]

Sprite ime::SpriteSheet::getSprite ( const std::string &  alias) const

Get a sprite from the index with a given alias.

Parameters
aliasThe name given to a frame in the spritesheet
Returns
The sprite at the aliased index or an empty sprite if there is no frame with the specified alias
See also
assignAlias

◆ getSprite() [2/2]

Sprite ime::SpriteSheet::getSprite ( Index  index) const

Get a sprite from an index.

Parameters
indexIndex to get the sprite from
Returns
The sprite at the given index, or an empty sprite if the index is out of bounds

Note than index starts at {0, 0}

◆ getSpritesInRange()

std::vector<Sprite> ime::SpriteSheet::getSpritesInRange ( Index  start,
Index  end 
) const

Get all the sprites in a range.

Parameters
startThe start of the range (inclusive)
endThe end of the range (inclusive)
Returns
All the sprites in the specified range or an empty vector if the range is invalid

The range must either be on a row or column. For rows the x components of the start and end arguments must be the same. Similarly, for columns, the y components of the start and end must be the same otherwise an empty vector will be returned. In addition for components that varies (row or column), the start component must be less than the end component. An empty vector will also be returned if the either the start or end index is out of bounds

Note that start and end start at {0, 0}

//Returns all the sprites in row 1 from column 2 to column 5
spritesheet.getSpritesInRange(Index{1, 2}, Index{1, 5});
//Returns all the sprites in column 4 from row 0 to row 5
spritesheet.getSpritesInRange(Index{0, 4}, Index{5, 4});

◆ getSpritesOnColumn()

std::vector<Sprite> ime::SpriteSheet::getSpritesOnColumn ( unsigned int  column) const

Get all the sprites in a column.

Parameters
columnThe column to get the sprites from
Returns
All the sprites in the specified column or an empty vector if the column is out of bounds

Note that column starts at 0

See also
getSpritesOnRows

◆ getSpritesOnRow()

std::vector<Sprite> ime::SpriteSheet::getSpritesOnRow ( unsigned int  row) const

Get all the sprites in a row.

Parameters
rowThe row to get the sprites from
Returns
All the sprites in the specified row or an empty vector if the row is out of bounds

Note that row starts at 0

See also
getSpritesOnColumn

◆ 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::SpriteImage::getTexture ( ) const
inherited

Get the source texture of the sprite image.

Returns
The source texture of the sprite image
Warning
Don't call this function when the sprite image is not yet created. Also, the texture is destroyed when the sprite image is destroyed, exercise caution when there are Sprite objects referencing the sprite image texture
See also
create

◆ getWidth()

unsigned int ime::SpriteImage::getWidth ( ) const
inherited

Get the width of the sprite image in pixels.

Returns
The width of the sprite image in pixels

◆ hasFrame() [1/2]

bool ime::SpriteSheet::hasFrame ( const std::string &  alias) const

Check if there is a frame with a specified alias.

Parameters
aliasName of the alias given to a frame
Returns
True if a frame with the specified alias exist or false if there is no frame with the given alias
See also
assignAlias

◆ hasFrame() [2/2]

bool ime::SpriteSheet::hasFrame ( Index  index) const

Check if an index has a frame or not.

Parameters
indexIndex to be checked
Returns
True if there is a frame at the specified index of false if the index is invalid

Note that index starts at {0, 0}

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

SpriteSheet& ime::SpriteSheet::operator= ( const SpriteSheet )
default

Copy assignment operator.

◆ operator=() [2/2]

SpriteSheet& ime::SpriteSheet::operator= ( SpriteSheet &&  )
defaultnoexcept

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

◆ 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

◆ 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);

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