Basic transliteration of the pycaster.py to work in SFML. The grid map on the left doesn't work right, but the rendering on the right does. Also the key detection is using discrete keypress instead of key up/down.

master
Zed A. Shaw 4 days ago
parent faead338e2
commit d5a372e751
  1. 68
      raycaster.cpp

@ -3,6 +3,7 @@
#include <numbers>
#include <cmath>
#include "matrix.hpp"
#include <cstdlib>
using matrix::Matrix;
using namespace fmt;
@ -33,20 +34,27 @@ float player_x = SCREEN_WIDTH / 4;
float player_y = SCREEN_WIDTH / 4;
float player_angle = std::numbers::pi;
void draw_map_rect(sf::RenderWindow &window, sf::Color color, int x, int y) {
sf::RectangleShape rect({TILE_SIZE-1, TILE_SIZE-1});
rect.setFillColor(color);
rect.setPosition(x * TILE_SIZE, y * TILE_SIZE);
void draw_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_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},
color);
}
void draw_map(sf::RenderWindow &window, Matrix &map) {
sf::Color light_grey{191, 191, 191};
sf::Color dark_grey{65,65,65};
uint8_t light_grey = 191;
uint8_t dark_grey = 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, map[y][x] == 1 ? light_grey : dark_grey, x, y);
draw_map_rect(window, x, y, map[y][x] == 1 ? light_grey : dark_grey);
}
}
}
@ -73,8 +81,24 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
int row = int(target_y / TILE_SIZE);
if(map[row][col] == 1) {
draw_map_rect(window, {195, 137, 38}, col & TILE_SIZE, row * TILE_SIZE);
draw_map_rect(window, col & TILE_SIZE, row * TILE_SIZE, 100);
draw_line(window, {player_x, player_y}, {target_x, target_y});
uint8_t color = 255 / (1 + depth * depth * 0.0001);
float fixed_depth = depth * std::cos(player_angle - start_angle);
float wall_height = 21000 / fixed_depth;
if(wall_height > SCREEN_HEIGHT){
wall_height = SCREEN_HEIGHT;
}
draw_rect(window,
{SCREEN_HEIGHT + ray * SCALE, (SCREEN_HEIGHT / 2) - wall_height / 2},
{SCALE, wall_height},
color);
break;
}
}
@ -88,12 +112,40 @@ int main() {
sf::Event event;
draw_map(window, MAP);
draw_rect(window,
{480, SCREEN_HEIGHT /2},
{SCREEN_HEIGHT, SCREEN_HEIGHT},
100);
draw_rect(window,
{480, (SCREEN_HEIGHT * -1) / 2},
{SCREEN_HEIGHT, SCREEN_HEIGHT},
200);
ray_casting(window, MAP);
window.display();
while(window.pollEvent(event)) {
using KB = sf::Keyboard;
if(event.type == sf::Event::Closed) {
window.close();
} else if(event.type == sf::Event::KeyPressed) {
if(KB::isKeyPressed(KB::Left)) {
player_angle -= 0.1;
} else if(KB::isKeyPressed(KB::Right)) {
player_angle += 0.1;
} else if(KB::isKeyPressed(KB::Up)) {
player_x += -1 * std::sin(player_angle) * 5;
player_y += std::cos(player_angle) * 5;
} else if(KB::isKeyPressed(KB::Down)) {
player_x -= -1 * std::sin(player_angle) * 5;
player_y -= std::cos(player_angle) * 5;
}
}
}
}

Loading…
Cancel
Save