PicoGraphics: Integer sprite scaling.

driver/sh1107
Phil Howard 2022-06-14 13:20:27 +01:00
rodzic f3d3dde04f
commit eb303ae811
5 zmienionych plików z 25 dodań i 11 usunięć

Wyświetl plik

@ -11,7 +11,7 @@ namespace pimoroni {
void PicoGraphics::set_pixel_dither(const Point &p, const RGB &c) {};
void PicoGraphics::set_pixel_dither(const Point &p, const RGB565 &c) {};
void PicoGraphics::scanline_convert(PenType type, conversion_callback_func callback) {};
void PicoGraphics::sprite(void* data, const Point &sprite, const Point &dest, const int transparent) {};
void PicoGraphics::sprite(void* data, const Point &sprite, const Point &dest, const int scale, const int transparent) {};
void PicoGraphics::set_dimensions(int width, int height) {
bounds = clip = {0, 0, width, height};

Wyświetl plik

@ -96,6 +96,7 @@ namespace pimoroni {
inline Point& operator-= (const Point &a) { x -= a.x; y -= a.y; return *this; }
inline Point& operator+= (const Point &a) { x += a.x; y += a.y; return *this; }
inline Point& operator/= (const int32_t a) { x /= a; y /= a; return *this; }
Point clamp(const Rect &r) const;
};
@ -105,6 +106,7 @@ namespace pimoroni {
inline Point operator- (Point lhs, const Point &rhs) { lhs -= rhs; return lhs; }
inline Point operator- (const Point &rhs) { return Point(-rhs.x, -rhs.y); }
inline Point operator+ (Point lhs, const Point &rhs) { lhs += rhs; return lhs; }
inline Point operator/ (Point lhs, const int32_t a) { lhs /= a; return lhs; }
struct Rect {
int32_t x = 0, y = 0, w = 0, h = 0;
@ -191,7 +193,7 @@ namespace pimoroni {
virtual void set_pixel_dither(const Point &p, const RGB &c);
virtual void set_pixel_dither(const Point &p, const RGB565 &c);
virtual void scanline_convert(PenType type, conversion_callback_func callback);
virtual void sprite(void* data, const Point &sprite, const Point &dest, const int transparent);
virtual void sprite(void* data, const Point &sprite, const Point &dest, const int scale, const int transparent);
void set_font(const bitmap::font_t *font);
void set_font(const hershey::font_t *font);
@ -269,7 +271,7 @@ namespace pimoroni {
void set_pixel_dither(const Point &p, const RGB &c) override;
void set_pixel_dither(const Point &p, const RGB565 &c) override;
void scanline_convert(PenType type, conversion_callback_func callback) override;
void sprite(void* data, const Point &sprite, const Point &dest, const int transparent) 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;
}

Wyświetl plik

@ -106,7 +106,7 @@ namespace pimoroni {
}
}
}
void PicoGraphics_PenRGB332::sprite(void* data, const Point &sprite, const Point &dest, const int transparent) {
void PicoGraphics_PenRGB332::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 {
@ -115,10 +115,15 @@ namespace pimoroni {
};
RGB332 *ptr = (RGB332 *)data;
Point o = {0, 0};
for(o.y = 0; o.y < 8; o.y++) {
for(o.x = 0; o.x < 8; o.x++) {
color = ptr[(s.y + o.y) * 128 + (s.x + o.x)];
if(color != transparent) set_pixel(dest + o);
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);
}
}
}

Wyświetl plik

@ -37,7 +37,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ModPicoGraphics_line_obj, 5, 5, ModPicoGraph
// Sprites
MP_DEFINE_CONST_FUN_OBJ_2(ModPicoGraphics_set_spritesheet_obj, ModPicoGraphics_set_spritesheet);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ModPicoGraphics_sprite_obj, 6, 6, ModPicoGraphics_sprite);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ModPicoGraphics_sprite_obj, 5, 7, ModPicoGraphics_sprite);
// Utility
//MP_DEFINE_CONST_FUN_OBJ_2(ModPicoGraphics_set_scanline_callback_obj, ModPicoGraphics_set_scanline_callback);

Wyświetl plik

@ -207,17 +207,24 @@ mp_obj_t ModPicoGraphics_set_spritesheet(mp_obj_t self_in, mp_obj_t spritedata)
}
mp_obj_t ModPicoGraphics_sprite(size_t n_args, const mp_obj_t *args) {
enum { ARG_self, ARG_sprite_x, ARG_sprite_y, ARG_x, ARG_y, ARG_transparent };
enum { ARG_self, ARG_sprite_x, ARG_sprite_y, ARG_x, ARG_y, ARG_scale, ARG_transparent };
ModPicoGraphics_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self], ModPicoGraphics_obj_t);
if(self->spritedata == nullptr) return mp_const_false;
int scale = 1;
int transparent = 0;
if(n_args >= 6) scale = mp_obj_get_int(args[ARG_scale]);
if(n_args >= 7) transparent = mp_obj_get_int(args[ARG_transparent]);
self->graphics->sprite(
self->spritedata,
{mp_obj_get_int(args[ARG_sprite_x]), mp_obj_get_int(args[ARG_sprite_y])},
{mp_obj_get_int(args[ARG_x]), mp_obj_get_int(args[ARG_y])},
mp_obj_get_int(args[ARG_transparent])
scale,
transparent
);
return mp_const_true;