AnimatedSprite class creation
This commit is contained in:
parent
d849c8b037
commit
adcd6146ed
3 changed files with 49 additions and 0 deletions
|
@ -25,6 +25,7 @@ add_executable(simpleGame
|
||||||
Source/NoteSprite.cpp Include/NoteSprite.hpp
|
Source/NoteSprite.cpp Include/NoteSprite.hpp
|
||||||
Source/NoteTile.cpp Include/NoteTile.hpp Include/NotePlaceEnum.hpp
|
Source/NoteTile.cpp Include/NoteTile.hpp Include/NotePlaceEnum.hpp
|
||||||
Source/TilePattern.cpp Include/TilePattern.hpp
|
Source/TilePattern.cpp Include/TilePattern.hpp
|
||||||
|
Source/AnimatedSprite.cpp Include/AnimatedSprite.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(simpleGame PRIVATE
|
target_include_directories(simpleGame PRIVATE
|
||||||
|
|
17
SimpleGame/src/Include/AnimatedSprite.hpp
Normal file
17
SimpleGame/src/Include/AnimatedSprite.hpp
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#pragma once
|
||||||
|
#include <SFML/Graphics/Sprite.hpp>
|
||||||
|
#include <SFML/Graphics/Texture.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class AnimatedSprite : public sf::Sprite {
|
||||||
|
private:
|
||||||
|
std::vector<sf::Texture> frames_textures;
|
||||||
|
float time_beetwen_frames = 0.5;
|
||||||
|
float time_without_change = 0.0;
|
||||||
|
int current_frame = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AnimatedSprite(const std::vector<std::string> &texture_paths);
|
||||||
|
void update(float dtime);
|
||||||
|
// draw will be same as sf::Sprite
|
||||||
|
};
|
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