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.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/graphics/animation.cpp b/src/graphics/animation.cpp
new file mode 100644
index 0000000..f326c22
--- /dev/null
+++ b/src/graphics/animation.cpp
@@ -0,0 +1,122 @@
+#include "animation.h"
+
+#include <cstdio>
+
+namespace Graphics{
+
+ // Basic animation, loops through the pictures
+ Animation::Animation(){}
+
+ Animation::Animation(const char* filename, int count, int switchTime) :
+ i_ (0),
+ count_ (count),
+ 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);
+ }
+ 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);
+ }
+
+ bool Animation::ticked(int deltaTime){
+ lastTime_ += deltaTime;
+ if ( switchTime_ > lastTime_ ){
+ return false;
+ }
+ lastTime_ = 0;
+ return true;
+ }
+
+ sf::Sprite& Animation::next(int deltaTime){
+ if ( !ticked(deltaTime) ){
+ return sprite_;
+ }
+ if (i_ < count_ -1 ){
+ i_ += 1;
+ } else {
+ i_ = 0;
+ }
+ rect_.left = i_ * w_;
+
+ sprite_.setTexture(texture_); // This shouldn't be needed...!
+ sprite_.setTextureRect(rect_);
+
+ return sprite_;
+ }
+
+ Animation::~Animation(){}
+
+ // ONESHOT ANIMATION
+ OneShotAnimation::OneShotAnimation(){}
+
+ OneShotAnimation::OneShotAnimation(const char* filename, int count, int switchTime):
+ Animation(filename, count, switchTime) {
+ }
+
+ bool OneShotAnimation::finished(){
+ return i_ == count_ - 1;
+ }
+
+ sf::Sprite& OneShotAnimation::next(int deltaTime){
+ if ( !ticked(deltaTime) ){
+ return sprite_;
+ }
+ if ( finished() ){
+ return sprite_;
+ }
+ if (i_ < count_ -1 ){
+ i_ += 1;
+ }
+ rect_.left = i_ * w_;
+
+ sprite_.setTexture(texture_); // This shouldn't be needed...!
+ sprite_.setTextureRect(rect_);
+
+ return sprite_;
+ }
+
+ // BOUNCING ANIMATION
+ BouncingAnimation::BouncingAnimation(){}
+
+ BouncingAnimation::BouncingAnimation(const char* filename, int count, int switchTime):
+ Animation(filename, count, switchTime),
+ up_ (true) {
+ }
+
+ sf::Sprite& BouncingAnimation::next(int deltaTime){
+ if( !ticked(deltaTime) ) {
+ return sprite_;
+ }
+ // Rewrite this and eliminate the boolean (use the count as
+ // positive/negative)
+ if ( up_ ) {
+ if( i_ == count_ - 1){
+ up_ = false;
+ } else {
+ i_++;
+ }
+ } else {
+ i_--;
+ if( i_ == 0 ){
+ up_ = true;
+ }
+ }
+ rect_.left = i_ * w_;
+
+ sprite_.setTexture(texture_); // This shouldn't be needed...!
+ sprite_.setTextureRect(rect_);
+
+ return sprite_;
+ }
+
+ BouncingAnimation::~BouncingAnimation(){}
+}