Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * animatedsprites.cpp
- *
- * Created on: Dec 13, 2017
- * Author: shinobu
- */
- //TODO: For debugging only, remove when finished
- #include <iostream>
- //~~~~~~~~~~~~~~~~~
- #include "AnimatedSprites.h"
- #include "Graphics.h"
- #include "Sprite.h"
- AnimatedSprite::AnimatedSprite() {
- //TODO
- }
- AnimatedSprite::AnimatedSprite(Graphics &_graphics, const std::string &_filePath, int _sourceX, int _sourceY,
- int _width, int _height, float _posX, float _posY, float _updateTime) :
- Sprite(_graphics, _filePath, _sourceX, _sourceY, _width, _height, _posX, _posY),
- frameIndex(0),
- updateTime(_updateTime),
- visible(true),
- currentAnimationOnce(false),
- currentAnimation("")
- {}
- void AnimatedSprite::addAnimation(int frames, int startX, int startY, std::string name, int width, int height, Vector2 offset) {
- std::vector<SDL_Rect> rectangles;
- //Grab all of the source rectangles on the sprite sheet for an animation
- for (int i = 0; i < frames; i++) {
- SDL_Rect newRectangle = {(i + startX) * width, startY, width, height};
- rectangles.push_back(newRectangle);
- }
- this->animations.insert(std::pair<std::string, std::vector<SDL_Rect>> (name, rectangles));
- this->offsets.insert(std::pair<std::string, Vector2> (name, offset));
- }
- void AnimatedSprite::resetAnimations() {
- this->animations.clear();
- this->offsets.clear();
- }
- /*DEBUGGING NOTICE: If the game is terminating at this function with no real indication,
- * check to make sure your animations are spelled correctly or are using
- * the correct name. */
- void AnimatedSprite::animate(std::string animation, bool once) {
- this->currentAnimationOnce = once;
- if (this->currentAnimation != animation) {
- this->currentAnimation = animation;
- this->frameIndex = 0;
- }
- }
- void AnimatedSprite::setVisible(bool _visible) {
- this->visible = _visible;
- }
- void AnimatedSprite::stopAnimation() {
- this->frameIndex = 0;
- this->animationDone(this->currentAnimation);
- }
- void AnimatedSprite::update(int elapsedTime) {
- Sprite::update();
- std::cout << "Elapsed time: " << elapsedTime << std::endl;
- std::cout << "Time to update: " << this->updateTime << std::endl;
- std::cout << "Time Elapsed: " << this->timeElapsed << std::endl;
- // any other variables that could help figure out what the problem is
- this->timeElapsed += elapsedTime;
- if (this->timeElapsed > this->updateTime) {
- this->timeElapsed -= this->updateTime;
- if (this->frameIndex < (this->animations[this->currentAnimation].size() - 1)) {
- this->frameIndex += 1;
- }
- else {
- if (this->currentAnimationOnce) {
- this->setVisible(false);
- this->animationDone(this->currentAnimation);
- }
- this->frameIndex = 0;
- //this->animationDone(this->currentAnimation);
- }
- }
- }
- void AnimatedSprite::draw(Graphics &graphics, int x, int y) {
- if (this->visible) {
- SDL_Rect destinationRectangle;
- destinationRectangle.x = x + this->offsets[this->currentAnimation].x;
- destinationRectangle.y = y + this->offsets[this->currentAnimation].y;
- destinationRectangle.w = this->sourceRect.w * globals::SPRITE_SCALE;
- destinationRectangle.h = this->sourceRect.h * globals::SPRITE_SCALE;
- SDL_Rect sourceRectangle = this->animations[this->currentAnimation][this->frameIndex];
- graphics.blitSurface(this->spriteSheet, &sourceRectangle, &destinationRectangle);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment