Guest User

AnimatedSprites.cpp

a guest
Dec 20th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. /*
  2.  * animatedsprites.cpp
  3.  *
  4.  *  Created on: Dec 13, 2017
  5.  *      Author: shinobu
  6.  */
  7. //TODO: For debugging only, remove when finished
  8. #include <iostream>
  9. //~~~~~~~~~~~~~~~~~
  10.  
  11. #include "AnimatedSprites.h"
  12. #include "Graphics.h"
  13. #include "Sprite.h"
  14.  
  15. AnimatedSprite::AnimatedSprite() {
  16.     //TODO
  17. }
  18.  
  19. AnimatedSprite::AnimatedSprite(Graphics &_graphics, const std::string &_filePath, int _sourceX, int _sourceY,
  20.         int _width, int _height, float _posX, float _posY, float _updateTime) :
  21.                 Sprite(_graphics, _filePath, _sourceX, _sourceY, _width, _height, _posX, _posY),
  22.                 frameIndex(0),
  23.                 updateTime(_updateTime),
  24.                 visible(true),
  25.                 currentAnimationOnce(false),
  26.                 currentAnimation("")
  27. {}
  28.  
  29. void AnimatedSprite::addAnimation(int frames, int startX, int startY, std::string name, int width, int height, Vector2 offset) {
  30.     std::vector<SDL_Rect> rectangles;
  31.  
  32.     //Grab all of the source rectangles on the sprite sheet for an animation
  33.     for (int i = 0; i < frames; i++) {
  34.         SDL_Rect newRectangle = {(i + startX) * width, startY, width, height};
  35.         rectangles.push_back(newRectangle);
  36.     }
  37.  
  38.     this->animations.insert(std::pair<std::string, std::vector<SDL_Rect>> (name, rectangles));
  39.     this->offsets.insert(std::pair<std::string, Vector2> (name, offset));
  40. }
  41.  
  42. void AnimatedSprite::resetAnimations() {
  43.     this->animations.clear();
  44.     this->offsets.clear();
  45. }
  46.  
  47. /*DEBUGGING NOTICE: If the game is terminating at this function with no real indication,
  48. *                   check to make sure your animations are spelled correctly or are using
  49. *                   the correct name.   */
  50. void AnimatedSprite::animate(std::string animation, bool once) {
  51.     this->currentAnimationOnce = once;
  52.     if (this->currentAnimation != animation) {
  53.         this->currentAnimation = animation;
  54.         this->frameIndex = 0;
  55.     }
  56. }
  57.  
  58. void AnimatedSprite::setVisible(bool _visible) {
  59.     this->visible = _visible;
  60. }
  61.  
  62. void AnimatedSprite::stopAnimation() {
  63.     this->frameIndex = 0;
  64.     this->animationDone(this->currentAnimation);
  65. }
  66.  
  67. void AnimatedSprite::update(int elapsedTime) {
  68.     Sprite::update();
  69.  
  70.     std::cout << "Elapsed time: " << elapsedTime << std::endl;
  71.     std::cout << "Time to update: " << this->updateTime << std::endl;
  72.     std::cout << "Time Elapsed: " << this->timeElapsed << std::endl;
  73.     // any other variables that could help figure out what the problem is
  74.  
  75.     this->timeElapsed += elapsedTime;
  76.  
  77.     if (this->timeElapsed > this->updateTime) {
  78.         this->timeElapsed -= this->updateTime;
  79.         if (this->frameIndex < (this->animations[this->currentAnimation].size() - 1)) {
  80.             this->frameIndex += 1;
  81.         }
  82.         else {
  83.             if (this->currentAnimationOnce) {
  84.                 this->setVisible(false);
  85.                 this->animationDone(this->currentAnimation);
  86.             }
  87.             this->frameIndex = 0;
  88.             //this->animationDone(this->currentAnimation);
  89.         }
  90.     }
  91. }
  92.  
  93. void AnimatedSprite::draw(Graphics &graphics, int x, int y) {
  94.     if (this->visible) {
  95.         SDL_Rect destinationRectangle;
  96.  
  97.         destinationRectangle.x = x + this->offsets[this->currentAnimation].x;
  98.         destinationRectangle.y = y + this->offsets[this->currentAnimation].y;
  99.         destinationRectangle.w = this->sourceRect.w * globals::SPRITE_SCALE;
  100.         destinationRectangle.h = this->sourceRect.h * globals::SPRITE_SCALE;
  101.  
  102.         SDL_Rect sourceRectangle = this->animations[this->currentAnimation][this->frameIndex];
  103.  
  104.         graphics.blitSurface(this->spriteSheet, &sourceRectangle, &destinationRectangle);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment