Icons now scale via aspect ratio, but it's still not as robust as it should be. I also think that Sprite should just work this way or stretch if an option is given, which would make Icon kind of pointless.

main
Zed A. Shaw 10 months ago
parent e128de3244
commit b7cfa4db2d
  1. 27
      src/guecs/sfml/components.cpp

@ -45,19 +45,16 @@ namespace guecs {
void Sprite::init(lel::Cell &cell) { void Sprite::init(lel::Cell &cell) {
auto sprite_texture = BACKEND->get_sprite(name); 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<sf::Sprite>(*sprite_texture.texture, rect); sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
sprite->setPosition({ sprite->setPosition({float(cell.x + padding), float(cell.y + padding)});
float(cell.x + padding),
float(cell.y + padding)});
auto bounds = sprite->getLocalBounds();
sprite->setScale({ sprite->setScale({
float(cell.w - padding * 2) / bounds.size.x, float(cell.w - padding * 2) / float(bounds.x),
float(cell.h - padding * 2) / bounds.size.y}); float(cell.h - padding * 2) / float(bounds.y)});
} }
void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
@ -75,21 +72,25 @@ namespace guecs {
void Icon::init(lel::Cell &cell) { void Icon::init(lel::Cell &cell) {
auto sprite_texture = BACKEND->get_icon(name); auto sprite_texture = BACKEND->get_icon(name);
auto bounds = sprite_texture.frame_size;
sf::IntRect rect{{0,0},sprite_texture.frame_size}; sf::IntRect rect{{0,0}, bounds};
fmt::println("ICON SIZE: {},{}; {},{}",
rect.position.x, rect.position.y,
rect.size.x, rect.size.y);
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect); sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
sprite->setPosition({ float(cell.x + padding), float(cell.y + padding)}); 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) { void Icon::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
window.draw(*sprite, shader_ptr); window.draw(*sprite, shader_ptr);
} }
void Rectangle::init(lel::Cell& cell) { void Rectangle::init(lel::Cell& cell) {
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2}; sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2};
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size); if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);