Created
February 19, 2020 19:34
-
-
Save treeform/bb3f4618d24535590043e264825f350d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sdl2 | |
| import math | |
| # ------ SDL2 CONF ------ | |
| discard sdl2.init(INIT_EVERYTHING) | |
| var | |
| window : WindowPtr | |
| render : RendererPtr | |
| window = createWindow("NIM RayCasting", 100,100,640,480, SDL_WINDOW_SHOWN or SDL_WINDOW_OPENGL) | |
| render = createRenderer(window, -1, Renderer_Accelerated or Renderer_PresentVsync) | |
| var | |
| evt = sdl2.defaultEvent | |
| runGame = true | |
| # ------ GAME DATA ------ | |
| #[ | |
| 0 -> No wall | |
| Other -> Wall (each number is a color) | |
| ]# | |
| var worldMap =[ | |
| [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1], | |
| [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1], | |
| [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], | |
| [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] | |
| ] | |
| var | |
| posX : float = 22 | |
| posY : float = 12 | |
| dirX : float = -1 | |
| diry : float = 0 | |
| planeX : float = 0 | |
| planeY : float = 0.66 | |
| time : uint32 = 0 | |
| oldTime : uint32 = 0 | |
| color : array[4, byte] = [0.byte,0,0,0] | |
| # ------ MAIN LOOP ------ | |
| while runGame: | |
| render.setDrawColor(0,0,0,255) | |
| render.clear | |
| oldTime = time | |
| for x in 0..640: | |
| var | |
| cameraX : float = 2.0 * float(x) / float(640) - 1 | |
| rayDirX : float = dirX + planeX * cameraX | |
| rayDirY : float = dirY + planeY * cameraX | |
| mapX : int32 = int32(posX) | |
| mapY : int32 = int32(posY) | |
| sideDistX : float | |
| sideDistY : float | |
| deltaDistX : float = abs(1 / rayDirX) | |
| deltaDistY : float = abs(1 / rayDirY) | |
| perpWallDist : float | |
| stepX : int32 | |
| stepY : int32 | |
| hit : int32 = 0 | |
| side : int32 | |
| if rayDirX < 0: | |
| stepX = -1 | |
| sideDistX = (posX - float(mapX)) * deltaDistX | |
| else: | |
| stepX = 1 | |
| sideDistX = (float(mapX) + 1.0 - posX) * deltaDistX | |
| if rayDirY < 0: | |
| stepY = -1 | |
| sideDistY = (posY - float(mapY)) * deltaDistY | |
| else: | |
| stepY = 1 | |
| sideDistY = (float(mapY) + 1.0 - posY) * deltaDistY | |
| # DDA Algorithm | |
| while hit == 0: | |
| if sideDistX < sideDistY: | |
| sideDistX += deltaDistX | |
| mapX += stepX | |
| side = 0 | |
| else: | |
| sideDistY += deltaDistY | |
| mapY += stepY | |
| side = 1 | |
| if worldMap[mapX][mapY] > 0 : hit = 1 | |
| if side == 0: | |
| perpWallDist = (float(mapX) - posX + (1 - stepX) / 2) / rayDirX | |
| else: | |
| perpWallDist = (float(mapY) - posY + (1 - stepY) / 2) / rayDirY | |
| var lineHeight : int32 = int32(480 / perpWallDist) | |
| var drawStart : int32 = int32(-lineHeight / 2 + 480 / 2) | |
| if drawStart < 0: drawStart = 0 | |
| var drawEnd : int32 = int32(lineHeight / 2 + 480 / 2) | |
| if drawEnd >= 480: drawEnd = 480 - 1 | |
| case worldMap[mapX][mapY] | |
| of 1: color = [245.byte,66,66,255] # Rojo | |
| of 2: color = [66.byte,255,95,255] # Verde | |
| of 3: color = [66.byte,81,245,255] # Azul | |
| of 4: color = [255.byte,255,255,255] # Blanco | |
| else: color = [255.byte,165,0,255] # Naranja | |
| if side == 1: | |
| for i in 0..2: # Change brigthness of the color | |
| color[i] = color[i] div 2 | |
| render.setDrawColor(uint8(color[0]),uint8(color[1]),uint8(color[2]),uint8(color[3])) | |
| render.drawLine(cint(x),cint(drawStart),cint(x),cint(drawEnd)) | |
| time = getTicks() | |
| var diff : uint32 = time - oldTime | |
| var frameTime : float = float(diff) / 1000.0 | |
| render.present | |
| var moveSpeed : float = frameTime * 5.0 | |
| var rotSpeed : float = frameTime * 3.0 | |
| while pollEvent(evt): | |
| if evt.kind == QuitEvent: | |
| runGame = false | |
| break | |
| # LookUp table for ScanCodes -> https://wiki.libsdl.org/SDLScancode3Lookup | |
| if evt.kind == KeyDown: | |
| if int32(evt.key.keysym.scancode) == 26: # W Key | |
| if worldMap[int32(posX + dirX * moveSpeed)][int32(posY)] == 0: posX += dirX * moveSpeed | |
| if worldMap[int32(posX)][int32(posY + dirY * moveSpeed)] == 0: posY += dirY * moveSpeed | |
| if int32(evt.key.keysym.scancode) == 22: # S Key | |
| if worldMap[int32(posX - dirX * moveSpeed)][int32(posY)] == 0: posX -= dirX * moveSpeed | |
| if worldMap[int32(posX)][int32(posY - dirY * moveSpeed)] == 0: posY -= dirY * moveSpeed | |
| if int32(evt.key.keysym.scancode) == 4: # A Key | |
| var oldDirX : float = dirX | |
| dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed) | |
| dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed) | |
| var oldPlaneX : float = planeX | |
| planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed) | |
| planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed) | |
| if int32(evt.key.keysym.scancode) == 7: # D Key | |
| var oldDirX : float = dirX | |
| dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed) | |
| dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed) | |
| var oldPlaneX : float = planeX | |
| planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed) | |
| planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed) | |
| destroy render | |
| destroy window | |
| sdl2.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment