FastLED 3.9.15
Loading...
Searching...
No Matches
tile2x2.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stdint.h"
4
5#include "fl/geometry.h"
6#include "fl/namespace.h"
7#include "fl/pair.h"
8#include "fl/span.h"
9#include "fl/xymap.h"
10#include "fl/vector.h"
11
13struct CRGB;
15
16namespace fl {
17
18class XYMap;
20
21
23
24 public:
25 static void Rasterize(const span<const Tile2x2_u8> &tiles,
26 XYRasterU8Sparse *output);
27
28 Tile2x2_u8() = default;
30 Tile2x2_u8(const Tile2x2_u8 &) = default;
31 Tile2x2_u8 &operator=(const Tile2x2_u8 &) = default;
32 Tile2x2_u8(Tile2x2_u8 &&) = default;
33
34 void scale(u8 scale);
35
36 void setOrigin(u16 x, u16 y) { mOrigin = vec2<u16>(x, y); }
37
38 u8 &operator()(int x, int y) { return at(x, y); }
39 u8 &at(int x, int y) { return mTile[y][x]; }
40 const u8 &at(int x, int y) const { return mTile[y][x]; }
41
42 u8 &lower_left() { return at(0, 0); }
43 u8 &upper_left() { return at(0, 1); }
44 u8 &lower_right() { return at(1, 0); }
45 u8 &upper_right() { return at(1, 1); }
46
47 const u8 &lower_left() const { return at(0, 0); }
48 const u8 &upper_left() const { return at(0, 1); }
49 const u8 &lower_right() const { return at(1, 0); }
50 const u8 &upper_right() const { return at(1, 1); }
51
52 u8 maxValue() const;
53
54 static Tile2x2_u8 MaxTile(const Tile2x2_u8 &a, const Tile2x2_u8 &b);
55
56 vec2<u16> origin() const { return mOrigin; }
57
59 rect<u16> bounds() const;
60
61 // Draws the subpixel tile to the led array.
62 void draw(const CRGB &color, const XYMap &xymap, CRGB *out) const;
63
64 // Inlined, yet customizable drawing access. This will only send you pixels
65 // that are within the bounds of the XYMap.
66 // Why use a template? Speed.
67 // You have an array of Tile2x2_u8 in a draw list and you need to dispatch
68 // them fast. Templates will inline completely for max speed.
69 template <typename XYVisitor>
70 void draw(const XYMap &xymap, XYVisitor &visitor) const {
71 for (u16 x = 0; x < 2; ++x) {
72 for (u16 y = 0; y < 2; ++y) {
73 u8 value = at(x, y);
74 if (value > 0) {
75 int xx = mOrigin.x + x;
76 int yy = mOrigin.y + y;
77 if (xymap.has(xx, yy)) {
78 // we know index cannot be -1 because we checked has(xx, yy) above.
79 u16 ux = static_cast<u16>(xx);
80 u16 uy = static_cast<u16>(yy);
81 int index = xymap.mapToIndex(ux, uy);
82 if (index >= 0) {
83 u32 uindex = static_cast<u32>(index);
84 vec2<u16> pt(ux, uy);
85 visitor.draw(pt, uindex, value);
86 } else {
87 // Unexpected because has(xx, yy) is true above therefore
88 // index cannot be < 0. TODO: low level log this.
89 }
90 }
91 }
92 }
93 }
94 }
95
96 private:
97 u8 mTile[2][2] = {};
98 // Subpixels can be rendered outside the viewport so this must be signed.
100};
101
103 // This is a class that is like a Tile2x2_u8 but wraps around the edges.
104 // This is useful for cylinder mapping where the x-coordinate wraps around
105 // the width of the cylinder and the y-coordinate wraps around the height.
106 // This converts a tile2x2 to a wrapped x,y version.
107 public:
108 using Entry = fl::pair<vec2<u16>, u8>; // absolute position, alpha
109 using Data = Entry[2][2];
110
112 Tile2x2_u8_wrap(const Tile2x2_u8 &from, u16 width);
113 Tile2x2_u8_wrap(const Tile2x2_u8 &from, u16 width, u16 height);
114
115 Tile2x2_u8_wrap(const Data& data);
116
117 // Returns the absolute position and the alpha.
118 Entry &at(u16 x, u16 y);
119 const Entry &at(u16 x, u16 y) const;
120
121 // Interpolates between two wrapped tiles and returns up to 2 interpolated tiles
123
124 private:
126};
127
128} // namespace fl
int y
Definition simple.h:93
int x
Definition simple.h:92
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
uint16_t scale
Definition Noise.ino:74
Entry & at(u16 x, u16 y)
Definition tile2x2.cpp:65
fl::pair< vec2< u16 >, u8 > Entry
Definition tile2x2.h:108
static vector_fixed< Tile2x2_u8_wrap, 2 > Interpolate(const Tile2x2_u8_wrap &a, const Tile2x2_u8_wrap &b, float t)
Definition tile2x2.cpp:127
Entry[2][2] Data
Definition tile2x2.h:109
const u8 & at(int x, int y) const
Definition tile2x2.h:40
static Tile2x2_u8 MaxTile(const Tile2x2_u8 &a, const Tile2x2_u8 &b)
Definition tile2x2.cpp:111
const u8 & upper_left() const
Definition tile2x2.h:48
Tile2x2_u8(const Tile2x2_u8 &)=default
const u8 & lower_right() const
Definition tile2x2.h:49
u8 & at(int x, int y)
Definition tile2x2.h:39
u8 mTile[2][2]
Definition tile2x2.h:97
void scale(u8 scale)
Definition tile2x2.cpp:43
Tile2x2_u8(const vec2< u16 > &origin)
Definition tile2x2.h:29
static void Rasterize(const span< const Tile2x2_u8 > &tiles, XYRasterU8Sparse *output)
Definition tile2x2.cpp:33
u8 maxValue() const
Definition tile2x2.cpp:102
u8 & lower_right()
Definition tile2x2.h:44
void setOrigin(u16 x, u16 y)
Definition tile2x2.h:36
const u8 & upper_right() const
Definition tile2x2.h:50
void draw(const CRGB &color, const XYMap &xymap, CRGB *out) const
Definition tile2x2.cpp:38
Tile2x2_u8(Tile2x2_u8 &&)=default
u8 & upper_right()
Definition tile2x2.h:45
u8 & operator()(int x, int y)
Definition tile2x2.h:38
Tile2x2_u8 & operator=(const Tile2x2_u8 &)=default
const u8 & lower_left() const
Definition tile2x2.h:47
u8 & lower_left()
Definition tile2x2.h:42
u8 & upper_left()
Definition tile2x2.h:43
vec2< u16 > origin() const
Definition tile2x2.h:56
void draw(const XYMap &xymap, XYVisitor &visitor) const
Definition tile2x2.h:70
rect< u16 > bounds() const
bounds => [begin_x, end_x) (where end_x is exclusive)
Definition tile2x2.cpp:121
Tile2x2_u8()=default
vec2< u16 > mOrigin
Definition tile2x2.h:99
static uint32_t t
Definition Luminova.h:54
#define FASTLED_NAMESPACE_END
Definition namespace.h:23
#define FASTLED_NAMESPACE_BEGIN
Definition namespace.h:22
Implements the FastLED namespace macros.
unsigned char u8
Definition int.h:17
Slice< T > span
Definition span.h:8
FixedVector< T, INLINED_SIZE > vector_fixed
Definition vector.h:1217
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
Definition pair.h:9