Skip to content

Commit 2d556d1

Browse files
committed
PicoGraphics: Add layer support to PicoVector tile renderer.
1 parent 5a8b946 commit 2d556d1

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

libraries/pico_graphics/pico_graphics_pen_rgb332.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace pimoroni {
9999
// Treat our void* frame_buffer as uint8_t
100100
uint8_t *src = (uint8_t *)frame_buffer;
101101

102-
if(this->layers > 1) {
102+
if(this->layers > 1) {
103103
// The size of a single layer
104104
uint offset = this->bounds.w * this->bounds.h;
105105

@@ -148,14 +148,23 @@ namespace pimoroni {
148148
bool PicoGraphics_PenRGB332::render_tile(const Tile *tile) {
149149
for(int y = 0; y < tile->h; y++) {
150150
uint8_t *palpha = &tile->data[(y * tile->stride)];
151-
uint8_t *pdest = &((uint8_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
151+
152+
uint8_t *p_dest = &((uint8_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
153+
p_dest += this->layer_offset;
154+
155+
uint8_t *p_layer0 = &((uint8_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
156+
157+
152158
for(int x = 0; x < tile->w; x++) {
153159
uint8_t alpha = *palpha;
154-
uint8_t dest = *pdest;
160+
uint8_t dest = *p_dest;
161+
if(dest == 0) {
162+
dest = *p_layer0;
163+
}
155164

156165
// TODO: Try to alpha blend RGB332... somewhat?
157166
if(alpha == 255) {
158-
*pdest = color;
167+
*p_dest = color;
159168
}else if(alpha == 0) {
160169
}else{
161170
// blend tha pixel
@@ -172,10 +181,10 @@ namespace pimoroni {
172181
uint8_t b = ((sb * alpha) + (db * (255 - alpha))) >> 8;
173182

174183
// recombine the channels
175-
*pdest = (r << 5) | (g << 2) | (b);
184+
*p_dest = (r << 5) | (g << 2) | (b);
176185
}
177186

178-
pdest++;
187+
p_dest++;
179188
palpha++;
180189
}
181190
}

libraries/pico_graphics/pico_graphics_pen_rgb565.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace pimoroni {
6464
if(!bounds.contains(p)) return;
6565

6666
uint16_t *buf = (uint16_t *)frame_buffer;
67+
buf += this->layer_offset;
6768

6869
RGB565 blended = RGB(buf[p.y * bounds.w + p.x]).blend(RGB(color), a).to_rgb565();
6970

@@ -72,14 +73,22 @@ namespace pimoroni {
7273

7374
bool PicoGraphics_PenRGB565::render_tile(const Tile *tile) {
7475
for(int y = 0; y < tile->h; y++) {
75-
uint8_t *palpha = &tile->data[(y * tile->stride)];
76-
uint16_t *pdest = &((uint16_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
76+
uint8_t *p_alpha = &tile->data[(y * tile->stride)];
77+
78+
uint16_t *p_dest = &((uint16_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
79+
p_dest += this->layer_offset;
80+
81+
uint16_t *p_layer0 = &((uint16_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
82+
7783
for(int x = 0; x < tile->w; x++) {
78-
uint16_t dest = *pdest;
79-
uint8_t alpha = *palpha;
84+
uint16_t dest = *p_dest;
85+
if(dest == 0) {
86+
dest = *p_layer0;
87+
}
88+
uint8_t alpha = *p_alpha;
8089

8190
if(alpha == 255) {
82-
*pdest = color;
91+
*p_dest = color;
8392
}else if(alpha == 0) {
8493
}else{
8594
// blend tha pixel
@@ -96,11 +105,12 @@ namespace pimoroni {
96105
uint8_t b = ((sb * alpha) + (db * (255 - alpha))) >> 8;
97106

98107
// recombine the channels
99-
*pdest = __builtin_bswap16((r << 11) | (g << 5) | (b));
108+
*p_dest = __builtin_bswap16((r << 11) | (g << 5) | (b));
100109
}
101110

102-
pdest++;
103-
palpha++;
111+
p_layer0++;
112+
p_dest++;
113+
p_alpha++;
104114
}
105115
}
106116

0 commit comments

Comments
 (0)