Skip to content

Commit ab28481

Browse files
committed
Add support for scaled displays
Allows to use 2x2 pixels on larger displays. Also fix some bounds checking code.
1 parent 2ae4bb1 commit ab28481

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

stage.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,15 @@ class Stage:
567567
"""
568568
buffer = bytearray(512)
569569

570-
def __init__(self, display, fps=6):
570+
def __init__(self, display, fps=6, scale=None):
571+
if scale is None:
572+
self.scale = max(1, display.width // 128)
573+
else:
574+
self.scale = scale
571575
self.layers = []
572576
self.display = display
573-
self.width = display.width
574-
self.height = display.height
577+
self.width = display.width // self.scale
578+
self.height = display.height // self.scale
575579
self.last_tick = time.monotonic()
576580
self.tick_delay = 1 / fps
577581

@@ -588,10 +592,17 @@ def render_block(self, x0=0, y0=0, x1=None, y1=None):
588592
"""Update a rectangle of the screen."""
589593
if x1 is None:
590594
x1 = self.width
595+
else:
596+
x1 = min(max(1, x1), self.width)
591597
if y1 is None:
592598
y1 = self.height
599+
else:
600+
y1 = min(max(1, y1), self.height)
601+
if x0 >= x1 or y0 >= y1:
602+
return
593603
layers = [l.layer for l in self.layers]
594-
_stage.render(x0, y0, x1, y1, layers, self.buffer, self.display)
604+
_stage.render(x0, y0, x1, y1, layers, self.buffer,
605+
self.display, self.scale)
595606

596607
def render_sprites(self, sprites):
597608
"""Update the spots taken by all the sprites in the list."""
@@ -601,8 +612,8 @@ def render_sprites(self, sprites):
601612
y0 = max(0, min(self.height - 1, min(sprite.py, int(sprite.y))))
602613
x1 = max(1, min(self.width, max(sprite.px, int(sprite.x)) + 16))
603614
y1 = max(1, min(self.height, max(sprite.py, int(sprite.y)) + 16))
604-
if x0 == x1 or y0 == y1:
615+
if x0 >= x1 or y0 >= y1:
605616
continue
606617
_stage.render(x0, y0, x1, y1, layers, self.buffer,
607-
self.display)
618+
self.display, self.scale)
608619
sprite._updated()

0 commit comments

Comments
 (0)