SDL2lazy
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SlSprite Class Reference

#include <SlSprite.h>

Collaboration diagram for SlSprite:
Collaboration graph
[legend]

Public Member Functions

 SlSprite (const std::string &name, SlTexture *texture, int x=0, int y=0, int width=0, int height=0)
 
 SlSprite (const SlSprite &)=delete
 
SlSprite & operator= (const SlSprite &)=delete
 
SlSprite * addDefaultDestination ()
 
SlSprite * addDestination (int x, int y, uint32_t renderOptions=SL_RENDER_DEFAULT)
 
SlSprite * centerInSprite (const std::shared_ptr< SlSprite > otherSprite, unsigned int destinationThis=0, unsigned int destinationOther=0)
 
SlSprite * centerAt (unsigned int x, unsigned int y, unsigned int destination=0)
 
void clearDestinations ()
 
SDL_Rect destination (unsigned int i=0)
 
void destinationDimension (int &width, int &height, unsigned int i=0)
 
void destinationOrigin (int &x, int &y, unsigned int i=0)
 
bool hasDestination ()
 
bool is_inside (const int &x, const int &y, const unsigned int &dest=0)
 
void moveDestinationOriginBy (int x, int y, unsigned int i=0)
 
std::string name () const
 
void render (SDL_Renderer *renderer)
 
void render (SDL_Renderer *renderer, unsigned int i)
 
void setAngle (double angle, unsigned int i=0)
 
void setColor (uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha=0xFF, unsigned int i=0)
 
void setDestination (const SDL_Rect &dstRect, unsigned int i=0)
 
void setDestination (int x, int y, int w, int h, unsigned int i=0)
 
void setDestinationDimension (int width, int height, unsigned int i=0)
 
void setDestinationOrigin (int x, int y, unsigned int i=0)
 
void setRenderOptions (uint32_t renderOptions, unsigned int i=0)
 
unsigned int size ()
 
std::string textureName () const
 

Protected Member Functions

 SlSprite ()
 
 SlSprite (const std::string &name)
 
 SlSprite (SlTexture *texture)
 

Protected Attributes

std::string name_ = "unnamedSprite"
 
SDL_Rect sourceRect_ = {0,0,0,0}
 
SlTexture * texture_
 
std::vector< SlRenderSettings > destinations_
 

Detailed Description

Defines a part of a texture to be rendered with given settings.
A SlSprite is any object that will be rendered from a texture. It contains a pointer to a SlTexture and has to be created after the SlTexture.
The SlSprite has a source rectangle which defines which part of the texture is rendered and cannnot be changed. It can have several destination rectangles with different render options to render it multiple times on screen.

Constructor & Destructor Documentation

SlSprite::SlSprite ( const std::string &  name,
SlTexture *  texture,
int  x = 0,
int  y = 0,
int  width = 0,
int  height = 0 
)

This is the constructor that should be used for creating SlSprites.

All SlSprites must have a name and a texture.
If either width or height are 0, the texture's width and height will be used.
The default values will use the whole texture as a sprite. Calls addDefaultDestination(), so if you don't want to use that, either delete or adjust that destination.

assumes whole texture to be used

SlSprite::SlSprite ( const SlSprite &  )
delete

Delete copy constructor. There's no reason to copy the sprite, if the goal is to render different parts of it or with different options create another sprite.

SlSprite::SlSprite ( )
protected

Default constructor

SlSprite::SlSprite ( const std::string &  name)
protected

Creates sprite with name. Remember to add a texture!

SlSprite::SlSprite ( SlTexture *  texture)
protected

Creates sprite with with this texture.

Member Function Documentation

SlSprite * SlSprite::addDefaultDestination ( )

Adds an entry to destinations_ with default settings. Default destination is x = 0, y = 0, and destination dimensions equal to source dimensions;

SlSprite * SlSprite::addDestination ( int  x,
int  y,
uint32_t  renderOptions = SL_RENDER_DEFAULT 
)

Adds an entry to destinations_. The destination dimensions are set to the source dimensions, color is default.

SlSprite * SlSprite::centerAt ( unsigned int  x,
unsigned int  y,
unsigned int  destination = 0 
)

Centers the destination of this sprite at the specified coordinates.

SlSprite * SlSprite::centerInSprite ( const std::shared_ptr< SlSprite >  otherSprite,
unsigned int  destinationThis = 0,
unsigned int  destinationOther = 0 
)

Centers the destination of this sprite in the destinationRect of the other sprite.

void SlSprite::clearDestinations ( )

Removes all entries from destinations_. Sprite will not be rendered.

SDL_Rect SlSprite::destination ( unsigned int  i = 0)

Returns the origin and dimension of the texture in the window, i.e. the destinationRect for destinations_ at position i.

void SlSprite::destinationDimension ( int &  width,
int &  height,
unsigned int  i = 0 
)

Returns the dimension of the texture in the window, i.e. destinationRect width, height for destinations_ at position i.

void SlSprite::destinationOrigin ( int &  x,
int &  y,
unsigned int  i = 0 
)

Returns the origin of the texture in the window, i.e. destinationRect x,y coordinates for destinations_ at position i.

bool SlSprite::hasDestination ( )

Checks if any destinations are defined, i.e. if destinations_ has size > 0;

bool SlSprite::is_inside ( const int &  x,
const int &  y,
const unsigned int &  dest = 0 
)

Checks whether the given coordinates are inside the specified destination for this sprite.

void SlSprite::moveDestinationOriginBy ( int  x,
int  y,
unsigned int  i = 0 
)

Moves the sprite by the amounts given by x and y, i.e. x and y are deltas not absolutes.

std::string SlSprite::name ( ) const
inline

Allows reading the name, but not changing it.

SlSprite& SlSprite::operator= ( const SlSprite &  )
delete

Deleted, same reason as copy constructor.

void SlSprite::render ( SDL_Renderer *  renderer)

Renders all copies of the sprite given in destinations_.

void SlSprite::render ( SDL_Renderer *  renderer,
unsigned int  i 
)

Renders the copy of the sprite at position i in render settings.

Exceptions
std::runtime_errorif invalid destination or unable to render.
void SlSprite::setAngle ( double  angle,
unsigned int  i = 0 
)

Sets angle for position i of destinations_. The angle in degrees that indicates the rotation that will be applied when that sprite destination is rendered. Rotation will be around object centre.

void SlSprite::setColor ( uint8_t  red,
uint8_t  green,
uint8_t  blue,
uint8_t  alpha = 0xFF,
unsigned int  i = 0 
)

Sets color for position i of destinations_. Color is use when using color mod to render, and when creating a texture from a rectangle.

void SlSprite::setDestination ( const SDL_Rect &  dstRect,
unsigned int  i = 0 
)

Changes destinationRect for position i of destinations_.

Sets where and how large the texture will be rendered.

void SlSprite::setDestination ( int  x,
int  y,
int  w,
int  h,
unsigned int  i = 0 
)

Changes destinationRect for position i of destinations_.

Sets where and how large the texture will be rendered.

void SlSprite::setDestinationDimension ( int  width,
int  height,
unsigned int  i = 0 
)

Changes destinationRect for position i of destinations_.

Sets how large the texture will be rendered.

void SlSprite::setDestinationOrigin ( int  x,
int  y,
unsigned int  i = 0 
)

Changes destinationRect for position i of destinations_ . Sets where the sprite will be rendered.

void SlSprite::setRenderOptions ( uint32_t  renderOptions,
unsigned int  i = 0 
)

Sets SlRenderOptions for position i of destinations_.

Return values
falseif i > destinations_ size.
unsigned int SlSprite::size ( )
inline

Number of defined destinations. Used by the SlManager to check if requested destinations are valid.

std::string SlSprite::textureName ( ) const
inline

Access to the name of the underlying SlTexture.

Member Data Documentation

std::vector<SlRenderSettings> SlSprite::destinations_
protected

Settings for where and how to render the sprite. Multiple copies of the sprite can be rendered with different settings.

std::string SlSprite::name_ = "unnamedSprite"
protected

The name of a sprite cannot be changed after it is created

SDL_Rect SlSprite::sourceRect_ = {0,0,0,0}
protected

When a texture is loaded from an image file, sourceRect is set to the width and height of the texture. This is set when the sprite is created.

SlTexture* SlSprite::texture_
protected

Contains the actual SDL_Texture.


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