Skip to content

Commit

Permalink
Fix bitmap rendering; shape bounds; and display for new file.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Oct 18, 2024
1 parent 624d7ee commit 287e857
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
43 changes: 37 additions & 6 deletions src/tempe/bitmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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)
5 changes: 4 additions & 1 deletion src/tempe/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/tempe/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 287e857

Please sign in to comment.