Working 2D acceleration

This commit is contained in:
Daniele Verducci 2023-01-15 08:35:07 +01:00
parent 83bbd94057
commit 001d5ca4fa
4 changed files with 32 additions and 19 deletions

View File

@ -3,6 +3,10 @@
A simple [raycasting engine](https://en.wikipedia.org/wiki/Ray_casting), like the one used for [Wolfenstein 3D](https://en.wikipedia.org/wiki/Wolfenstein_3D), written in Python using the [SDL graphics library](https://en.wikipedia.org/wiki/Simple_DirectMedia_Layer). A simple [raycasting engine](https://en.wikipedia.org/wiki/Ray_casting), like the one used for [Wolfenstein 3D](https://en.wikipedia.org/wiki/Wolfenstein_3D), written in Python using the [SDL graphics library](https://en.wikipedia.org/wiki/Simple_DirectMedia_Layer).
Inspired by [this video of 3DSage](https://www.youtube.com/watch?v=gYRrGTC7GtA) Inspired by [this video of 3DSage](https://www.youtube.com/watch?v=gYRrGTC7GtA)
![Screenshot](images/screenshot.png)
![Screenshot](images/screenshot2.png)
## Requirements ## Requirements
Install SDL libs for Python and PNG lib (required for texture loading): Install SDL libs for Python and PNG lib (required for texture loading):
``` ```

BIN
images/screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

BIN
images/screenshot2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

View File

@ -48,13 +48,14 @@ TEXTURES = [
TEXTURE_SIZE = 64 TEXTURE_SIZE = 64
# Raycast cfg # Raycast cfg
RAYCAST_WIN_WIDTH = 1024 RAYCAST_WIN_WIDTH = 1000
RAYCAST_WIN_HEIGHT = 1024 RAYCAST_WIN_HEIGHT = 1000
RAYCAST_RENDER_WIDTH = int(RAYCAST_WIN_WIDTH / 4) RAYCAST_RENDER_MULTIPLIER = 4
RAYCAST_RENDER_HEIGHT = int(RAYCAST_WIN_HEIGHT / 4) RAYCAST_RENDER_WIDTH = int(RAYCAST_WIN_WIDTH / RAYCAST_RENDER_MULTIPLIER)
RAYCAST_RENDER_HEIGHT = int(RAYCAST_WIN_HEIGHT / RAYCAST_RENDER_MULTIPLIER)
DOF = 2*MAP_SIZE # Depth Of Field DOF = 2*MAP_SIZE # Depth Of Field
CEILING_COLOR = sdl2.ext.Color(0,128,255,255) CEILING_COLOR = [0,128,255]
FLOOR_COLOR = sdl2.ext.Color(64,64,64,255) FLOOR_COLOR = [64,64,64]
# Player cfg # Player cfg
PLAYER_SPEED = 8 PLAYER_SPEED = 8
@ -119,10 +120,9 @@ class Main:
self.mapWindow.show() self.mapWindow.show()
self.mapSurface = self.mapWindow.get_surface() self.mapSurface = self.mapWindow.get_surface()
self.raycastWindow = sdl2.ext.Window("3D View", size=(RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT)) self.raycastWindow = sdl2.SDL_CreateWindow(b"3D View", 100, 100, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT,sdl2.SDL_WINDOW_SHOWN)
self.raycastWindow.show() self.raycastRenderer = sdl2.SDL_CreateRenderer(self.raycastWindow, -1,sdl2.SDL_RENDERER_ACCELERATED |sdl2.SDL_RENDERER_PRESENTVSYNC)
self.raycastSurface = self.raycastWindow.get_surface() self.raycastSurface = sdl2.SDL_CreateRGBSurface(0,RAYCAST_WIN_WIDTH,RAYCAST_WIN_HEIGHT,32,0,0,0,0)
self.raycast_u32_pixels = ctypes.cast(self.raycastSurface.pixels, ctypes.POINTER(ctypes.c_uint32)) # Raw SDL surface pixel array
# Player # Player
self.player_position = {"x": int(MAP_SCALE * PLAYER_SPAWN_POSITION["x"]), "y": int(MAP_SCALE * PLAYER_SPAWN_POSITION["y"]), "r": PLAYER_SPAWN_POSITION["r"]} # r is rotation in radiants self.player_position = {"x": int(MAP_SCALE * PLAYER_SPAWN_POSITION["x"]), "y": int(MAP_SCALE * PLAYER_SPAWN_POSITION["y"]), "r": PLAYER_SPAWN_POSITION["r"]} # r is rotation in radiants
@ -179,7 +179,7 @@ class Main:
self.draw() self.draw()
if not MAP_HIDDEN: if not MAP_HIDDEN:
self.mapWindow.refresh() self.mapWindow.refresh()
self.raycastWindow.refresh() #self.raycastWindow.refresh()
# Calculate FPS # Calculate FPS
frames = frames + 1 frames = frames + 1
@ -214,7 +214,9 @@ class Main:
if not MAP_HIDDEN: if not MAP_HIDDEN:
self.draw2Dmap() self.draw2Dmap()
self.drawPlayer() self.drawPlayer()
self.drawRays() self.drawRays()
sdl2.SDL_RenderPresent(self.raycastRenderer)
def drawPlayer(self): def drawPlayer(self):
# Player in 2D map # Player in 2D map
@ -239,8 +241,12 @@ class Main:
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1)) sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(color,color,color,255), (posX, posY, MAP_SCALE - 1, MAP_SCALE - 1))
def drawRays(self): def drawRays(self):
sdl2.ext.draw.fill(self.raycastSurface, CEILING_COLOR, (0, 0, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws ceiling) # Ceiling
sdl2.ext.draw.fill(self.raycastSurface, FLOOR_COLOR, (0, RAYCAST_WIN_HEIGHT/2, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws floor) sdl2.SDL_SetRenderDrawColor(self.raycastRenderer, CEILING_COLOR[0], CEILING_COLOR[1], CEILING_COLOR[2], sdl2.SDL_ALPHA_OPAQUE)
sdl2.SDL_RenderClear(self.raycastRenderer)
# Floor
sdl2.SDL_SetRenderDrawColor(self.raycastRenderer, FLOOR_COLOR[0], FLOOR_COLOR[1], FLOOR_COLOR[2], sdl2.SDL_ALPHA_OPAQUE)
sdl2.SDL_RenderFillRect(self.raycastRenderer, sdl2.SDL_Rect(0, int(RAYCAST_WIN_HEIGHT/2), RAYCAST_WIN_WIDTH, int(RAYCAST_WIN_HEIGHT)))
# Casts rays for raycasting # Casts rays for raycasting
playerAngle = self.player_position["r"] playerAngle = self.player_position["r"]
@ -394,12 +400,15 @@ class Main:
continue continue
# Draw segment (all is scaled x4) # Draw segment (all is scaled x4)
x = i * 4
for idx in range(int(lineStart * 4) * RAYCAST_WIN_WIDTH + x, int(lineEnd * 4) * RAYCAST_WIN_WIDTH + x, RAYCAST_WIN_WIDTH): b = color & 0b000000000000000011111111
self.raycast_u32_pixels[idx] = color g = color >> 8 & 0b000000000000000011111111
self.raycast_u32_pixels[idx + 1] = color r = color >> 16 & 0b000000000000000011111111
self.raycast_u32_pixels[idx + 2] = color sdl2.SDL_SetRenderDrawColor(self.raycastRenderer, r, g, b, sdl2.SDL_ALPHA_OPAQUE) # Non fare in tutti i cicli
self.raycast_u32_pixels[idx + 3] = color
x = i * RAYCAST_RENDER_MULTIPLIER
#sdl2.SDL_RenderFillRect(self.raycastRenderer, sdl2.SDL_Rect(x, int(lineStart * RAYCAST_RENDER_MULTIPLIER), RAYCAST_RENDER_MULTIPLIER, int((lineEnd - lineStart) * RAYCAST_RENDER_MULTIPLIER) + 1))
sdl2.SDL_RenderFillRectF(self.raycastRenderer, sdl2.SDL_FRect(x, lineStart * RAYCAST_RENDER_MULTIPLIER, RAYCAST_RENDER_MULTIPLIER, (lineEnd - lineStart) * RAYCAST_RENDER_MULTIPLIER))
def shade(self, color): def shade(self, color):
# Obtain channels # Obtain channels