Calamity Engine 1.0.0
A cross-platform 2D game engine written in C++ and SDL3.
Loading...
Searching...
No Matches
AnimatedSprite Class Reference

#include <components.hpp>

Inheritance diagram for AnimatedSprite:
Component

Public Member Functions

 AnimatedSprite ()=default
 
 AnimatedSprite (std::shared_ptr< Window > window)
 
void initialize () override
 
void render (std::shared_ptr< Window >) override
 
void update (float deltaTime) override
 
void addAnimation (const Animation &animation)
 
void removeAnimation (const std::string &name)
 
void play (const std::string &animation)
 
void stop ()
 
void pause ()
 
const TexturegetCurrentTexture () const
 
Vector2 getCurrentSize () const
 
bool isPlaying () const
 
FramegetCurrentFrame () const
 
std::string getCurrentAnimationName () const
 
AnimationgetCurrentAnimation () const
 
template<class Archive >
void save (Archive &ar) const
 
template<class Archive >
void load (Archive &ar)
 
void postLoad ()
 
- Public Member Functions inherited from Component
virtual ~Component ()=default
 
virtual void physicsUpdate ()
 
virtual void input (InputEvent &event)
 
virtual void exit ()
 
NodegetNode () const
 
void setNode (Node *n)
 
template<class Archive >
void save (Archive &ar) const
 
template<class Archive >
void load (Archive &ar)
 

Public Attributes

Signal< std::string > changed
 
Signal< std::string > finished
 
Signal< std::string > looped
 
Signal< std::string > stopped
 
Signal< std::string > paused
 
bool flipH = false
 
bool flipV = false
 
std::map< std::string, Animationanimations
 
bool visible = true
 
bool screenSpace = false
 

Detailed Description

Animated Sprite

The AnimatedSprite class allows you to define Animations composed of Frames which will get rendered on screen.

Example usage:

std::shared_ptr<Node> node = std::make_shared<Node>("myNode");
std::shared_ptr<AnimatedSprite> sprite = std::make_shared<AnimatedSprite>(window);
// arguments: animation name, FPS, size, loop and autoplay
Animation anim = Animation("test", 2, Vector2{15, 15}, true, false);
anim.texturePath = "res://assets/frames.png";
// append several frames to the sprite
anim.addFrames(
// This adds a frame, with a sourceRect, a centered origin and the modulate color of blue.
Frame({{0, 0}, {32, 32}}, {0.5, 0.5}, Color::BLUE),
Frame({{32, 0}, {32, 32}}),
Frame({{64, 0}, {32, 32}}),
Frame({{64, 0}, {32, 32}}),
Frame({{64, 0}, {32, 32}}),
Frame({{64, 0}, {32, 32}}),
Frame({{96, 0}, {32, 32}}),
Frame({{128, 0}, {32, 32}})
);
sprite->addAnimation(anim);
node->addComponent(sprite);
sprite->play("test"); // The animation will now play and loop... forever!
Definition definitions.hpp:372
std::string texturePath
Definition definitions.hpp:380
TextureScaling textureScaling
Definition definitions.hpp:381
void addFrames(Args... frames)
Definition definitions.hpp:388
static const Color BLUE
Definition definitions.hpp:62
Definition definitions.hpp:324
Definition definitions.hpp:77

Properties and usages

You can start, stop and pause animations:

sprite->play("test");
sprite->pause(); // the animation is now paused.
sprite->play("test"); // you can resume it by firing play with the same animation name (in this case, "test")
sprite->stop();

AnimatedSprites also have a bunch of signals that you can register to callbacks. For example, you can register a callback when the animation is finished!

sprite->finished.connect([]() {
Logger::info("Animation finished playing!");
});
static void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.hpp:28

And, just like regular Sprites, AnimatedSprites have the flipV and flipH attributes. This is useful when animating the sprite!

sprite->flipV = true; // The sprite will now render vertically flipped!

You can also make AnimatedSprites render using screen space positioning. This can be used for UI elements and things like that:

sprite->screenSpace = true; // Now, the position of the sprite will directly translate to screen coordinates!

Make sure to also check out the animated sprite example!

Constructor & Destructor Documentation

◆ AnimatedSprite() [1/2]

AnimatedSprite::AnimatedSprite ( )
default

◆ AnimatedSprite() [2/2]

AnimatedSprite::AnimatedSprite ( std::shared_ptr< Window window)
inline

Member Function Documentation

◆ addAnimation()

void AnimatedSprite::addAnimation ( const Animation animation)

◆ getCurrentAnimation()

Animation * AnimatedSprite::getCurrentAnimation ( ) const

◆ getCurrentAnimationName()

std::string AnimatedSprite::getCurrentAnimationName ( ) const

◆ getCurrentFrame()

Frame * AnimatedSprite::getCurrentFrame ( ) const

◆ getCurrentSize()

Vector2 AnimatedSprite::getCurrentSize ( ) const

◆ getCurrentTexture()

const Texture * AnimatedSprite::getCurrentTexture ( ) const

◆ initialize()

void AnimatedSprite::initialize ( )
overridevirtual

Reimplemented from Component.

◆ isPlaying()

bool AnimatedSprite::isPlaying ( ) const

◆ load()

template<class Archive >
void AnimatedSprite::load ( Archive &  ar)
inline

◆ pause()

void AnimatedSprite::pause ( )

◆ play()

void AnimatedSprite::play ( const std::string &  animation)

◆ postLoad()

void AnimatedSprite::postLoad ( )
virtual

Reimplemented from Component.

◆ removeAnimation()

void AnimatedSprite::removeAnimation ( const std::string &  name)

◆ render()

void AnimatedSprite::render ( std::shared_ptr< Window window)
overridevirtual

Reimplemented from Component.

◆ save()

template<class Archive >
void AnimatedSprite::save ( Archive &  ar) const
inline

◆ stop()

void AnimatedSprite::stop ( )

◆ update()

void AnimatedSprite::update ( float  deltaTime)
overridevirtual

Reimplemented from Component.

Member Data Documentation

◆ animations

std::map<std::string, Animation> AnimatedSprite::animations

◆ changed

Signal<std::string> AnimatedSprite::changed

◆ finished

Signal<std::string> AnimatedSprite::finished

◆ flipH

bool AnimatedSprite::flipH = false

◆ flipV

bool AnimatedSprite::flipV = false

◆ looped

Signal<std::string> AnimatedSprite::looped

◆ paused

Signal<std::string> AnimatedSprite::paused

◆ screenSpace

bool AnimatedSprite::screenSpace = false

◆ stopped

Signal<std::string> AnimatedSprite::stopped

◆ visible

bool AnimatedSprite::visible = true

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