From a22342cd7ee96b0a6b6047e85c36e0f5d9338a45 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 22 Jul 2025 16:22:05 -0400 Subject: [PATCH] Now Sprite can do either aspect_ratio scaling or stretching and Icon is just a subclass. --- demos/clicker_game.cpp | 2 +- include/guecs/sfml/components.hpp | 11 ++------ src/guecs/sfml/components.cpp | 43 +++++++------------------------ 3 files changed, 13 insertions(+), 43 deletions(-) diff --git a/demos/clicker_game.cpp b/demos/clicker_game.cpp index bb6cda7..d932de6 100644 --- a/demos/clicker_game.cpp +++ b/demos/clicker_game.cpp @@ -101,7 +101,7 @@ struct ClickerUI { } $clicker = $gui.entity("clicker"); - $gui.set($clicker, {"clicker_the_dog"}); + $gui.set($clicker, {"clicker_the_dog", 0, true}); $gui.set($clicker, {"clicker_bark"}); $gui.set($clicker, { [&](auto, auto) { handle_button(Event::CLICKER); } diff --git a/include/guecs/sfml/components.hpp b/include/guecs/sfml/components.hpp index a323371..49ade35 100644 --- a/include/guecs/sfml/components.hpp +++ b/include/guecs/sfml/components.hpp @@ -40,6 +40,7 @@ namespace guecs { struct Sprite { string name; int padding = THEME.PADDING; + bool stretch = false; std::shared_ptr sprite = nullptr; void init(lel::Cell &cell); @@ -47,15 +48,7 @@ namespace guecs { void render(sf::RenderWindow& window, sf::Shader *shader_ptr); }; - struct Icon { - string name; - int padding = THEME.PADDING; - std::shared_ptr sprite = nullptr; - - void init(lel::Cell &cell); - void update(const string& new_name); - void render(sf::RenderWindow& window, sf::Shader *shader_ptr); - }; + struct Icon : public Sprite { }; struct Rectangle { int padding = THEME.PADDING; diff --git a/src/guecs/sfml/components.cpp b/src/guecs/sfml/components.cpp index 52c86cd..329a069 100644 --- a/src/guecs/sfml/components.cpp +++ b/src/guecs/sfml/components.cpp @@ -52,42 +52,19 @@ namespace guecs { sprite->setPosition({float(cell.x + padding), float(cell.y + padding)}); - sprite->setScale({ - 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) { - window.draw(*sprite, shader_ptr); - } - - void Icon::update(const string& new_name) { - if(new_name != name) { - name = new_name; - auto sprite_texture = BACKEND->get_icon(name); - sprite->setTexture(*sprite_texture.texture); - sprite->setTextureRect({{0,0},sprite_texture.frame_size}); + if(stretch) { + sprite->setScale({ + float(cell.w - padding * 2) / float(bounds.x), + float(cell.h - padding * 2) / float(bounds.y)}); + } else { + float box_width = float(cell.w - padding * 2); + float box_height = float(cell.h - padding * 2); + float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y)); + sprite->setScale({scale, scale}); } } - void Icon::init(lel::Cell &cell) { - auto sprite_texture = BACKEND->get_icon(name); - auto bounds = 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)}); - - 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 Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { window.draw(*sprite, shader_ptr); }