diff options
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/animation.cpp | 73 | ||||
-rw-r--r-- | src/graphics/animation.h | 18 |
2 files changed, 51 insertions, 40 deletions
diff --git a/src/graphics/animation.cpp b/src/graphics/animation.cpp index f326c22..b93387e 100644 --- a/src/graphics/animation.cpp +++ b/src/graphics/animation.cpp @@ -7,24 +7,32 @@ namespace Graphics{ // Basic animation, loops through the pictures Animation::Animation(){} - Animation::Animation(const char* filename, int count, int switchTime) : + 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){ - texture_.loadFromFile(filename); - sf::Vector2u size = texture_.getSize(); - h_ = size.y; - if (size.x % count != 0){ - printf("Animation: size not divisible by %d\n", count); + 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; } - w_ = size.x / count_; - rect_ = sf::IntRect(0,0,w_,h_); // set to first picture in - // animation - sprite_ = sf::Sprite(texture_, rect_); - sprite_.setScale(8,8); + + rect_ = sf::IntRect(0,row_ * h_,w_,h_); // set to first picture in + // animation } bool Animation::ticked(int deltaTime){ @@ -36,9 +44,9 @@ namespace Graphics{ return true; } - sf::Sprite& Animation::next(int deltaTime){ + sf::IntRect& Animation::next(int deltaTime){ if ( !ticked(deltaTime) ){ - return sprite_; + return rect_; } if (i_ < count_ -1 ){ i_ += 1; @@ -47,10 +55,13 @@ namespace Graphics{ } rect_.left = i_ * w_; - sprite_.setTexture(texture_); // This shouldn't be needed...! - sprite_.setTextureRect(rect_); + return rect_; + } - return sprite_; + void Animation::reset(){ + i_ = 0; + rect_.left = i_ * w_; + rect_.top = row_ * h_; } Animation::~Animation(){} @@ -58,43 +69,42 @@ namespace Graphics{ // ONESHOT ANIMATION OneShotAnimation::OneShotAnimation(){} - OneShotAnimation::OneShotAnimation(const char* filename, int count, int switchTime): - Animation(filename, count, switchTime) { + OneShotAnimation::OneShotAnimation(const sf::Texture& texture, int count, + int switchTime, int height = 0, int width = 0, int row = 0): + Animation(texture, count, switchTime, height, width, row) { } bool OneShotAnimation::finished(){ return i_ == count_ - 1; } - sf::Sprite& OneShotAnimation::next(int deltaTime){ + sf::IntRect& OneShotAnimation::next(int deltaTime){ if ( !ticked(deltaTime) ){ - return sprite_; + return rect_; } if ( finished() ){ - return sprite_; + return rect_; } if (i_ < count_ -1 ){ i_ += 1; } rect_.left = i_ * w_; - sprite_.setTexture(texture_); // This shouldn't be needed...! - sprite_.setTextureRect(rect_); - - return sprite_; + return rect_; } // BOUNCING ANIMATION BouncingAnimation::BouncingAnimation(){} - BouncingAnimation::BouncingAnimation(const char* filename, int count, int switchTime): - Animation(filename, count, switchTime), + BouncingAnimation::BouncingAnimation(const sf::Texture& texture, int count, + int switchTime, int height = 0, int width = 0, int row = 0): + Animation(texture, count, switchTime, height, width, row), up_ (true) { } - sf::Sprite& BouncingAnimation::next(int deltaTime){ + sf::IntRect& BouncingAnimation::next(int deltaTime){ if( !ticked(deltaTime) ) { - return sprite_; + return rect_; } // Rewrite this and eliminate the boolean (use the count as // positive/negative) @@ -112,10 +122,7 @@ namespace Graphics{ } rect_.left = i_ * w_; - sprite_.setTexture(texture_); // This shouldn't be needed...! - sprite_.setTextureRect(rect_); - - return sprite_; + return rect_; } BouncingAnimation::~BouncingAnimation(){} diff --git a/src/graphics/animation.h b/src/graphics/animation.h index 87b4427..9f5dfa5 100644 --- a/src/graphics/animation.h +++ b/src/graphics/animation.h @@ -9,26 +9,29 @@ namespace Graphics{ protected: int i_; int count_; + int row_; int lastTime_; int switchTime_; unsigned int w_, h_; sf::IntRect rect_; sf::Texture texture_; - sf::Sprite sprite_; bool ticked(int deltaTime); public: Animation(); ~Animation(); - Animation(const char* filename, int count, int switchTime); - sf::Sprite& next(int deltaTime); + Animation(const sf::Texture& texture, int count, int switchTime, + int height, int width, int row); + sf::IntRect& next(int deltaTime); + void reset(); }; class OneShotAnimation : Animation { public: OneShotAnimation(); - OneShotAnimation(const char* filename, int count, int switchTime); + OneShotAnimation(const sf::Texture& texture, int count, int + switchTime, int height, int width, int row); bool finished(); - sf::Sprite& next(int deltaTime); + sf::IntRect& next(int deltaTime); }; class BouncingAnimation : Animation{ @@ -36,9 +39,10 @@ namespace Graphics{ bool up_; public: BouncingAnimation(); - BouncingAnimation(const char* filename, int count, int switchTime); + BouncingAnimation(const sf::Texture& texture, int count, int + switchTime, int height, int width, int row); ~BouncingAnimation(); - sf::Sprite& next(int deltaTime); + sf::IntRect& next(int deltaTime); }; } #endif // GRAPHICS_ANIMATION_H |