From ec332a3e2d2faa7a4abd2350e8f868a95d434c58 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 5 Jan 2025 12:18:19 -0500 Subject: [PATCH] Small tweak to allow changing the map, and then added a hirdrac suggestion to vsync. --- raycaster.cpp | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/raycaster.cpp b/raycaster.cpp index 113be76..594b29f 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -8,9 +8,21 @@ using matrix::Matrix; using namespace fmt; +Matrix MAP{ + {1,1,1,1,1,1,1,1,1}, + {1,0,1,0,0,0,0,0,1}, + {1,0,1,0,0,1,1,0,1}, + {1,0,0,0,0,0,0,0,1}, + {1,1,0,0,0,0,0,0,1}, + {1,0,0,1,1,1,0,0,1}, + {1,0,0,0,1,0,0,0,1}, + {1,0,0,0,0,0,1,1,1}, + {1,1,1,1,1,1,1,1,1} +}; + const int SCREEN_HEIGHT=480; const int SCREEN_WIDTH=SCREEN_HEIGHT * 2; -const int MAP_SIZE=8; +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; @@ -19,17 +31,6 @@ const float STEP_ANGLE = FOV / CASTED_RAYS; const int MAX_DEPTH = MAP_SIZE * TILE_SIZE; const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS; -Matrix MAP{ - {1,1,1,1,1,1,1,1}, - {1,0,1,0,0,0,0,1}, - {1,0,1,0,0,1,1,1}, - {1,0,0,0,0,0,0,1}, - {1,1,0,0,0,0,0,1}, - {1,0,0,1,1,1,0,1}, - {1,0,0,0,1,0,0,1}, - {1,1,1,1,1,1,1,1} -}; - float player_x = SCREEN_WIDTH / 4; float player_y = SCREEN_WIDTH / 4; float player_angle = std::numbers::pi; @@ -44,7 +45,7 @@ void draw_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, ui void draw_map_rect(sf::RenderWindow &window, int x, int y, uint8_t color) { draw_rect(window, {float(x * TILE_SIZE), float(y * TILE_SIZE)}, - {TILE_SIZE-1, TILE_SIZE-1}, + {float(TILE_SIZE-1), float(TILE_SIZE-1)}, color); } @@ -107,6 +108,7 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) { int main() { sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Raycaster"); + window.setVerticalSyncEnabled(true); while(window.isOpen()) { sf::Event event; @@ -125,7 +127,6 @@ int main() { ray_casting(window, MAP); - window.display(); while(window.pollEvent(event)) {