diff --git a/src/guecs/sfml/components.cpp b/src/guecs/sfml/components.cpp index 064cc3a..52c86cd 100644 --- a/src/guecs/sfml/components.cpp +++ b/src/guecs/sfml/components.cpp @@ -45,19 +45,16 @@ namespace guecs { void Sprite::init(lel::Cell &cell) { auto sprite_texture = BACKEND->get_sprite(name); + auto bounds = sprite_texture.frame_size; - sf::IntRect rect{{0,0},sprite_texture.frame_size}; + sf::IntRect rect{{0,0}, bounds}; sprite = make_shared(*sprite_texture.texture, rect); - sprite->setPosition({ - float(cell.x + padding), - float(cell.y + padding)}); - - auto bounds = sprite->getLocalBounds(); + sprite->setPosition({float(cell.x + padding), float(cell.y + padding)}); sprite->setScale({ - float(cell.w - padding * 2) / bounds.size.x, - float(cell.h - padding * 2) / bounds.size.y}); + float(cell.w - padding * 2) / float(bounds.x), + float(cell.h - padding * 2) / float(bounds.y)}); } void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { @@ -75,21 +72,25 @@ namespace guecs { void Icon::init(lel::Cell &cell) { auto sprite_texture = BACKEND->get_icon(name); + auto bounds = sprite_texture.frame_size; - sf::IntRect rect{{0,0},sprite_texture.frame_size}; - fmt::println("ICON SIZE: {},{}; {},{}", - rect.position.x, rect.position.y, - rect.size.x, rect.size.y); + sf::IntRect rect{{0,0}, bounds}; sprite = make_shared(*sprite_texture.texture, rect); sprite->setPosition({ float(cell.x + padding), float(cell.y + padding)}); + + float a_ratio = float(bounds.x) / float(bounds.y); + float x_scale = float(cell.w - padding * 2) / float(bounds.x); + float y_new_size = (float(bounds.x) * x_scale) / a_ratio; + float y_scale = float(cell.h - padding * 2) / y_new_size; + + sprite->setScale({x_scale, y_scale}); } void Icon::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { window.draw(*sprite, shader_ptr); } - void Rectangle::init(lel::Cell& cell) { sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2}; if(shape == nullptr) shape = make_shared(size);