Made sprites resizable

This commit is contained in:
Maximilian Giller 2023-06-03 20:29:24 +02:00
parent f778e702e4
commit 7b56493fa9
11 changed files with 57 additions and 21 deletions

BIN
assets/64.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View file

@ -5,7 +5,7 @@
#include <map>
#include "game/input/input_device_group.h"
#define DEVELOPER_MODE false
#define DEVELOPER_MODE true
// FPS

View file

@ -6,12 +6,6 @@ GameObject::GameObject()
children = std::vector<std::shared_ptr<GameObject>>();
}
void GameObject::addChild(const std::shared_ptr<GameObject>& 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<GameObject> &child, IsometricCoordinates offset)
{
auto worldOffset = CoordinateTransformer::isometricToWorld(offset);
addChild(child, worldOffset);
}
void GameObject::addChild(const std::shared_ptr<GameObject> &child, WorldCoordinates offset)
{
children.push_back(child);
child->coordinates->setParent(coordinates, offset);
}

View file

@ -21,7 +21,8 @@ public:
void setActive(bool active);
bool getActive() const { return isActive; }
void addChild(const std::shared_ptr<GameObject> &child, WorldCoordinates offset = {0, 0});
void addChildScreenOffset(const std::shared_ptr<GameObject> &child, IsometricCoordinates offset = {0, 0});
void addChild(const std::shared_ptr<GameObject> &child, WorldCoordinates offset);
std::vector<std::shared_ptr<GameObject>> getChildren() const { return children; }
std::shared_ptr<TranslatedCoordinates> coordinates;

View file

@ -10,8 +10,9 @@ Player::Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &
input = std::move(assignedInput);
auto sprite = std::make_shared<SingleSprite>(TextureManager::getInstance()->getTexture(image));
addChild(sprite);
auto sprite = std::make_shared<SingleSprite>(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()

View file

@ -24,6 +24,7 @@ public:
private:
std::shared_ptr<InputIdentity> input;
float width = 10;
};

View file

@ -1,9 +1,10 @@
#include "animated_sprite.hpp"
#include "../config.h"
AnimatedSprite::AnimatedSprite(const std::vector<sf::Sprite> &sprites)
AnimatedSprite::AnimatedSprite(const std::vector<sf::Sprite> &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);
}
}

View file

@ -8,7 +8,7 @@
class AnimatedSprite : public GameObject
{
public:
AnimatedSprite(const std::vector<sf::Sprite>& sprites);
AnimatedSprite(const std::vector<sf::Sprite>& 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<sf::Sprite> sprites;
void setSize(const sf::Vector2f &size);
};

View file

@ -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<sf::Texture> &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<sf::Texture>& 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);
}

View file

@ -8,8 +8,8 @@
class SingleSprite : public GameObject
{
public:
SingleSprite(const sf::Sprite& sprite);
SingleSprite(const std::shared_ptr<sf::Texture>& texture);
SingleSprite(const sf::Sprite& sprite, const sf::Vector2f& size = sf::Vector2f(0, 0));
SingleSprite(const std::shared_ptr<sf::Texture>& 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);
};

View file

@ -7,7 +7,7 @@
#define PLAYER_TEXTURE "player"
std::map<std::string, std::string> const all_textures = {
{PLAYER_TEXTURE, "assets/player.png"}
{PLAYER_TEXTURE, "assets/64.png"}
};
#endif //HOLESOME_TEXTURE_CONFIG_H