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.cpp73
1 files changed, 40 insertions, 33 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(){}