From 7b56493fa995a724d37933a6ccd6a37961eb3ee8 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Sat, 3 Jun 2023 20:29:24 +0200 Subject: [PATCH] Made sprites resizable --- assets/64.png | Bin 0 -> 2108 bytes src/config.h | 2 +- src/game/game_object.cpp | 18 ++++++++++++------ src/game/game_object.h | 3 ++- src/game/player/player.cpp | 8 ++++---- src/game/player/player.hpp | 1 + src/sprites/animated_sprite.cpp | 11 ++++++++++- src/sprites/animated_sprite.hpp | 4 +++- src/sprites/single_sprite.cpp | 23 +++++++++++++++++++---- src/sprites/single_sprite.hpp | 6 ++++-- src/texture_config.h | 2 +- 11 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 assets/64.png diff --git a/assets/64.png b/assets/64.png new file mode 100644 index 0000000000000000000000000000000000000000..9be8440db28a3f1d0e7183c275797c8adda0f3fd GIT binary patch literal 2108 zcmV-C2*dY@P)EX>4Tx04R}tkv&MmKpe$iQ?;TMK|6>zWT?7W5EXIMDionYs1;guFuC*#nlvOS zE{=k0!NHHks)LKOt`4q(Aou~|>f)s6A|?JWDYS_7;J6>}?mh0_0YbING^=eK&~)2O z#G+y4y_(bAaW|%hd2J!T! zZE)Tv4zawf5}y-~nRG$oN3JU#zj4mFEbz>bnM}+Rhls^o6Dv*3@}@>SMI2T&o$`g0 z$13M7&RVg|>i6U?3}*C|Wvu?$|#@+8xdM{QY<8BKia}S==vpcDdZ}E zkz)ZRXpmh$_#gc4*2+#!cuB!1(Ej2$AHzUk7pPSn=lj@kYA1mI8Mx9L{!$f~`6Rv4 z(85PR?>2C8-O!Xh;Bp5Tcrs*Db|pVeA)5u>&*+=dK>sb!wdVC!-^b|#kfg5SH^9Lm zFp{I}^%n2$Z0_yfGxh#{05R=yjb1mb&;S4c24YJ`L;(K){{a7>y{D4^000SaNLh0L z04^f{04^f|c%?sf00007bV*G`2j>O@5)K|APP``o000?uMObu0Z*6U5Zgc=ca%Ew3 zWn>_CX>@2HM@dakSAh-}000IPNklQ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O93(jd?OKK~1uED;%fCqSwI0000 #include "game/input/input_device_group.h" -#define DEVELOPER_MODE false +#define DEVELOPER_MODE true // FPS diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index 368c2c1..9165db7 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -6,12 +6,6 @@ GameObject::GameObject() children = std::vector>(); } -void GameObject::addChild(const std::shared_ptr& child, WorldCoordinates offset) -{ - children.push_back(child); - child->coordinates->setParent(coordinates, offset); -} - void GameObject::setActive(bool active) { // Careful: potential infinite loop! @@ -46,3 +40,15 @@ void GameObject::lateUpdate() child->lateUpdate(); } } + +void GameObject::addChildScreenOffset(const std::shared_ptr &child, IsometricCoordinates offset) +{ + auto worldOffset = CoordinateTransformer::isometricToWorld(offset); + addChild(child, worldOffset); +} + +void GameObject::addChild(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 93bb126..a476f1b 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -21,7 +21,8 @@ public: void setActive(bool active); bool getActive() const { return isActive; } - void addChild(const std::shared_ptr &child, WorldCoordinates offset = {0, 0}); + void addChildScreenOffset(const std::shared_ptr &child, IsometricCoordinates offset = {0, 0}); + void addChild(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 277f53e..671dd0b 100644 --- a/src/game/player/player.cpp +++ b/src/game/player/player.cpp @@ -10,8 +10,9 @@ Player::Player(std::shared_ptr assignedInput, const std::string & input = std::move(assignedInput); - auto sprite = std::make_shared(TextureManager::getInstance()->getTexture(image)); - addChild(sprite); + auto sprite = std::make_shared(TextureManager::getInstance()->getTexture(image), + sf::Vector2f{width, width}); + addChildScreenOffset(sprite, {-width / 2.f, -width / 2.f}); } sf::Vector2f Player::getTrackablePosition() const @@ -21,8 +22,7 @@ sf::Vector2f Player::getTrackablePosition() const sf::Vector2f Player::getTrackableSize() const { - // TODO: Proper implementation - return {50, 50}; + return {width, width}; } void Player::update() diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index bedb5d0..231d0fb 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -24,6 +24,7 @@ public: private: std::shared_ptr input; + float width = 10; }; diff --git a/src/sprites/animated_sprite.cpp b/src/sprites/animated_sprite.cpp index 86adc36..b1404ef 100644 --- a/src/sprites/animated_sprite.cpp +++ b/src/sprites/animated_sprite.cpp @@ -1,9 +1,10 @@ #include "animated_sprite.hpp" #include "../config.h" -AnimatedSprite::AnimatedSprite(const std::vector &sprites) +AnimatedSprite::AnimatedSprite(const std::vector &sprites, const sf::Vector2f& size) { this->sprites = sprites; + setSize(size); } void AnimatedSprite::update() @@ -27,3 +28,11 @@ void AnimatedSprite::draw(sf::RenderWindow *window) const window->draw(currentSprite); } + +void AnimatedSprite::setSize(const sf::Vector2f &size) +{ + for (auto &sprite: sprites) + { + sprite.setScale(size.x / sprite.getTextureRect().width, size.y / sprite.getTextureRect().height); + } +} diff --git a/src/sprites/animated_sprite.hpp b/src/sprites/animated_sprite.hpp index b2dc6c1..f314f4d 100644 --- a/src/sprites/animated_sprite.hpp +++ b/src/sprites/animated_sprite.hpp @@ -8,7 +8,7 @@ class AnimatedSprite : public GameObject { public: - AnimatedSprite(const std::vector& sprites); + AnimatedSprite(const std::vector& sprites, const sf::Vector2f& size = sf::Vector2f(0, 0)); void update() override; @@ -19,6 +19,8 @@ private: int currentFrame = 0; sf::Time timeSinceLastFrame = sf::Time::Zero; std::vector sprites; + + void setSize(const sf::Vector2f &size); }; diff --git a/src/sprites/single_sprite.cpp b/src/sprites/single_sprite.cpp index f7c6f01..fcb2d52 100644 --- a/src/sprites/single_sprite.cpp +++ b/src/sprites/single_sprite.cpp @@ -1,8 +1,18 @@ #include "single_sprite.hpp" -SingleSprite::SingleSprite(const sf::Sprite &sprite) +SingleSprite::SingleSprite(const sf::Sprite &sprite, const sf::Vector2f &size) { this->sprite = sprite; + + setSize(size); +} + +SingleSprite::SingleSprite(const std::shared_ptr &texture, const sf::Vector2f &size) +{ + sprite = sf::Sprite(); + sprite.setTexture(*texture); + + setSize(size); } void SingleSprite::draw(sf::RenderWindow *window) const @@ -15,8 +25,13 @@ void SingleSprite::lateUpdate() sprite.setPosition(coordinates->isometric().toScreen()); } -SingleSprite::SingleSprite(const std::shared_ptr& texture) +void SingleSprite::setSize(const sf::Vector2f &size) { - sprite = sf::Sprite(); - sprite.setTexture(*texture); + if (size == sf::Vector2f(0, 0)) + { + return; + } + + auto textureSize = sprite.getTextureRect(); + sprite.setScale(size.x / textureSize.width, size.y / textureSize.height); } diff --git a/src/sprites/single_sprite.hpp b/src/sprites/single_sprite.hpp index 462f604..f1e93db 100644 --- a/src/sprites/single_sprite.hpp +++ b/src/sprites/single_sprite.hpp @@ -8,8 +8,8 @@ class SingleSprite : public GameObject { public: - SingleSprite(const sf::Sprite& sprite); - SingleSprite(const std::shared_ptr& texture); + SingleSprite(const sf::Sprite& sprite, const sf::Vector2f& size = sf::Vector2f(0, 0)); + SingleSprite(const std::shared_ptr& texture, const sf::Vector2f& size = sf::Vector2f(0, 0)); void draw(sf::RenderWindow *window) const override; @@ -17,6 +17,8 @@ public: private: sf::Sprite sprite; + + void setSize(const sf::Vector2f &size); }; diff --git a/src/texture_config.h b/src/texture_config.h index 596369f..7538628 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/player.png"} + {PLAYER_TEXTURE, "assets/64.png"} }; #endif //HOLESOME_TEXTURE_CONFIG_H