RythmGame/SimpleGame/src/Source/AnimatedSprite.cpp

31 lines
966 B
C++

#include "AnimatedSprite.hpp"
#include "GameData.hpp"
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <cassert>
AnimatedSprite::AnimatedSprite(const std::vector<std::string> &texture_paths)
: sf::Sprite(PLACE_HOLDER_TEXTURE) {
frames_textures.reserve(texture_paths.size());
for (const auto &path : texture_paths) {
auto new_texture = sf::Texture();
if (!new_texture.loadFromFile(path)) {
printf("failed to load a texture of AnimatedSprite");
exit(0);
};
frames_textures.push_back(std::move(new_texture));
}
assert(frames_textures.size() > 0);
// setTexture is frome Sprite
setTexture(frames_textures[0]);
};
void AnimatedSprite::update(float dtime) {
time_without_change += dtime;
if (time_without_change > time_beetwen_frames) {
current_frame = (current_frame + 1) % frames_textures.size();
time_without_change = 0.0;
setTexture(frames_textures[current_frame]);
}
}