From b4f1cf63531ba7553f2effcf8cebae0ed731f1cf Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Tue, 10 Jan 2023 19:01:57 +0100 Subject: [PATCH] Better clipping --- v2/raycaster.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/v2/raycaster.py b/v2/raycaster.py index 7946ab2..ac216d7 100755 --- a/v2/raycaster.py +++ b/v2/raycaster.py @@ -366,20 +366,28 @@ class Main: # Clipping lineEnd = textureSegmentEnd - if lineEnd > lineOffset + lineHeight: - lineEnd = lineOffset + lineHeight + if lineEnd > RAYCAST_RENDER_HEIGHT: + lineEnd = RAYCAST_RENDER_HEIGHT lineStart = textureSegmentStart if lineStart < 0: lineStart = 0 + if lineEnd < lineStart: + continue + # Upscaling lineStart = lineStart * RAYCAST_RESOLUTION_SCALING lineEnd = lineEnd * RAYCAST_RESOLUTION_SCALING + # Draw segment for repeat in range(1, RAYCAST_RESOLUTION_SCALING + 1): x = i * RAYCAST_RESOLUTION_SCALING + repeat self.drawVline(self.raycastSurface, color, x, int(lineStart), int(lineEnd)) def drawVline(self, surface, color, x, startY, endY): + if x < 0 or x > RAYCAST_WIN_WIDTH or startY < 0 or endY > RAYCAST_WIN_HEIGHT or endY < startY: + print("Trying to write outside bounds: vertical line with x {} from y {} to y {}".format(x, startY, endY)) + return + startIdx = startY * RAYCAST_WIN_WIDTH + x for idx in range(startIdx, endY * RAYCAST_WIN_WIDTH + x, RAYCAST_WIN_WIDTH): self.raycast_u32_pixels[idx] = color