diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2019-01-24 13:43:39 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott@tannewt.org> | 2019-01-31 11:42:13 -0800 |
| commit | 73cf490635809c2e5b1f57675d59507a247456f2 (patch) | |
| tree | 4fe1dbcbacccc6c701f64b689424c8e938c52c41 /shared-module | |
| parent | 63d456127b783efb376bd2b25598cc9e7f9dceb6 (diff) | |
Add TileGrid
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/displayio/Group.c | 12 | ||||
| -rw-r--r-- | shared-module/displayio/TileGrid.c | 124 | ||||
| -rw-r--r-- | shared-module/displayio/TileGrid.h | 55 |
3 files changed, 189 insertions, 2 deletions
diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c index 255349d7c..46898fb8e 100644 --- a/shared-module/displayio/Group.c +++ b/shared-module/displayio/Group.c @@ -28,6 +28,7 @@ #include "py/runtime.h" #include "shared-bindings/displayio/Sprite.h" +#include "shared-bindings/displayio/TileGrid.h" void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size) { mp_obj_t* children = m_new(mp_obj_t, max_size); @@ -43,7 +44,10 @@ void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer) native_layer = mp_instance_cast_to_native_base(layer, &displayio_sprite_type); } if (native_layer == MP_OBJ_NULL) { - mp_raise_ValueError(translate("Layer must be a Group or Sprite subclass.")); + native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type); + } + if (native_layer == MP_OBJ_NULL) { + mp_raise_ValueError(translate("Layer must be a Group or TileGrid subclass.")); } self->children[self->size] = layer; self->size++; @@ -77,7 +81,11 @@ bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, ui y /= self->scale; for (int32_t i = self->size - 1; i >= 0 ; i--) { mp_obj_t layer = self->children[i]; - if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) { + if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) { + if (displayio_tilegrid_get_pixel(layer, x, y, pixel)) { + return true; + } + } else if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) { if (displayio_sprite_get_pixel(layer, x, y, pixel)) { return true; } diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c new file mode 100644 index 000000000..7e4bfe0a4 --- /dev/null +++ b/shared-module/displayio/TileGrid.c @@ -0,0 +1,124 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/TileGrid.h" + +#include "shared-bindings/displayio/Bitmap.h" +#include "shared-bindings/displayio/ColorConverter.h" +#include "shared-bindings/displayio/OnDiskBitmap.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-bindings/displayio/Shape.h" + +void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_t bitmap, + mp_obj_t pixel_shader, uint16_t width, uint16_t height, + uint16_t tile_width, uint16_t tile_height, uint16_t x, uint16_t y, uint8_t default_tile) { + uint32_t total_tiles = width * height; + // Sprites will only have one tile so save a little memory by inlining values in the pointer. + uint8_t inline_tiles = sizeof(uint8_t*); + if (total_tiles <= inline_tiles) { + self->inline_tiles = true; + } else { + self->tiles = (uint8_t*) m_malloc(total_tiles, false); + self->inline_tiles = false; + } + self->width_in_tiles = width; + self->total_width = width * tile_width; + self->total_height = height * tile_height; + self->tile_width = tile_width; + self->tile_height = tile_height; + self->bitmap = bitmap; + self->pixel_shader = pixel_shader; + self->x = x; + self->y = y; +} + +void common_hal_displayio_tilegrid_get_position(displayio_tilegrid_t *self, int16_t* x, int16_t* y) { + *x = self->x; + *y = self->y; +} + +void common_hal_displayio_tilegrid_set_position(displayio_tilegrid_t *self, int16_t x, int16_t y) { + self->x = x; + self->y = y; + self->needs_refresh = true; +} + +mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *self) { + return self->pixel_shader; +} + +void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self, mp_obj_t pixel_shader) { + self->pixel_shader = pixel_shader; + self->needs_refresh = true; +} + +bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t y, uint16_t* pixel) { + x -= self->x; + y -= self->y; + if (y < 0 || y >= self->total_height || x >= self->total_width || x < 0) { + return false; + } + + uint8_t* tiles = self->tiles; + if (self->inline_tiles) { + tiles = (uint8_t*) &self->tiles; + } + uint16_t tile_location = (y / self->tile_height) * self->width_in_tiles + x / self->tile_width; + uint8_t tile = tiles[tile_location]; + uint16_t tile_x = tile_x = (tile % self->width_in_tiles) * self->tile_width + x % self->tile_width; + uint16_t tile_y = tile_y = (tile / self->width_in_tiles) * self->tile_height + y % self->tile_height; + + uint32_t value = 0; + if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) { + value = common_hal_displayio_bitmap_get_pixel(self->bitmap, tile_x, tile_y); + } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) { + value = common_hal_displayio_shape_get_pixel(self->bitmap, tile_x, tile_y); + } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) { + value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y); + } + + if (self->pixel_shader == mp_const_none) { + *pixel = value; + return true; + } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type) && displayio_palette_get_color(self->pixel_shader, value, pixel)) { + return true; + } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) && common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) { + return true; + } + + return false; +} + +bool displayio_tilegrid_needs_refresh(displayio_tilegrid_t *self) { + return self->needs_refresh || displayio_palette_needs_refresh(self->pixel_shader); +} + +void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) { + self->needs_refresh = false; + displayio_palette_finish_refresh(self->pixel_shader); + // TODO(tannewt): We could double buffer changes to position and move them over here. + // That way they won't change during a refresh and tear. +} diff --git a/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h new file mode 100644 index 000000000..880895304 --- /dev/null +++ b/shared-module/displayio/TileGrid.h @@ -0,0 +1,55 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H + +#include <stdbool.h> +#include <stdint.h> + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_t bitmap; + mp_obj_t pixel_shader; + uint16_t x; + uint16_t y; + uint16_t width_in_tiles; + uint16_t total_width; + uint16_t total_height; + uint16_t tile_width; + uint16_t tile_height; + uint8_t* tiles; + bool needs_refresh; + bool inline_tiles; +} displayio_tilegrid_t; + +bool displayio_tilegrid_get_pixel(displayio_tilegrid_t *self, int16_t x, int16_t y, uint16_t *pixel); +bool displayio_tilegrid_needs_refresh(displayio_tilegrid_t *self); +void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_TILEGRID_H |
