#include #include #include #include #include #include #include "matrix.hpp" #include #include "dbc.hpp" using matrix::Matrix; using namespace fmt; Matrix MAP{ {8,8,8,8,8,8,8,8,8}, {8,0,2,0,0,0,0,0,8}, {8,0,7,0,0,5,6,0,8}, {8,0,0,0,0,0,0,0,8}, {8,8,0,0,0,0,0,8,8}, {8,0,0,1,3,4,0,0,8}, {8,0,0,0,0,0,8,8,8}, {8,0,0,0,0,0,0,0,8}, {8,8,8,8,8,8,8,8,8} }; const int SCREEN_HEIGHT=480; const int SCREEN_WIDTH=SCREEN_HEIGHT * 2; const int THREED_VIEW_WIDTH=480; const int THREED_VIEW_HEIGHT=480; const int MAP_SIZE=matrix::width(MAP); const int TILE_SIZE=(SCREEN_WIDTH/2) / MAP_SIZE; const float FOV = std::numbers::pi / 3.0; const float HALF_FOV = FOV / 2; const int CASTED_RAYS=120; const float STEP_ANGLE = FOV / CASTED_RAYS; const int MAX_DEPTH = MAP_SIZE * TILE_SIZE; const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS; int PITCH=0; float player_x = SCREEN_WIDTH / 4; float player_y = SCREEN_WIDTH / 4; // x and y start position double posX = player_x / TILE_SIZE; double posY = player_y / TILE_SIZE; // initial direction vector double dirX = -1; double dirY = 0; // the 2d raycaster version of camera plane double planeX = 0; double planeY = 0.66; #define rgba_color(r,g,b,a) (r<<(0*8))|(g<<(1*8))|(b<<(2*8))|(a<<(3*8)) #define gray_color(c) rgba_color(c, c, c, 255) std::vector texture[8]; #define texWidth 64 // must be power of two #define texHeight 64 // must be power of two #define pixcoord(X, Y) ((Y) * SCREEN_HEIGHT) + (X) uint32_t pixels[SCREEN_HEIGHT * SCREEN_HEIGHT] = {0}; sf::Texture view_texture; sf::Sprite view_sprite; void loadImage(std::vector& texture, const char *filename) { sf::Image img; bool good = img.loadFromFile(filename); dbc::check(good, format("failed to load {}", filename)); uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr(); std::copy_n(pixbuf, texture.size(), texture.begin()); } void load_textures() { for(int i = 0; i < 8; i++) { texture[i].resize(texWidth * texHeight); } loadImage(texture[0], "pics/eagle.png"); loadImage(texture[1], "pics/redbrick.png"); loadImage(texture[2], "pics/purplestone.png"); loadImage(texture[3], "pics/greystone.png"); loadImage(texture[4], "pics/bluestone.png"); loadImage(texture[5], "pics/mossy.png"); loadImage(texture[6], "pics/wood.png"); loadImage(texture[7], "pics/colorstone.png"); } void draw_sfml_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, uint8_t color) { sf::RectangleShape rect(size); rect.setFillColor({color, color, color}); rect.setPosition(pos); window.draw(rect); } void draw_pixel_rect(sf::RenderWindow &window, Point pos, Point size, uint32_t color) { size_t x_start = size_t(pos.x - SCREEN_HEIGHT); size_t y_start = size_t(pos.y); size_t width = size_t(size.x); size_t height = size_t(size.y); for(size_t y = y_start; y < y_start + height; y++) { for(size_t x = x_start; x < x_start + width; x++) { size_t pixel_index = (y * SCREEN_HEIGHT) + x; pixels[pixel_index] = color; } } } void draw_pixel_buffer(sf::RenderWindow &window) { view_texture.update((uint8_t *)pixels, SCREEN_HEIGHT, SCREEN_HEIGHT, 0, 0); // BUG: can I do this once and just update it? window.draw(view_sprite); } void draw_map_rect(sf::RenderWindow &window, int x, int y, uint32_t color) { draw_sfml_rect(window, {float(x * TILE_SIZE), float(y * TILE_SIZE)}, {float(TILE_SIZE-1), float(TILE_SIZE-1)}, color); } void draw_map(sf::RenderWindow &window, Matrix &map) { uint32_t light_grey = gray_color(191); uint32_t dark_grey = gray_color(65); for(size_t y = 0; y < matrix::height(map); y++) { for(size_t x = 0; x < matrix::width(map); x++) { draw_map_rect(window, x, y, map[y][x] == 0 ? dark_grey : light_grey); } } } void draw_line(sf::RenderWindow &window, Point start, Point end, uint32_t color) { int x = int(start.x); int y = int(start.y); int x1 = int(end.x); int y1 = int(end.y); int dx = std::abs(x1 - x); int sx = x < x1 ? 1 : -1; int dy = std::abs(y1 - y) * -1; int sy = y < y1 ? 1 : -1; int error = dx + dy; while(x != x1 || y != y1) { int e2 = 2 * error; if(e2 >= dy) { error = error + dy; x = x + sx; } if(e2 <= dx) { error = error + dx; y = y + sy; } pixels[pixcoord(x,y)] = color; } } void clear(sf::RenderWindow &window) { std::fill_n(pixels, SCREEN_HEIGHT * SCREEN_HEIGHT, 0); window.clear(); } void draw_map_blocks(sf::RenderWindow &window, int col, int row) { draw_map_rect(window, col, row, rgba_color(100, 20, 20, 255)); } void ray_casting(sf::RenderWindow &window, Matrix& map) { int w = THREED_VIEW_WIDTH; int h = THREED_VIEW_HEIGHT; for(int x = 0; x < w; x++) { // calculate ray position and direction double cameraX = 2 * x / double(w) - 1; // x-coord in camera space double rayDirX = dirX + planeX * cameraX; double rayDirY = dirY + planeY * cameraX; // which box of the map we're in int mapX = int(posX); int mapY = int(posY); // length of ray from current pos to next x or y-side double sideDistX; double sideDistY; // length of ray from one x or y-side to next x or y-side double deltaDistX = std::abs(1.0 / rayDirX); double deltaDistY = std::abs(1.0 / rayDirY); double perpWallDist; int stepX = 0; int stepY = 0; int hit = 0; int side = 0; // calculate step and initial sideDist if(rayDirX < 0) { stepX = -1; sideDistX = (posX - mapX) * deltaDistX; } else { stepX = 1; sideDistX = (mapX + 1.0 - posX) * deltaDistX; } if(rayDirY < 0) { stepY = -1; sideDistY = (posY - mapY) * deltaDistY; } else { stepY = 1; sideDistY = (mapY + 1.0 - posY) * deltaDistY; } // perform DDA while(hit == 0) { if(sideDistX < sideDistY) { sideDistX += deltaDistX; mapX += stepX; side = 0; } else { sideDistY += deltaDistY; mapY += stepY; side = 1; } if(map[mapY][mapX] > 0) hit = 1; } if(side == 0) { perpWallDist = (sideDistX - deltaDistX); } else { perpWallDist = (sideDistY - deltaDistY); } draw_map_blocks(window, mapX, mapY); // TODO: player direction ray int lineHeight = int(h / perpWallDist); int drawStart = -lineHeight / 2 + h / 2 + PITCH; if(drawStart < 0) drawStart = 0; int drawEnd = lineHeight / 2 + h / 2 + PITCH; if(drawEnd >= h) drawEnd = h - 1; int texNum = MAP[mapY][mapX] - 1; // calculate value of wallX double wallX; // where exactly the wall was hit if(side == 0) { wallX = posY + perpWallDist * rayDirY; } else { wallX = posX + perpWallDist * rayDirX; } wallX -= floor((wallX)); // x coorindate on the texture int texX = int(wallX * double(texWidth)); if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1; if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1; // LODE: an integer-only bresenham or DDA like algorithm could make the texture coordinate stepping faster // How much to increase the texture coordinate per screen pixel double step = 1.0 * texHeight / lineHeight; // Starting texture coordinate double texPos = (drawStart - PITCH - h / 2 + lineHeight / 2) * step; for(int y = drawStart; y < drawEnd; y++) { // BUG? Why bitwise and here? int texY = (int)texPos & (texHeight - 1); texPos += step; uint32_t color = texture[texNum][texHeight * texY + texX]; pixels[pixcoord(x, y)] = color; } } } void draw_ceiling_floor(sf::RenderWindow &window) { int screenHeight = THREED_VIEW_HEIGHT; int screenWidth = THREED_VIEW_HEIGHT; for(int y = screenHeight / 2 + 1; y < screenHeight; ++y) { float rayDirX0 = dirX - planeX; float rayDirY0 = dirY - planeY; float rayDirX1 = dirX + planeX; float rayDirY1 = dirY + planeY; int p = y - screenHeight / 2; float posZ = 0.5 * screenHeight; float rowDistance = posZ / p; float floorStepX = rowDistance * (rayDirX1 - rayDirX0) / screenWidth; float floorStepY = rowDistance * (rayDirY1 - rayDirY0) / screenWidth; float floorX = posX + rowDistance * rayDirX0; float floorY = posY + rowDistance * rayDirY0; for(int x = 0; x < screenWidth; ++x) { int cellX = int(floorX); int cellY = int(floorY); int tx = int(texWidth * (floorX - cellX)) & (texWidth - 1); int ty = int(texWidth * (floorY - cellY)) & (texHeight - 1); floorX += floorStepX; floorY += floorStepY; int checkerBoardPattern = int(cellX + cellY) & 1; int floorTexture = checkerBoardPattern == 0 ? 3 : 4; int ceilingTexture = 6; uint32_t color; color = texture[floorTexture][texWidth * ty + tx]; pixels[pixcoord(x, y)] = color; color = texture[ceilingTexture][texWidth * ty + tx]; pixels[pixcoord(x, screenHeight - y - 1)] = color; } } } void draw_everything(sf::RenderWindow &window) { clear(window); draw_map(window, MAP); draw_ceiling_floor(window); ray_casting(window, MAP); draw_pixel_buffer(window); window.display(); } bool empty_space(int new_x, int new_y) { dbc::check((size_t)new_x < matrix::width(MAP), format("x={} too wide={}", new_x, matrix::width(MAP))); dbc::check((size_t)new_y < matrix::height(MAP), format("y={} too high={}", new_y, matrix::height(MAP))); return MAP[new_y][new_x] == 0; } int main() { using KB = sf::Keyboard; sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFMLCaster"); window.setVerticalSyncEnabled(true); view_texture.create(SCREEN_HEIGHT, SCREEN_HEIGHT); view_sprite.setTexture(view_texture); view_sprite.setPosition(THREED_VIEW_WIDTH, 0); load_textures(); double moveSpeed = 0.1; double rotSpeed = 0.1; while(window.isOpen()) { draw_everything(window); if(KB::isKeyPressed(KB::W)) { if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed; if(empty_space(int(posX), int(posY + dirY * moveSpeed))) posY += dirY * moveSpeed; } else if(KB::isKeyPressed(KB::S)) { if(empty_space(int(posX - dirX * moveSpeed), int(posY))) posX -= dirX * moveSpeed; if(empty_space(int(posX), int(posY - dirY * moveSpeed))) posY -= dirY * moveSpeed; } if(KB::isKeyPressed(KB::D)) { double oldDirX = dirX; dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed); dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed); double oldPlaneX = planeX; planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed); planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed); } else if(KB::isKeyPressed(KB::A)) { double oldDirX = dirX; dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed); dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed); double oldPlaneX = planeX; planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed); planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed); } if(KB::isKeyPressed(KB::E)) { println("PITCH DISABLED"); // PITCH = std::clamp(PITCH + 10, -60, 240); } else if(KB::isKeyPressed(KB::Q)) { println("PITCH DISABLED"); // PITCH = std::clamp(PITCH - 10, -60, 240); } sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed) { window.close(); } } } return 0; }