From adcd6146edc87055765197d29d82d66b28e59e46 Mon Sep 17 00:00:00 2001 From: Crizomb Date: Sun, 15 Jun 2025 17:28:53 +0200 Subject: [PATCH] AnimatedSprite class creation --- SimpleGame/src/CMakeLists.txt | 1 + SimpleGame/src/Include/AnimatedSprite.hpp | 17 +++++++++++++ SimpleGame/src/Source/AnimatedSprite.cpp | 31 +++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 SimpleGame/src/Include/AnimatedSprite.hpp create mode 100644 SimpleGame/src/Source/AnimatedSprite.cpp diff --git a/SimpleGame/src/CMakeLists.txt b/SimpleGame/src/CMakeLists.txt index a6f1fe0..1433ce2 100644 --- a/SimpleGame/src/CMakeLists.txt +++ b/SimpleGame/src/CMakeLists.txt @@ -25,6 +25,7 @@ add_executable(simpleGame Source/NoteSprite.cpp Include/NoteSprite.hpp Source/NoteTile.cpp Include/NoteTile.hpp Include/NotePlaceEnum.hpp Source/TilePattern.cpp Include/TilePattern.hpp + Source/AnimatedSprite.cpp Include/AnimatedSprite.hpp ) target_include_directories(simpleGame PRIVATE diff --git a/SimpleGame/src/Include/AnimatedSprite.hpp b/SimpleGame/src/Include/AnimatedSprite.hpp new file mode 100644 index 0000000..44c6832 --- /dev/null +++ b/SimpleGame/src/Include/AnimatedSprite.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +#include +#include + +class AnimatedSprite : public sf::Sprite { +private: + std::vector frames_textures; + float time_beetwen_frames = 0.5; + float time_without_change = 0.0; + int current_frame = 0; + +public: + AnimatedSprite(const std::vector &texture_paths); + void update(float dtime); + // draw will be same as sf::Sprite +}; diff --git a/SimpleGame/src/Source/AnimatedSprite.cpp b/SimpleGame/src/Source/AnimatedSprite.cpp new file mode 100644 index 0000000..c13fdda --- /dev/null +++ b/SimpleGame/src/Source/AnimatedSprite.cpp @@ -0,0 +1,31 @@ + +#include "AnimatedSprite.hpp" +#include "GameData.hpp" +#include +#include +#include + +AnimatedSprite::AnimatedSprite(const std::vector &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]); + } +}