Skip to content

Commit c8d2c3f

Browse files
committed
Add support for viewport scrolling
1 parent e531c94 commit c8d2c3f

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

stage.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,6 @@ def set_frame(self, frame=None, rotation=None):
482482
def update(self):
483483
pass
484484

485-
def _updated(self):
486-
self.px = int(self.x)
487-
self.py = int(self.y)
488-
489485

490486
class Text:
491487
"""Text layer. For displaying text."""
@@ -584,6 +580,8 @@ def __init__(self, display, fps=6, scale=None):
584580
self.height = display.height // self.scale
585581
self.last_tick = time.monotonic()
586582
self.tick_delay = 1 / fps
583+
self.vx = 0
584+
self.vy = 0
587585

588586
def tick(self):
589587
"""Wait for the start of the next frame."""
@@ -594,32 +592,39 @@ def tick(self):
594592
else:
595593
self.last_tick = time.monotonic()
596594

597-
def render_block(self, x0=0, y0=0, x1=None, y1=None):
595+
def render_block(self, x0=None, y0=None, x1=None, y1=None):
598596
"""Update a rectangle of the screen."""
597+
if x0 is None:
598+
x0 = self.vx
599+
if y0 is None:
600+
y0 = self.vy
599601
if x1 is None:
600-
x1 = self.width
601-
else:
602-
x1 = min(max(1, x1), self.width)
602+
x1 = self.width + self.vx
603603
if y1 is None:
604-
y1 = self.height
605-
else:
606-
y1 = min(max(1, y1), self.height)
604+
y1 = self.height + self.vy
605+
x0 = min(max(0, x0 - self.vx), self.width - 1)
606+
y0 = min(max(0, y0 - self.vy), self.height - 1)
607+
x1 = min(max(1, x1 - self.vx), self.width)
608+
y1 = min(max(1, y1 - self.vy), self.height)
607609
if x0 >= x1 or y0 >= y1:
608610
return
609611
layers = [l.layer for l in self.layers]
610612
_stage.render(x0, y0, x1, y1, layers, self.buffer,
611-
self.display, self.scale)
613+
self.display, self.scale, self.vx, self.vy)
612614

613615
def render_sprites(self, sprites):
614616
"""Update the spots taken by all the sprites in the list."""
615617
layers = [l.layer for l in self.layers]
616618
for sprite in sprites:
617-
x0 = max(0, min(self.width - 1, min(sprite.px, int(sprite.x))))
618-
y0 = max(0, min(self.height - 1, min(sprite.py, int(sprite.y))))
619-
x1 = max(1, min(self.width, max(sprite.px, int(sprite.x)) + 16))
620-
y1 = max(1, min(self.height, max(sprite.py, int(sprite.y)) + 16))
619+
x = int(sprite.x) - self.vx
620+
y = int(sprite.y) - self.vy
621+
x0 = max(0, min(self.width - 1, min(sprite.px, x)))
622+
y0 = max(0, min(self.height - 1, min(sprite.py, y)))
623+
x1 = max(1, min(self.width, max(sprite.px, x) + 16))
624+
y1 = max(1, min(self.height, max(sprite.py, y) + 16))
625+
sprite.px = x
626+
sprite.py = y
621627
if x0 >= x1 or y0 >= y1:
622628
continue
623629
_stage.render(x0, y0, x1, y1, layers, self.buffer,
624-
self.display, self.scale)
625-
sprite._updated()
630+
self.display, self.scale, self.vx, self.vy)

0 commit comments

Comments
 (0)