A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Animation.cc 834B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "Animation.h"
  2. namespace Swan {
  3. void Animation::init(int w, int h, double interval, const Asset &asset, int flags) {
  4. width_ = w;
  5. height_ = h;
  6. interval_ = interval;
  7. asset_ = &asset;
  8. fcount_ = asset_->image_.getSize().y / height_;
  9. sprite_.setTexture(asset_->tex_);
  10. sprite_.setTextureRect(sf::IntRect(0, 0, width_, height_));
  11. if (flags & (int)Flags::HFLIP) {
  12. sprite_.setOrigin(Vec2(width_, 0));
  13. sprite_.setScale(Vec2(-1, 1));
  14. }
  15. }
  16. void Animation::tick(double dt) {
  17. timer_.tick(dt);
  18. if (timer_.periodic(interval_)) {
  19. dirty_ = true;
  20. frame_ += 1;
  21. if (frame_ >= fcount_)
  22. frame_ = 0;
  23. sprite_.setTextureRect(sf::IntRect(0, height_ * frame_, width_, height_));
  24. }
  25. }
  26. void Animation::draw(Win &win) {
  27. win.draw(sprite_);
  28. }
  29. void Animation::reset() {
  30. timer_.reset();
  31. frame_ = 0;
  32. dirty_ = true;
  33. }
  34. }