Skip to content

Commit

Permalink
[examples] Add a spritesheet image reader example
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Oct 21, 2024
1 parent a151d2b commit 7e237ee
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions examples/Helpers/SpriteReader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once
#include <cmath>
#include <halp/audio.hpp>
#include <halp/controls.hpp>
#include <halp/file_port.hpp>
#include <halp/meta.hpp>
#include <halp/sample_accurate_controls.hpp>
#include <halp/texture.hpp>

#include <QImage>

/**
* 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);
}
};
}

0 comments on commit 7e237ee

Please sign in to comment.