An ImageSprite which contains images of the same size arranged in a grid. More...
#include <SpriteSheet.h>
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 () | |
Default constructor. More... | |
SpriteSheet (const std::string &sourceTexture, Vector2u frameSize, Vector2u spacing={0, 0}, UIntRect area={}) | |
Constructor. More... | |
void | create (const std::string &sourceTexture, Vector2u frameSize, Vector2u spacing={0, 0}, UIntRect area={}) |
Create the spritesheet. More... | |
SpriteSheet (const SpriteSheet &)=default | |
Copy constructor. More... | |
SpriteSheet & | operator= (const SpriteSheet &)=default |
Copy assignment operator. More... | |
SpriteSheet (SpriteSheet &&) noexcept=default | |
Move constructor. More... | |
SpriteSheet & | operator= (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< Frame > | getFrame (Index index) const |
Get the frame at a given index. More... | |
std::optional< Frame > | getFrame (const std::string &alias) const |
Get the frame by its assigned alias. More... | |
std::vector< Frame > | getFramesOnRow (unsigned int row) const |
Get all the frames in a given row. More... | |
std::vector< Frame > | getFramesOnColumn (unsigned int column) const |
Get all the frames in a given column. More... | |
std::vector< Frame > | getFramesInRange (Index start, Index end) const |
Get all the frames in a range. More... | |
std::vector< Frame > | getAllFrames () 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< Sprite > | getSpritesOnRow (unsigned int row) const |
Get all the sprites in a row. More... | |
std::vector< Sprite > | getSpritesOnColumn (unsigned int column) const |
Get all the sprites in a column. More... | |
std::vector< Sprite > | getSpritesInRange (Index start, Index end) const |
Get all the sprites in a range. More... | |
std::vector< Sprite > | getAllSprites () 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... | |
void | create (const std::string &sourceTexture, UIntRect area={}) |
Create the sprite image. 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 Texture & | getTexture () 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 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... | |
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 | emitDestruction () |
Emit a destruction event. More... | |
Protected Attributes | |
EventEmitter | eventEmitter_ |
Event dispatcher. More... | |
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.
using ime::SpriteSheet::Frame = UIntRect |
A frame in the spritesheet.
Definition at line 55 of file SpriteSheet.h.
using ime::SpriteSheet::Ptr = std::shared_ptr<SpriteSheet> |
shared spritesheet pointer
Definition at line 54 of file SpriteSheet.h.
ime::SpriteSheet::SpriteSheet | ( | ) |
ime::SpriteSheet::SpriteSheet | ( | const std::string & | sourceTexture, |
Vector2u | frameSize, | ||
Vector2u | spacing = {0, 0} , |
||
UIntRect | area = {} |
||
) |
Constructor.
sourceTexture | Filename of the image used by the spritesheet |
frameSize | The size of each frame in the source texture |
spacing | The space between frames in the source texture |
area | Sub-rectangle to construct spritesheet from |
FileNotFount | if 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
|
default |
Copy constructor.
|
defaultnoexcept |
Move constructor.
|
override |
Destructor.
bool ime::SpriteSheet::assignAlias | ( | Index | index, |
const std::string & | alias | ||
) |
Assign an alias to a frame.
index | Index of the frame to assign an alias |
alias | The alias to be assigned to the frame |
This function allows a frame or a sprite to be accessed by its name instead of its index
|
protectedinherited |
Check if the sprite image contains a point or not.
point | The point to be checked |
|
inherited |
Create the sprite image.
sourceTexture | The filename of the sprite image on the disk |
area | Sub-rectangle to construct sprite image from |
FileNotFoundException | If the image could not be found on the disk |
The area can be used to construct the sprite image from a sub-rectangle of the sourceTexture. To construct the sprite image from the whole sourceTexture (default), leave the area argument unspecified. If the area rectangle crosses the bounds of the sourceTexture, it is adjusted to fit the sourceTexture size
void ime::SpriteSheet::create | ( | const std::string & | sourceTexture, |
Vector2u | frameSize, | ||
Vector2u | spacing = {0, 0} , |
||
UIntRect | area = {} |
||
) |
Create the spritesheet.
sourceTexture | Filename of the image used by the spritesheet |
frameSize | The size of each frame in the source texture |
spacing | The space between frames in the source texture |
area | Sub-rectangle to construct spritesheet from |
FileNotFount | if 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
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
protectedinherited |
Emit a destruction event.
std::vector< Frame > ime::SpriteSheet::getAllFrames | ( | ) | const |
Get all the frames in the spritesheet.
std::vector< Sprite > ime::SpriteSheet::getAllSprites | ( | ) | const |
Get all the sprites in the spritesheet.
|
overridevirtual |
|
overridevirtualinherited |
Get 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
Reimplemented from ime::Object.
unsigned int ime::SpriteSheet::getColumnCount | ( | ) | const |
Get the number of columns in the spritesheet.
std::optional< Frame > ime::SpriteSheet::getFrame | ( | const std::string & | alias | ) | const |
Get the frame by its assigned alias.
alias | The alias assigned to a frame |
Get the frame at a given index.
index | The index to get the frame of |
Note that index starts at {0, 0}
std::size_t ime::SpriteSheet::getFramesCount | ( | ) | const |
Get the number of frames in the spritesheet.
Note that the number of sprites in the spritesheet is the same as the number of frames
Get all the frames in a range.
start | The start of the range (inclusive) |
end | The end of the range (inclusive) |
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}
Vector2u ime::SpriteSheet::getFrameSize | ( | ) | const |
Get the size of each frame in the spritesheet.
std::vector< Frame > ime::SpriteSheet::getFramesOnColumn | ( | unsigned int | column | ) | const |
Get all the frames in a given column.
column | The column to get the frames from |
Note that column starts at 0
std::vector< Frame > ime::SpriteSheet::getFramesOnRow | ( | unsigned int | row | ) | const |
Get all the frames in a given row.
row | The row to get the frames from |
Note that row starts at 0
|
inherited |
Get the height of the sprite image in pixels.
|
inherited |
Get the unique id of the object.
Note that each instance of ime::Object has a unique id
|
inherited |
Get the top-left position of the sprite image relative to the sprite image source texture.
If the sprite image was created from the whole source texture, this function returns {0, 0}
unsigned int ime::SpriteSheet::getRowCount | ( | ) | const |
Get the number of rows in the spritesheet.
|
inherited |
Get the size of the sprite image in pixels.
The x component is width whilst the y component is the height of the sprite image
Vector2u ime::SpriteSheet::getSizeInFrames | ( | ) | const |
Get the size of the spritesheet in frames.
Vector2u ime::SpriteSheet::getSpaceBetweenFrames | ( | ) | const |
Get the space between frames in the spritesheet.
Sprite ime::SpriteSheet::getSprite | ( | const std::string & | alias | ) | const |
Get a sprite from the index with a given alias.
alias | The name given to a frame in the spritesheet |
Get a sprite from an index.
index | Index to get the sprite from |
Note than index starts at {0, 0}
Get all the sprites in a range.
start | The start of the range (inclusive) |
end | The end of the range (inclusive) |
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}
std::vector< Sprite > ime::SpriteSheet::getSpritesOnColumn | ( | unsigned int | column | ) | const |
Get all the sprites in a column.
column | The column to get the sprites from |
Note that column starts at 0
std::vector< Sprite > ime::SpriteSheet::getSpritesOnRow | ( | unsigned int | row | ) | const |
Get all the sprites in a row.
row | The row to get the sprites from |
Note that row starts at 0
|
inherited |
|
inherited |
Get the source texture of the sprite image.
|
inherited |
Get the width of the sprite image in pixels.
bool ime::SpriteSheet::hasFrame | ( | const std::string & | alias | ) | const |
Check if there is a frame with a specified alias.
alias | Name of the alias given to a frame |
bool ime::SpriteSheet::hasFrame | ( | Index | index | ) | const |
Check if an index has a frame or not.
index | Index to be checked |
Note that index starts at {0, 0}
|
inherited |
Check if an event listener is suspended or not.
id | The identification number of the listener to be checked |
This function also returns false if the specified event listener does not exist
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
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
|
inherited |
Add an event listener to any property change event.
callback | The function to be executed when any property changes |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to add an event listener to |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
callback | The function to be executed when the property changes |
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.
|
default |
Copy assignment operator.
|
defaultnoexcept |
Move assignment operator.
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove an event listener from |
id | The unique id of the event listener to be removed |
|
inherited |
Remove an event listener.
id | The id of the event listener to be removed |
|
inherited |
Assign the object an alias.
tag | The 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
|
inherited |
Pause or resume execution of an event listener.
id | The event listeners unique identification number |
suspend | True to suspend/pause or false to unsuspend/resume |
|
protectedinherited |