From 37ef4020a141f70e1bb73d3d1f2d1d584a1d50da Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Sun, 4 Jun 2023 20:55:14 +0200 Subject: [PATCH] Working example for animated sprites --- assets/numbers.png | Bin 0 -> 6789 bytes src/debug/grid_debug_layer.cpp | 2 +- src/game/game_object.cpp | 4 ++-- src/game/game_object.h | 2 +- src/game/player/player.cpp | 15 ++++++++++++--- src/game/player/player.hpp | 2 +- src/sprites/animated_sprite.cpp | 1 + src/sprites/animated_sprite.hpp | 4 +++- src/sprites/sprite_sheet.cpp | 2 +- src/texture_config.h | 2 +- 10 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 assets/numbers.png diff --git a/assets/numbers.png b/assets/numbers.png new file mode 100644 index 0000000000000000000000000000000000000000..224b9782d742175b6aa72d1b9564b0af54c86acb GIT binary patch literal 6789 zcmW-mcQjl78^>=VNQ~Nhg&3iB)!uv6-c@3js!gpTR%@$LrD$oJ)+}mtpn{sE_Fkc= zSz3Gl?)Udk&T~%AdFS5T=RVK-_5LK880k=wv62A*K&hvzX$AlgumrGh67X}m{uB5G ztG=G5nnkF?_8sC$1}!G#?D)hYBqdLg6i=+KOzd#wZeoz0K7v+AP|yrI%uPAWtt$i9 z)kTX4atr?5OTW69Yz*<;nxATJ{2`aI@as)T$Wq}{h`Z6Px-L!QO9V25=!$*!j=(vy zh+Sx9ki7EZ;lg@gSKp-qknfT4N;lLIXz7QGLyfes2z)83Yug>hi^Ri_5KYlo8b2YZ zAz;KI=OcqcLLmGA0vSM)iZ!RfK>aql{To>z2)H;z7syP(zzUK9I;^=)`bIvJFzchO3Wes-;5$;7)+ZdJ(@qrvmf?3=q6+6je^G|-^~(rG9BwTw ztdG2Zzc%#7V2zcD#uLV=7^)h5dGh}FZ2Qv!+$C!1JL2?Lh6*}xwJ%%JO<(`HXd(02 zghRxw^NvIs)Np@})z!1>)@#c>SxY;krRJ}%?*1$&{?(OXULfoL-QGAyn8mF(TkpL1 z%5&r$&V%u*K)w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UWf+M$_b05HX*`5>b(al zc|f1`r0R*?T}S%3T!{S>C+neMB5<5)pqux}&VeUf5Q)F~F~8ZL_Q{&o(c`RKFC-4& zNH1h4=`PpPOGSu8ST)bIZQY0P=i7)xVe^fkfCru5N2DtYl8!|RIG(0P?OjtPNv8dQ zEu{+iUCKvm#1q~~Ovl8-I+ho6j*Ho|R@SZ7czi8>&Em+oTKRQrk4|rwrM1A`ICAG0MQ!gA)V}JX@ zJ|b&~`4yiQBNIt4|JigbDny?hZkMF09%IhkV0K@FN_EATcp=5yoSKny3a40aB`XbA zSTd8?%fT>0m~0y|+x|L=Iy*5@M2&=7AeoW-!NzEk-}MJ?6GDKe+N!qz$z-jU$Dm{E zLHGWXe1a7w$$?lQUpel8TtW6=%waGSU*Ksu&ZP{6vGE~V+E*C#gb9_T zBA;7CB8$q3)Vj)&y5*IMM`>U9+R?~PxG({8@INGQEi9z(<*`XY!3F91aPf(b5e$c% zB3EDAMm9`ACLt8I-q>-b2ac{GYO4!iK0p_+j0)kr#s&QzNw9;1gYh-6a5#eRID0E2^7$-Dha3$cxr;DeA69? zfl>U&7Sys)X*7OBAO!d@YyBZYVW>f=IQ(C)R4f}ccmUIm4&Y*j#DP-GuyjWk*en)* b1*GrXu&hQ>8-RyPfS#6-<_C35ECBou>}6TN literal 0 HcmV?d00001 diff --git a/src/debug/grid_debug_layer.cpp b/src/debug/grid_debug_layer.cpp index 1722188..0c3ef8a 100644 --- a/src/debug/grid_debug_layer.cpp +++ b/src/debug/grid_debug_layer.cpp @@ -23,7 +23,7 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY) } auto gameObject = std::make_shared(DB_ISOPLANE_CORNER_RADIUS, color); - addChild(gameObject, WorldCoordinates(x, y)); + addChildWorldOffset(gameObject, WorldCoordinates(x, y)); } } } diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index 9165db7..ee7989a 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -44,10 +44,10 @@ void GameObject::lateUpdate() void GameObject::addChildScreenOffset(const std::shared_ptr &child, IsometricCoordinates offset) { auto worldOffset = CoordinateTransformer::isometricToWorld(offset); - addChild(child, worldOffset); + addChildWorldOffset(child, worldOffset); } -void GameObject::addChild(const std::shared_ptr &child, WorldCoordinates offset) +void GameObject::addChildWorldOffset(const std::shared_ptr &child, WorldCoordinates offset) { children.push_back(child); child->coordinates->setParent(coordinates, offset); diff --git a/src/game/game_object.h b/src/game/game_object.h index a476f1b..3bc490d 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -22,7 +22,7 @@ public: bool getActive() const { return isActive; } void addChildScreenOffset(const std::shared_ptr &child, IsometricCoordinates offset = {0, 0}); - void addChild(const std::shared_ptr &child, WorldCoordinates offset); + void addChildWorldOffset(const std::shared_ptr &child, WorldCoordinates offset); std::vector> getChildren() const { return children; } std::shared_ptr coordinates; diff --git a/src/game/player/player.cpp b/src/game/player/player.cpp index 671dd0b..7e63c0f 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -1,6 +1,8 @@ #include "player.hpp" #include "../../sprites/texture_manager.hpp" #include "../../sprites/single_sprite.hpp" +#include "../../sprites/animated_sprite.hpp" +#include "../../sprites/sprite_sheet.hpp" #include @@ -10,9 +12,14 @@ Player::Player(std::shared_ptr assignedInput, const std::string & input = std::move(assignedInput); - auto sprite = std::make_shared(TextureManager::getInstance()->getTexture(image), - sf::Vector2f{width, width}); - addChildScreenOffset(sprite, {-width / 2.f, -width / 2.f}); +// auto sprite = std::make_shared(TextureManager::getInstance()->getTexture(image), +// sf::Vector2f{width, width}); +// addChildScreenOffset(sprite, {-width / 2.f, -width / 2.f}); + + auto spriteSheet = std::make_shared(TextureManager::getInstance()->getTexture(image), 4, 2); + auto animatedSprite = spriteSheet->getAnimation(0, 8); + animatedSprite->setSize({width, width}); + addChildScreenOffset(animatedSprite, {-width / 2.f, -width / 2.f}); } sf::Vector2f Player::getTrackablePosition() const @@ -36,6 +43,8 @@ void Player::update() auto moveDirection = input->direction.asIsometricVector(); auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds(); coordinates->move(moveDelta); + + GameObject::update(); } TrackableState Player::getTrackableState() const diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index 231d0fb..b3fd4f3 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -24,7 +24,7 @@ public: private: std::shared_ptr input; - float width = 10; + float width = 50; }; diff --git a/src/sprites/animated_sprite.cpp b/src/sprites/animated_sprite.cpp index b1404ef..997b391 100644 --- a/src/sprites/animated_sprite.cpp +++ b/src/sprites/animated_sprite.cpp @@ -1,5 +1,6 @@ #include "animated_sprite.hpp" #include "../config.h" +#include "../logging/easylogging++.h" AnimatedSprite::AnimatedSprite(const std::vector &sprites, const sf::Vector2f& size) { diff --git a/src/sprites/animated_sprite.hpp b/src/sprites/animated_sprite.hpp index f314f4d..ea7a345 100644 --- a/src/sprites/animated_sprite.hpp +++ b/src/sprites/animated_sprite.hpp @@ -15,12 +15,14 @@ public: void draw(sf::RenderWindow *window) const override; sf::Time frameDuration = sf::seconds(0.1f); + + void setSize(const sf::Vector2f &size); + private: int currentFrame = 0; sf::Time timeSinceLastFrame = sf::Time::Zero; std::vector sprites; - void setSize(const sf::Vector2f &size); }; diff --git a/src/sprites/sprite_sheet.cpp b/src/sprites/sprite_sheet.cpp index cd80142..ddc77f6 100644 --- a/src/sprites/sprite_sheet.cpp +++ b/src/sprites/sprite_sheet.cpp @@ -29,7 +29,7 @@ sf::Sprite SpriteSheet::getSprite(int sequenceIndex) const std::shared_ptr SpriteSheet::getAnimation(int startingSequenceIndex, int numberOfFrames) const { - if (startingSequenceIndex < 0 || startingSequenceIndex + numberOfFrames >= sprites.size()) + if (startingSequenceIndex < 0 || startingSequenceIndex + numberOfFrames > sprites.size()) { throw std::runtime_error("Invalid starting sequence index"); } diff --git a/src/texture_config.h b/src/texture_config.h index 7538628..d63144c 100644 --- a/src/texture_config.h +++ b/src/texture_config.h @@ -7,7 +7,7 @@ #define PLAYER_TEXTURE "player" std::map const all_textures = { - {PLAYER_TEXTURE, "assets/64.png"} + {PLAYER_TEXTURE, "assets/numbers.png"} }; #endif //HOLESOME_TEXTURE_CONFIG_H