summaryrefslogtreecommitdiff
path: root/src/graphics/animation.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics/animation.cpp')
-rw-r--r--src/graphics/animation.cpp145
1 files changed, 48 insertions, 97 deletions
diff --git a/src/graphics/animation.cpp b/src/graphics/animation.cpp
index f311cf0..828772c 100644
--- a/src/graphics/animation.cpp
+++ b/src/graphics/animation.cpp
@@ -1,118 +1,69 @@
#include "animation.h"
-#include <cstdio>
+#include <string>
namespace Graphics{
- // Basic animation, loops through the pictures
Animation::Animation(){}
- Animation::Animation(const sf::Texture& texture, int count, int switchTime,
- int height = 0, int width = 0, int row = 0) :
- i_ (0),
- count_ (count),
- row_ (row),
- lastTime_ (0),
- switchTime_ (switchTime){
-
- sf::Vector2u size = texture_.getSize();
- if ( height == 0 ){
- h_ = size.y;
- } else {
- h_ = height;
- }
-
- if ( width == 0 ){
- if (size.x % count != 0){
- printf("Animation: size not divisible by %d\n", count);
- }
- w_ = size.x / count_;
- } else {
- w_ = width;
+ Animation::Animation(const sf::Texture& texture, std::size_t count,
+ sf::Time duration, bool repeat)
+ : numFrames_ (count)
+ , currentFrame_(0)
+ , duration_(duration)
+ , elapsedTime_(sf::Time::Zero)
+ , repeat_ (repeat)
+ {
+ sprite_.setTexture(texture);
+ sf::Vector2u size = texture.getSize();
+
+ if (size.x % numFrames_ != 0){
+ throw ("Animation: size not divisible by " + std::to_string(count));
}
+ frameSize_.x = static_cast<std::size_t>(size.x / count);
+ frameSize_.y = size.y;
- rect_ = sf::IntRect(0,row_ * h_,w_,h_); // set to first picture in
- // animation
+ sprite_.setTextureRect( sf::IntRect(0,0,frameSize_.x,frameSize_.y) );
}
- bool Animation::ticked(int deltaTime){
- lastTime_ += deltaTime;
- if ( switchTime_ > lastTime_ ){
- return false;
- }
- lastTime_ = 0;
- return true;
- }
+ void Animation::update(sf::Time deltaTime){
+ sf::Time timePerFrame = duration_ / static_cast<float>(numFrames_);
+ elapsedTime_ += deltaTime;
- sf::IntRect& Animation::next(int deltaTime){
- if ( !ticked(deltaTime) ){
- return rect_;
- }
- if (i_ < count_ -1 ){
- i_ += 1;
- } else {
- i_ = 0;
- }
- rect_.left = i_ * w_;
-
- return rect_;
- }
-
- void Animation::reset(){
- i_ = 0;
- rect_.left = i_ * w_;
- rect_.top = row_ * h_;
- }
-
- bool Animation::finished(){
- return false;
- }
- Animation::~Animation(){}
+ while (elapsedTime_ > timePerFrame ){
- // ONESHOT ANIMATION
- bool OneShotAnimation::finished(){
- return (i_ == count_ - 1);
- }
-
- sf::IntRect& OneShotAnimation::next(int deltaTime){
- if ( !ticked(deltaTime) ){
- return rect_;
- }
- if ( finished() ){
- return rect_;
- }
- if (i_ < count_ -1 ){
- i_ += 1;
- }
- rect_.left = i_ * w_;
-
- return rect_;
- }
-
- // BOUNCING ANIMATION
- sf::IntRect& BouncingAnimation::next(int deltaTime){
- if( !ticked(deltaTime) ) {
- return rect_;
- }
- // Rewrite this and eliminate the boolean (use the count as
- // positive/negative)
- if ( up_ ) {
- if( i_ == count_ - 1){
- up_ = false;
+ if(currentFrame_ == numFrames_ - 1){
+ if (repeat_) {
+ currentFrame_ = 0;
+ } else {
+ return;
+ }
} else {
- i_++;
- }
- } else {
- i_--;
- if( i_ == 0 ){
- up_ = true;
+ currentFrame_ ++;
}
+ elapsedTime_ -= timePerFrame;
+ sprite_.setTextureRect(
+ sf::IntRect(
+ currentFrame_ * frameSize_.x,
+ 0,
+ frameSize_.x,
+ frameSize_.y)
+ );
}
- rect_.left = i_ * w_;
+ }
- return rect_;
+ void Animation::draw(sf::RenderTarget& target, sf::RenderStates states)
+ const{
+ target.draw(sprite_, states);
+ }
+ void Animation::reset(){
+ currentFrame_ = 0;
+ elapsedTime_ = sf::Time::Zero;
+ sprite_.setTextureRect( sf::IntRect(0,0,frameSize_.x,frameSize_.y) );
}
- BouncingAnimation::~BouncingAnimation(){}
+ bool Animation::finished() const{
+ return (!repeat_ && currentFrame_ == numFrames_ - 1) ;
+ }
}