Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes from examples #13

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)