AnimatedSprite class creation
This commit is contained in:
parent
d849c8b037
commit
adcd6146ed
3 changed files with 49 additions and 0 deletions
31
SimpleGame/src/Source/AnimatedSprite.cpp
Normal file
31
SimpleGame/src/Source/AnimatedSprite.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
|
||||
#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]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue