Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
sprite_strip.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <algorithm>
6#include <cstddef>
7#include <cstdint>
8#include <string>
9#include <vector>
10
11namespace pulp::view {
12
21public:
27 enum class Source { raw_pixels, image_file };
28
29 SpriteStrip() = default;
30
32 void load(const uint8_t* data, size_t data_size,
35 data_.assign(data, data + data_size);
36 // Hashed once here so every paint can draw through a content-keyed,
37 // cached decode instead of decoding the whole strip per frame.
38 data_key_ = data_.empty() ? std::string{} : runtime::sha256_hex(data_.data(), data_.size());
39 decoded_size_known_ = false;
40 decoded_width_ = decoded_height_ = 0;
41 path_.clear();
42 source_ = Source::raw_pixels;
43 total_width_ = total_width;
44 total_height_ = total_height;
45 frame_count_ = frame_count;
46 orientation_ = orientation;
47 compute_frame_dimensions();
48 }
49
53 void load_from_file(std::string file_path,
55 int frame_count,
57 path_ = std::move(file_path);
58 data_.clear();
59 data_key_.clear();
60 source_ = Source::image_file;
61 total_width_ = total_width;
62 total_height_ = total_height;
63 frame_count_ = frame_count;
64 orientation_ = orientation;
65 compute_frame_dimensions();
66 }
67
69 int frame_for_value(float value) const {
70 if (frame_count_ <= 0) return 0;
71 int idx = static_cast<int>(value * static_cast<float>(frame_count_ - 1) + 0.5f);
72 return std::clamp(idx, 0, frame_count_ - 1);
73 }
74
76 void frame_offset(int frame, int& x, int& y) const {
77 if (orientation_ == Orientation::vertical) {
78 x = 0;
79 y = frame * frame_height_;
80 } else {
81 x = frame * frame_width_;
82 y = 0;
83 }
84 }
85
86 bool loaded() const {
87 if (frame_count_ <= 0) return false;
88 return source_ == Source::raw_pixels ? !data_.empty() : !path_.empty();
89 }
90 Source source() const { return source_; }
91 const std::string& path() const { return path_; }
92 int frame_count() const { return frame_count_; }
93 int frame_width() const { return frame_width_; }
94 int frame_height() const { return frame_height_; }
95 int total_width() const { return total_width_; }
96 int total_height() const { return total_height_; }
97 Orientation orientation() const { return orientation_; }
98 const uint8_t* data() const { return data_.data(); }
99 size_t data_size() const { return data_.size(); }
102 const std::string& data_key() const {
103 return data_key_;
104 }
105
110 decoded_width_ = width;
111 decoded_height_ = height;
112 decoded_size_known_ = true;
113 }
114 bool decoded_size_known() const {
115 return decoded_size_known_;
116 }
117 int decoded_width() const {
118 return decoded_width_;
119 }
120 int decoded_height() const {
121 return decoded_height_;
122 }
123
125 void set_interpolate(bool v) { interpolate_ = v; }
126 bool interpolate() const { return interpolate_; }
127
128private:
129 void compute_frame_dimensions() {
130 if (orientation_ == Orientation::vertical) {
131 frame_width_ = total_width_;
132 frame_height_ = (frame_count_ > 0) ? total_height_ / frame_count_ : 0;
133 } else {
134 frame_width_ = (frame_count_ > 0) ? total_width_ / frame_count_ : 0;
135 frame_height_ = total_height_;
136 }
137 }
138
139 std::vector<uint8_t> data_;
140 std::string data_key_;
141 bool decoded_size_known_ = false;
142 int decoded_width_ = 0;
143 int decoded_height_ = 0;
144 std::string path_;
145 Source source_ = Source::raw_pixels;
146 int total_width_ = 0;
147 int total_height_ = 0;
148 int frame_width_ = 0;
149 int frame_height_ = 0;
150 int frame_count_ = 0;
151 Orientation orientation_ = Orientation::vertical;
152 bool interpolate_ = false;
153};
154
155} // namespace pulp::view
int total_height() const
Definition sprite_strip.hpp:96
void load_from_file(std::string file_path, int total_width, int total_height, int frame_count, Orientation orientation=Orientation::vertical)
Load from an encoded image file (PNG/JPEG/etc.).
Definition sprite_strip.hpp:53
Orientation orientation() const
Definition sprite_strip.hpp:97
const uint8_t * data() const
Definition sprite_strip.hpp:98
size_t data_size() const
Definition sprite_strip.hpp:99
bool decoded_size_known() const
Definition sprite_strip.hpp:114
int frame_for_value(float value) const
Get the frame index for a normalized value [0.0–1.0].
Definition sprite_strip.hpp:69
int frame_height() const
Definition sprite_strip.hpp:94
int decoded_width() const
Definition sprite_strip.hpp:117
bool loaded() const
Definition sprite_strip.hpp:86
int frame_width() const
Definition sprite_strip.hpp:93
void set_interpolate(bool v)
Whether to interpolate between adjacent frames for smooth sub-frame animation.
Definition sprite_strip.hpp:125
void set_decoded_size(int width, int height)
Pixel size a canvas decoder reported for data(), recorded by the painting widget on the first success...
Definition sprite_strip.hpp:109
const std::string & data_key() const
Lowercase hex SHA-256 of data(), computed when the strip loads; empty for an image_file strip.
Definition sprite_strip.hpp:102
int frame_count() const
Definition sprite_strip.hpp:92
bool interpolate() const
Definition sprite_strip.hpp:126
int decoded_height() const
Definition sprite_strip.hpp:120
const std::string & path() const
Definition sprite_strip.hpp:91
Source source() const
Definition sprite_strip.hpp:90
void frame_offset(int frame, int &x, int &y) const
Get the pixel offset (x, y) for a given frame index.
Definition sprite_strip.hpp:76
int total_width() const
Definition sprite_strip.hpp:95
void load(const uint8_t *data, size_t data_size, int total_width, int total_height, int frame_count, Orientation orientation=Orientation::vertical)
Load from raw pixel data (RGBA8, row-major).
Definition sprite_strip.hpp:32
Source
Where the strip's pixel data lives.
Definition sprite_strip.hpp:27
@ raw_pixels
Definition sprite_strip.hpp:27
@ image_file
Definition sprite_strip.hpp:27
Orientation
Definition sprite_strip.hpp:22
@ horizontal
Definition sprite_strip.hpp:22
@ vertical
Definition sprite_strip.hpp:22
std::string sha256_hex(const uint8_t *data, size_t size)
Compute SHA-256 hash as hex string (64 chars).
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ value
the widget's primary value (Knob, Fader, RangeSlider, Toggle, ProgressBar)
Definition binding_diagnostics.hpp:19
@ y
Definition input_events.hpp:42
@ x
Definition input_events.hpp:41
@ v
Definition input_events.hpp:41
@ height
Definition css_animation.hpp:128
@ width
Definition css_animation.hpp:127