You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.6 KiB
103 lines
2.6 KiB
5 days ago
|
#include <fmt/core.h>
|
||
|
#include <SFML/Graphics.hpp>
|
||
|
#include <numbers>
|
||
|
#include <cmath>
|
||
|
#include "matrix.hpp"
|
||
|
|
||
|
using matrix::Matrix;
|
||
|
using namespace fmt;
|
||
|
|
||
|
const int SCREEN_HEIGHT=480;
|
||
|
const int SCREEN_WIDTH=SCREEN_HEIGHT * 2;
|
||
|
const int MAP_SIZE=8;
|
||
|
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=30;
|
||
|
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;
|
||
|
|
||
|
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);
|
||
|
window.draw(rect);
|
||
|
}
|
||
|
|
||
|
void draw_map(sf::RenderWindow &window, Matrix &map) {
|
||
|
sf::Color light_grey{191, 191, 191};
|
||
|
sf::Color dark_grey{65,65,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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void draw_line(sf::RenderWindow &window, sf::Vector2f start, sf::Vector2f end) {
|
||
|
sf::Vertex line[] = {
|
||
|
sf::Vertex(start),
|
||
|
sf::Vertex(end)
|
||
|
};
|
||
|
|
||
|
window.draw(line, 2, sf::Lines);
|
||
|
}
|
||
|
|
||
|
void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
||
|
float start_angle = player_angle - HALF_FOV;
|
||
|
|
||
|
for(int ray = 0; ray < CASTED_RAYS; ray++, start_angle += STEP_ANGLE)
|
||
|
{
|
||
|
for(int depth = 1; depth < MAX_DEPTH; depth++) {
|
||
|
float target_x = player_x - std::sin(start_angle) * depth;
|
||
|
float target_y = player_y + std::cos(start_angle) * depth;
|
||
|
|
||
|
int col = int(target_x / TILE_SIZE);
|
||
|
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_line(window, {player_x, player_y}, {target_x, target_y});
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Raycaster");
|
||
|
|
||
|
while(window.isOpen()) {
|
||
|
sf::Event event;
|
||
|
|
||
|
draw_map(window, MAP);
|
||
|
ray_casting(window, MAP);
|
||
|
window.display();
|
||
|
|
||
|
while(window.pollEvent(event)) {
|
||
|
if(event.type == sf::Event::Closed) {
|
||
|
window.close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|