From 7e237ee0a8bc49ae509760716b8475f595925dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Mon, 21 Oct 2024 14:42:04 -0400 Subject: [PATCH] [examples] Add a spritesheet image reader example --- examples/Helpers/SpriteReader.hpp | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 examples/Helpers/SpriteReader.hpp diff --git a/examples/Helpers/SpriteReader.hpp b/examples/Helpers/SpriteReader.hpp new file mode 100644 index 0000000..277a7c2 --- /dev/null +++ b/examples/Helpers/SpriteReader.hpp @@ -0,0 +1,76 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +#include + +/** + * An example of how to make an object that reads a sprite from a sprite sheet. + * Uses Qt's QImage for image processing but any pull request using a simpler image + * reader such as stb_image will be very welcome! + */ +namespace examples::helpers +{ +struct SpriteReader +{ + halp_meta(name, "Spirte reader"); + halp_meta(c_name, "sprite_reader"); + halp_meta(category, "GFX"); + halp_meta(author, "Jean-Michaƫl Celerier"); + halp_meta(description, "Loads an image file into a texture"); + halp_meta(uuid, "b28b0cf5-164b-4aa0-8edd-8830ade702e5"); + + struct + { + struct : halp::file_port<"Image", halp::mmap_file_view> + { + void update(SpriteReader& r) { r.on_image_changed(); } + } img; + + halp::xy_spinboxes_i32<"Sprite size", halp::range{1, 1024, 32}> sz; + halp::hslider_f32<"Image count"> percentage; + } inputs; + + struct + { + halp::texture_output<"Out"> tex; + } outputs; + + QImage image_data; + QImage sprite; + + void on_image_changed() + { + image_data = QImage(QString::fromUtf8(this->inputs.img.file.filename)) + .convertedTo(QImage::Format_RGBA8888); + } + + void operator()() + { + if(image_data.isNull()) + return; + + const int sprite_w = inputs.sz.value.x; + const int sprite_h = inputs.sz.value.y; + const QSize img_size = image_data.size(); + const int columns = std::floor(img_size.width() / float(sprite_w)); + const int rows = std::floor(img_size.height() / float(sprite_h)); + if(columns <= 0 || rows <= 0) + return; + + const int N = columns * rows; + const int cur = std::floor(inputs.percentage * N); + const int cur_row = cur / columns; + const int cur_col = cur % columns; + const QRect rect(cur_col * sprite_w, cur_row * sprite_h, sprite_w, sprite_h); + sprite = image_data.copy(rect); + sprite.mirror(); + outputs.tex.texture.update(sprite.bits(), inputs.sz.value.x, inputs.sz.value.y); + } +}; +}