CUGL 2.3
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::SpriteSheet Class Reference

#include <CUSpriteSheet.h>

Public Member Functions

 SpriteSheet ()
 
 ~SpriteSheet ()
 
void dispose ()
 
bool init (const std::shared_ptr< Texture > &texture, int rows, int cols)
 
bool init (const std::shared_ptr< Texture > &texture, int rows, int cols, int size)
 
int getSize () const
 
unsigned int getFrame () const
 
void setFrame (int frame)
 
std::shared_ptr< TexturegetTexture () const
 
Size getFrameSize () const
 
Vec2 getOrigin () const
 
void setOrigin (Vec2 origin)
 
void draw (const std::shared_ptr< SpriteBatch > &batch, const Affine2 &transform) const
 
void draw (const std::shared_ptr< SpriteBatch > &batch, Color4 color, const Affine2 &transform) const
 
void draw (const std::shared_ptr< SpriteBatch > &batch, Vec2 origin, const Affine2 &transform) const
 
void draw (const std::shared_ptr< SpriteBatch > &batch, Color4 color, Vec2 origin, const Affine2 &transform) const
 

Static Public Member Functions

static std::shared_ptr< SpriteSheetalloc (const std::shared_ptr< Texture > &texture, int rows, int cols)
 
static std::shared_ptr< SpriteSheetalloc (const std::shared_ptr< Texture > &texture, int rows, int cols, int size)
 

Protected Attributes

std::shared_ptr< cugl::Texture_texture
 
int _cols
 
int _size
 
int _frame
 
Vec2 _origin
 
Rect _bounds
 
Poly2 _region
 

Detailed Description

This class breaks a sprite sheet into frames for animation.

This class is an alternative for scene2::SpriteNode for developers that do not want to use scene graphs in their implementation. Like that class, it allows the user to manage the current frame of the sprite sheet. All drawing details are handled by the class, greatly simplifying the animation.

Note that this is a stateful class with a mutable attribute (setFrame). That means this class is not an asset, and should not be loaded as such. The underlying texture is an asset. Multiple objects could all share the same sprite sheet texture. But as all of these objects may be at different animation frames, they each need their own sprite sheet.

You cannot change the texture or size of a sprite sheet. If you need to change the animation source, you should make a new sprite sheet object.

Constructor & Destructor Documentation

◆ SpriteSheet()

cugl::SpriteSheet::SpriteSheet ( )

Creates a degenerate sprite sheet with no frames.

You must initialize the sheet before using it.

◆ ~SpriteSheet()

cugl::SpriteSheet::~SpriteSheet ( )
inline

Deletes the sprite sheet, disposing all resources

Member Function Documentation

◆ alloc() [1/2]

static std::shared_ptr< SpriteSheet > cugl::SpriteSheet::alloc ( const std::shared_ptr< Texture > &  texture,
int  rows,
int  cols 
)
inlinestatic

Returns a newly allocated sprite sheet from the given texture.

This constructor assumes that the prite sheet is rectangular, and that there are no unused frames.

Parameters
textureThe texture image to use
rowsThe number of rows in the sprite sheet
colsThe number of columns in the sprite sheet
Returns
a newly allocated sprite sheete from the given texture.

◆ alloc() [2/2]

static std::shared_ptr< SpriteSheet > cugl::SpriteSheet::alloc ( const std::shared_ptr< Texture > &  texture,
int  rows,
int  cols,
int  size 
)
inlinestatic

Returns a newly allocated filmstrip node from the given texture.

The parameter size is to indicate that there are unused frames in the filmstrip. The value size must be less than or equal to rows*cols, or this constructor will raise an error.

The size of the node is equal to the size of a single frame in the filmstrip. To resize the node, scale it up or down. Do NOT change the polygon, as that will interfere with the animation.

Parameters
textureThe texture image to use
rowsThe number of rows in the filmstrip
colsThe number of columns in the filmstrip
sizeThe number of frames in the filmstrip
Returns
a newly allocated filmstrip node from the given texture.

◆ dispose()

void cugl::SpriteSheet::dispose ( )

Deletes the sprite sheet and resets all attributes.

You must reinitialize the sprite batch to use it.

◆ draw() [1/4]

void cugl::SpriteSheet::draw ( const std::shared_ptr< SpriteBatch > &  batch,
Color4  color,
const Affine2 transform 
) const

Draws this sprite sheet to the given sprite batch.

Only the active frame will be drawn. The transform will be applied to the active frame at the default origin. The sprite will be tinted by the given color.

Parameters
batchThe sprite batch for the drawing
colorThe tint color
transformThe drawing transform

◆ draw() [2/4]

void cugl::SpriteSheet::draw ( const std::shared_ptr< SpriteBatch > &  batch,
Color4  color,
Vec2  origin,
const Affine2 transform 
) const

Draws this sprite sheet to the given sprite batch.

Only the active frame will be drawn. The transform will be applied to the active frame at the specified origin. The sprite will be tinted by the given color.

Parameters
batchThe sprite batch for the drawing
colorThe tint color
originThe transform origin offset
transformThe drawing transform

◆ draw() [3/4]

void cugl::SpriteSheet::draw ( const std::shared_ptr< SpriteBatch > &  batch,
const Affine2 transform 
) const

Draws this sprite sheet to the given sprite batch.

Only the active frame will be drawn. The transform will be applied to the active frame at the default origin. The sprite will not be tinted.

Parameters
batchThe sprite batch for the drawing
transformThe drawing transform

◆ draw() [4/4]

void cugl::SpriteSheet::draw ( const std::shared_ptr< SpriteBatch > &  batch,
Vec2  origin,
const Affine2 transform 
) const

Draws this sprite sheet to the given sprite batch.

Only the active frame will be drawn. The transform will be applied to the active frame at the specified origin. The sprite will not be tinted.

Parameters
batchThe sprite batch for the drawing
originThe transform origin offset
transformThe drawing transform

◆ getFrame()

unsigned int cugl::SpriteSheet::getFrame ( ) const
inline

Returns the current active frame.

Returns
the current active frame.

◆ getFrameSize()

Size cugl::SpriteSheet::getFrameSize ( ) const
inline

Returns the size of a single animation frame.

Returns
the size of a single animation frame.

◆ getOrigin()

Vec2 cugl::SpriteSheet::getOrigin ( ) const
inline

Returns the default origin of this sprite sheet

The origin is the offset (in pixels) from the bottom left corner of the current frame. The origin is used when drawing the sprite sheet; the transform applies rotations and scale operations relative to this origin. By default this value is (0,0).

Returns
the default origin of this sprite sheet

◆ getSize()

int cugl::SpriteSheet::getSize ( ) const
inline

Returns the number of frames in this sprite sheet.

Returns
the number of frames in this sprite sheet.

◆ getTexture()

std::shared_ptr< Texture > cugl::SpriteSheet::getTexture ( ) const
inline

Returns the texture associated with this sprite sheet.

Returns
the texture associated with this sprite sheet.

◆ init() [1/2]

bool cugl::SpriteSheet::init ( const std::shared_ptr< Texture > &  texture,
int  rows,
int  cols 
)
inline

Initializes the sprite sheet with the given texture.

This initializer assumes that the sprite sheet is rectangular, and that there are no unused frames.

Parameters
textureThe texture image to use
rowsThe number of rows in the sprite sheet
colsThe number of columns in the sprite sheet
Returns
true if the sprite sheet is initialized properly, false otherwise.

◆ init() [2/2]

bool cugl::SpriteSheet::init ( const std::shared_ptr< Texture > &  texture,
int  rows,
int  cols,
int  size 
)

Initializes the sprite sheet with the given texture.

The parameter size is to indicate that there are unused frames in the filmstrip. The value size must be less than or equal to rows*cols, or this constructor will raise an error.

Parameters
textureThe texture image to use
rowsThe number of rows in the sprite sheet
colsThe number of columns in the sprite sheet
sizeThe number of frames in the sprite sheet
Returns
true if the sprite sheet is initialized properly, false otherwise.

◆ setFrame()

void cugl::SpriteSheet::setFrame ( int  frame)

Sets the active frame as the given index.

If the frame index is invalid, an error is raised.

Parameters
framethe index to make the active frame

◆ setOrigin()

void cugl::SpriteSheet::setOrigin ( Vec2  origin)
inline

Sets the default origin of this sprite sheet

The origin is the offset (in pixels) from the bottom left corner of the current frame. The origin is used when drawing the sprite sheet; the transform applies rotations and scale operations relative to this origin. By default this value is (0,0).

Parameters
originThe default origin of this sprite sheet

Member Data Documentation

◆ _bounds

Rect cugl::SpriteSheet::_bounds
protected

The bounds of the current frame in the sprite sheet

◆ _cols

int cugl::SpriteSheet::_cols
protected

The number of columns in this sprite sheet

◆ _frame

int cugl::SpriteSheet::_frame
protected

The active animation frame

◆ _origin

Vec2 cugl::SpriteSheet::_origin
protected

The default transform origin of this sprite sheet

◆ _region

Poly2 cugl::SpriteSheet::_region
protected

The display region for animation

◆ _size

int cugl::SpriteSheet::_size
protected

The number of frames in this sprite sheet

◆ _texture

std::shared_ptr<cugl::Texture> cugl::SpriteSheet::_texture
protected

The underlying sprite sheet texture


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