CUGL 2.3
Cornell University Game Library
|
#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< Texture > | getTexture () 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< SpriteSheet > | alloc (const std::shared_ptr< Texture > &texture, int rows, int cols) |
static std::shared_ptr< SpriteSheet > | alloc (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 |
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.
cugl::SpriteSheet::SpriteSheet | ( | ) |
Creates a degenerate sprite sheet with no frames.
You must initialize the sheet before using it.
|
inline |
Deletes the sprite sheet, disposing all resources
|
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.
texture | The texture image to use |
rows | The number of rows in the sprite sheet |
cols | The number of columns in the sprite sheet |
|
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.
texture | The texture image to use |
rows | The number of rows in the filmstrip |
cols | The number of columns in the filmstrip |
size | The number of frames in the filmstrip |
void cugl::SpriteSheet::dispose | ( | ) |
Deletes the sprite sheet and resets all attributes.
You must reinitialize the sprite batch to use it.
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.
batch | The sprite batch for the drawing |
color | The tint color |
transform | The drawing transform |
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.
batch | The sprite batch for the drawing |
color | The tint color |
origin | The transform origin offset |
transform | The drawing transform |
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.
batch | The sprite batch for the drawing |
transform | The drawing transform |
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.
batch | The sprite batch for the drawing |
origin | The transform origin offset |
transform | The drawing transform |
|
inline |
Returns the current active frame.
|
inline |
Returns the size of a single animation frame.
|
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).
|
inline |
Returns the number of frames in this sprite sheet.
|
inline |
Returns the texture associated with this sprite sheet.
|
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.
texture | The texture image to use |
rows | The number of rows in the sprite sheet |
cols | The number of columns in the sprite sheet |
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.
texture | The texture image to use |
rows | The number of rows in the sprite sheet |
cols | The number of columns in the sprite sheet |
size | The number of frames in the sprite sheet |
void cugl::SpriteSheet::setFrame | ( | int | frame | ) |
Sets the active frame as the given index.
If the frame index is invalid, an error is raised.
frame | the index to make the active frame |
|
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).
origin | The default origin of this sprite sheet |
|
protected |
The bounds of the current frame in the sprite sheet
|
protected |
The number of columns in this sprite sheet
|
protected |
The active animation frame
|
protected |
The default transform origin of this sprite sheet
|
protected |
The display region for animation
|
protected |
The number of frames in this sprite sheet
|
protected |
The underlying sprite sheet texture