From 6d6a1e2b7df74e28577eb52307416233e251c120 Mon Sep 17 00:00:00 2001 From: Tobias Date: Thu, 24 Oct 2024 17:45:35 +0200 Subject: [PATCH] Allow spritesheets for PenRGB565 --- libraries/pico_graphics/pico_graphics.hpp | 3 +++ .../pico_graphics_pen_rgb565.cpp | 25 ++++++++++++++++++- micropython/modules/picographics/README.md | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/libraries/pico_graphics/pico_graphics.hpp b/libraries/pico_graphics/pico_graphics.hpp index 7445c534..d6fadc08 100644 --- a/libraries/pico_graphics/pico_graphics.hpp +++ b/libraries/pico_graphics/pico_graphics.hpp @@ -510,6 +510,9 @@ namespace pimoroni { int create_pen_hsv(float h, float s, float v) override; void set_pixel(const Point &p) override; void set_pixel_span(const Point &p, uint l) override; + + void sprite(void* data, const Point &sprite, const Point &dest, const int scale, const int transparent) override; + static size_t buffer_size(uint w, uint h) { return w * h * sizeof(RGB565); } diff --git a/libraries/pico_graphics/pico_graphics_pen_rgb565.cpp b/libraries/pico_graphics/pico_graphics_pen_rgb565.cpp index f17c6be4..30ef5bed 100644 --- a/libraries/pico_graphics/pico_graphics_pen_rgb565.cpp +++ b/libraries/pico_graphics/pico_graphics_pen_rgb565.cpp @@ -34,4 +34,27 @@ namespace pimoroni { *buf++ = color; } } -} \ No newline at end of file + + + void PicoGraphics_PenRGB565::sprite(void* data, const Point &sprite, const Point &dest, const int scale, const int transparent) { + //int sprite_x = (sprite & 0x0f) << 3; + //int sprite_y = (sprite & 0xf0) >> 1; + Point s { + sprite.x << 3, + sprite.y << 3 + }; + RGB565 *ptr = (RGB565 *)data; + Point o = {0, 0}; + for(o.y = 0; o.y < 8 * scale; o.y++) { + Point so = { + 0, + o.y / scale + }; + for(o.x = 0; o.x < 8 * scale; o.x++) { + so.x = o.x / scale; + color = ptr[(s.y + so.y) * 128 + (s.x + so.x)]; + if(color != transparent) pixel(dest + o); + } + } + } +} diff --git a/micropython/modules/picographics/README.md b/micropython/modules/picographics/README.md index afdb17e1..3f3b99eb 100644 --- a/micropython/modules/picographics/README.md +++ b/micropython/modules/picographics/README.md @@ -510,6 +510,8 @@ Sprites must be 8x8 pixels arranged in a 128x128 pixel spritesheet. 1-bit transp We've prepared some RGB332-compatible sprite assets for you, but you can use `spritesheet-to-rgb332.py ` to convert your own. +For higher quality you can use RGB565 Spritesheets on some devices, like the Tufty2040, but try using a lower spritesheet resolution of up to 128x96 pixels to not exceed device memory. + #### Loading Sprites You'll need to include the [pen_type](#supported-graphics-modes-pen-type) in the import statement, and define the pen_type before using loading the spritesheet: