From b62d148d7d2aae4fb0402460c550240b51438c48 Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Fri, 18 Oct 2024 09:39:44 +0100 Subject: [PATCH] Fixes from examples (#13) Fix bitmap rendering; shape bounds; and display for new file. --- src/tempe/bitmaps.py | 43 +++++++++++++++++++++++++++++++++++++------ src/tempe/display.py | 5 ++++- src/tempe/shapes.py | 4 ++-- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/tempe/bitmaps.py b/src/tempe/bitmaps.py index 40c3dae..a63d736 100644 --- a/src/tempe/bitmaps.py +++ b/src/tempe/bitmaps.py @@ -8,13 +8,13 @@ class Bitmaps(Shape): """Draw framebuffer bitmaps at points""" def __init__( - self, geometry, buffers, *, key=None, palette=None, surface=None, clip=None + self, geometry, buffers, *, key=-1, palette=None, surface=None, clip=None ): super().__init__(surface, clip=clip) self.geometry = geometry self.buffers = buffers - self.key = None - self.palette = None + self.key = key + self.palette = palette def update(self, geometry=None, buffers=None): if geometry is not None: @@ -30,10 +30,28 @@ def __iter__(self): yield from zip(self.geometry, self.buffers) def draw(self, buffer, x=0, y=0): - for geometry, buf in self: + if self.palette is not None: + palette = framebuf.FrameBuffer(self.palette, len(self.palette), 1, framebuf.RGB565) + for geometry, fbuf in self: px = geometry[0] - x py = geometry[1] - y - buffer.blit(buffer, px, py, self.key, self.palette) + if self.palette is not None: + buffer.blit(fbuf, px, py, self.key, palette) + else: + buffer.blit(fbuf, px, py, self.key) + + def _bounds(self): + max_x = -0x7fff + min_x = 0x7fff + max_y = -0x7fff + min_y = 0x7fff + for geometry in self.geometry: + max_x = max(max_x, geometry[0], geometry[0] + geometry[2]) + min_x = min(min_x, geometry[0], geometry[0] + geometry[2]) + max_y = max(max_y, geometry[1], geometry[1] + geometry[3]) + min_y = min(min_y, geometry[1], geometry[1] + geometry[3]) + + return (min_x, min_y, max_x - min_x, max_y - min_y) class ColoredBitmaps(ColoredGeometry): @@ -65,4 +83,17 @@ def draw(self, buffer, x=0, y=0): palette_buf[1] = color px = geometry[0] - x py = geometry[1] - y - buffer.blit(buffer, px, py, BLIT_KEY_RGB565, palette) + buffer.blit(buf, px, py, BLIT_KEY_RGB565, palette) + + def _bounds(self): + max_x = -0x7fff + min_x = 0x7fff + max_y = -0x7fff + min_y = 0x7fff + for geometry in self.geometry: + max_x = max(max_x, geometry[0], geometry[0] + geometry[2]) + min_x = min(min_x, geometry[0], geometry[0] + geometry[2]) + max_y = max(max_y, geometry[1], geometry[1] + geometry[3]) + min_y = min(min_y, geometry[1], geometry[1] + geometry[3]) + + return (min_x, min_y, max_x - min_x, max_y - min_y) diff --git a/src/tempe/display.py b/src/tempe/display.py index 6428c69..30fd5e1 100644 --- a/src/tempe/display.py +++ b/src/tempe/display.py @@ -39,7 +39,10 @@ def blit(self, buffer, x, y, w, h): def __enter__(self): if self._io is None: - self._io = open(self.name, 'r+b') + try: + self._io = open(self.name, 'r+b') + except OSError: + self._io = open(self.name, 'wb') def __exit__(self, *args): self._io.close() diff --git a/src/tempe/shapes.py b/src/tempe/shapes.py index 0b87acf..9b9b66d 100644 --- a/src/tempe/shapes.py +++ b/src/tempe/shapes.py @@ -166,7 +166,7 @@ def _bounds(self): max_y = max(max_y, max(geometry[1::2])) min_y = min(min_y, min(geometry[1::2])) - return (min_x, min_y, max_x - min_x, max_y - min_y) + return (min_x - 1, min_y - 1, max_x - min_x + 2, max_y - min_y + 2) class Rectangles(FillableGeometry): @@ -256,4 +256,4 @@ def _bounds(self): max_y = max(max_y, geometry[1] + abs(geometry[3])) min_y = min(min_y, geometry[1] - abs(geometry[3])) - return (min_x, min_y, max_x - min_x, max_y - min_y) + return (min_x - 1, min_y - 1, max_x - min_x + 2, max_y - min_y + 2)