Optimization: Do not reinitialize pixel array at every line draw

This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-10 10:09:20 +01:00
parent b398503ada
commit b063e5606e

View File

@ -107,6 +107,7 @@ class Main:
self.raycastWindow = sdl2.ext.Window("3D View", size=(RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT)) self.raycastWindow = sdl2.ext.Window("3D View", size=(RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT))
self.raycastWindow.show() self.raycastWindow.show()
self.raycastSurface = self.raycastWindow.get_surface() self.raycastSurface = self.raycastWindow.get_surface()
self.raycast_u32_pixels = ctypes.cast(self.raycastSurface.pixels, ctypes.POINTER(ctypes.c_uint32)) # Raw SDL surface pixel array
# Player # Player
self.player_position = PLAYER_SPAWN_POSITION self.player_position = PLAYER_SPAWN_POSITION
@ -379,10 +380,9 @@ class Main:
self.drawVline(self.raycastSurface, color, x, int(lineStart), int(lineEnd)) self.drawVline(self.raycastSurface, color, x, int(lineStart), int(lineEnd))
def drawVline(self, surface, color, x, startY, endY): def drawVline(self, surface, color, x, startY, endY):
u32_pixels = ctypes.cast(self.raycastSurface.pixels, ctypes.POINTER(ctypes.c_uint32));
startIdx = startY * RAYCAST_WIN_WIDTH + x startIdx = startY * RAYCAST_WIN_WIDTH + x
for idx in range(startIdx, endY * RAYCAST_WIN_WIDTH + x, RAYCAST_WIN_WIDTH): for idx in range(startIdx, endY * RAYCAST_WIN_WIDTH + x, RAYCAST_WIN_WIDTH):
u32_pixels[idx] = color self.raycast_u32_pixels[idx] = color
def dist(self, ax, ay, bx, by): def dist(self, ax, ay, bx, by):