Skip to content

Instantly share code, notes, and snippets.

@bjumbeck
Last active March 19, 2024 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjumbeck/5d695bfb62313fab33d5 to your computer and use it in GitHub Desktop.
Save bjumbeck/5d695bfb62313fab33d5 to your computer and use it in GitHub Desktop.
#include "Animation.h"
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Texture.hpp>
Animation::Animation() :
sprite(),
frameSize(),
numFrames(0),
currentFrame(0),
duration(sf::Time::Zero),
elapsedTime(sf::Time::Zero),
repeat(false)
{
}
Animation::Animation(const sf::Texture& texture) :
sprite(texture),
frameSize(),
numFrames(0),
currentFrame(0),
duration(sf::Time::Zero),
elapsedTime(sf::Time::Zero),
repeat(false)
{
}
void Animation::setTexture(const sf::Texture& texture)
{
sprite.setTexture(texture);
}
const sf::Texture* Animation::getTexture() const
{
return sprite.getTexture();
}
void Animation::setFrameSize(sf::Vector2i frameSize)
{
this->frameSize = frameSize;
}
sf::Vector2i Animation::getFrameSize() const
{
return frameSize;
}
void Animation::setNumFrames(std::size_t numFrames)
{
this->numFrames = numFrames;
}
std::size_t Animation::getNumFrames() const
{
return numFrames;
}
void Animation::setDuration(sf::Time duration)
{
this->duration = duration;
}
sf::Time Animation::getDuration() const
{
return duration;
}
void Animation::setRepeating(bool flag)
{
repeat = flag;
}
bool Animation::isRepeating() const
{
return repeat;
}
void Animation::restart()
{
currentFrame = 0;
}
bool Animation::isFinished() const
{
return currentFrame >= numFrames;
}
sf::FloatRect Animation::getLocalBounds() const
{
return sf::FloatRect(getOrigin(), static_cast<sf::Vector2f>(getFrameSize()));
}
sf::FloatRect Animation::getGlobalBounds() const
{
return getTransform().transformRect(getLocalBounds());
}
void Animation::update(sf::Time dt)
{
sf::Time timePerFrame = duration / static_cast<float>(numFrames);
elapsedTime += dt;
sf::Vector2i textureBounds(sprite.getTexture()->getSize());
sf::IntRect textureRect = sprite.getTextureRect();
if (currentFrame == 0)
textureRect = sf::IntRect(0, 0, frameSize.x, frameSize.y);
while (elapsedTime >= timePerFrame && (currentFrame <= numFrames || repeat))
{
textureRect.left += textureRect.width;
if (textureRect.left + textureRect.width > textureBounds.x)
{
textureRect.left = 0;
textureRect.top += textureRect.height;
}
elapsedTime -= timePerFrame;
if (repeat)
{
currentFrame = (currentFrame + 1) % numFrames;
if (currentFrame == 0)
textureRect = sf::IntRect(0, 0, frameSize.x, frameSize.y);
}
else
{
++currentFrame;
}
}
sprite.setTextureRect(textureRect);
}
void Animation::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(sprite, states);
}
#pragma once
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Time.hpp>
class Animation : public sf::Drawable, public sf::Transformable
{
public:
Animation();
explicit Animation(const sf::Texture& texture);
void setTexture(const sf::Texture& texture);
const sf::Texture* getTexture() const;
void setFrameSize(sf::Vector2i mFrameSize);
sf::Vector2i getFrameSize() const;
void setNumFrames(std::size_t numFrames);
std::size_t getNumFrames() const;
void setDuration(sf::Time duration);
sf::Time getDuration() const;
void setRepeating(bool flag);
bool isRepeating() const;
void restart();
bool isFinished() const;
sf::FloatRect getLocalBounds() const;
sf::FloatRect getGlobalBounds() const;
void update(sf::Time dt);
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
private:
sf::Sprite sprite;
sf::Vector2i frameSize;
std::size_t numFrames;
std::size_t currentFrame;
sf::Time duration;
sf::Time elapsedTime;
bool repeat;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment