#pragma once #include #include #include #include namespace Animation { struct Animation { sf::Vector2u frameOrigin; // offset in pixels sf::Vector2u frameSize; // in pixels sf::Vector2u frameStep; // in pixels sf::Vector2u frameMargin; // in pixels unsigned frameCount; unsigned frameTime; // in milliseconds bool reverse; bool loop; bool bounce; sf::IntRect GetFrame(unsigned const frameNumber, bool const bounced = false) const; Animation(); }; std::unordered_map LoadFromFile(std::string const & filepath); class Sprite : public sf::Sprite { protected: std::unordered_map const & animations; Animation const * currentAnimation; unsigned currentFrame; unsigned currentFrameTime; bool bouncing; bool isFlippedHorizontally; bool isFlippedVertically; void SetFrame(); public: virtual bool IsAnimationFinished() const; // This flip call is not free, so be careful with lots // of repeated calls to this function virtual void SetHorizontalFlip(bool const v); // This flip call is not free, so be careful with lots // of repeated calls to this function virtual void SetVerticalFlip(bool const v); bool IsFlippedHorizontally() const; bool IsFlippedVertically() const; void ResetAnimation(); bool SetAnimation(const std::string & name); virtual void Update(sf::Time const & elapsed); void UpdateFromOther(Sprite const & other); Sprite(std::unordered_map const & _animations, std::string const & animation, sf::Texture const & texture); }; }