Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SFML/Graphics.hpp"
- #include "iostream"
- #include "math.h"
- constexpr float PI = 3.14159265358979323846264338327952f;
- constexpr float dr = 0.0174532925f;
- float deg_to_rad(float i_degrees)
- {
- return PI * i_degrees / 180;
- }
- int main() {
- float gridF = 64.f;
- float dist[60]{};
- float io[60]{};
- sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML Application");
- window.setFramerateLimit(60);
- sf::RectangleShape shape(sf::Vector2f(10.f, 10.f));
- shape.setPosition(300.f, 300.f);
- shape.setFillColor(sf::Color::Cyan);
- sf::RectangleShape line(sf::Vector2f(70.f, 3.f));
- line.setFillColor(sf::Color::Cyan);
- sf::RectangleShape shape1[60];
- float timer;
- float timerMax;
- float px, py, pdx, pdy;
- float pa = 0;
- sf::CircleShape c;
- c.setRadius(3.f);
- c.setPointCount(100.f);
- float yx;
- float xy;
- sf::Text text;
- sf::Font font;
- float rad[60]{};
- int map[][8] = {
- {1, 1, 1, 1, 1, 1, 1, 1},
- {1, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 0, 1},
- {1, 1, 1, 1, 1, 1, 1, 1}
- };
- int mapX = 8;
- int mapY = 8;
- px = 300.f;
- float projection;
- py = 300.f;
- timerMax = 1.5f;
- timer = timerMax;
- pdx = cos(pa) * 5;
- pdy = sin(pa) * 5;
- sf::Clock clock;
- sf::VertexArray cast(sf::LineStrip, 2.f);
- sf::VertexArray lines(sf::LinesStrip,60);
- sf::RectangleShape tile[mapY][mapX];
- for (int y = 0; y < mapY; y++) {
- for (int x = 0; x < mapX; x++) {
- if (map[y][x] == 1) {
- tile[y][x].setSize(sf::Vector2f(gridF, gridF));
- tile[y][x].setFillColor(sf::Color(55, 55, 55, 255));
- tile[y][x].setOutlineThickness(1.f);
- tile[y][x].setOutlineColor(sf::Color::White);
- tile[y][x].setPosition(x * gridF, y * gridF);
- } else {
- tile[y][x].setSize(sf::Vector2f(gridF, gridF));
- tile[y][x].setFillColor(sf::Color::Transparent);
- tile[y][x].setOutlineThickness(1.f);
- tile[y][x].setOutlineColor(sf::Color::White);
- tile[y][x].setPosition(x * gridF, y * gridF);
- }
- }
- }
- float a;
- float ra;
- pa = PI * 2;
- sf::VertexArray fov{sf::TriangleFan,60+1};
- while (window.isOpen()) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && timer >= timerMax) {
- pa -= 0.1f;
- pdx = cos(pa) * 5;
- pdy = sin(pa) * 5;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && timer >= timerMax) {
- pa += 0.1f;
- pdx = cos(pa) * 5;
- pdy = sin(pa) * 5;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
- px += pdx;
- py += pdy;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
- px -= pdx;
- py -= pdy;
- }
- if (timer >= timerMax) {
- timer = 0;
- } else {
- timer += 1.f;
- }
- shape.setPosition(px, py);
- xy = px + (shape.getSize().x / 2);
- yx = py + (shape.getSize().y / 2);
- cast[0].position = sf::Vector2f(xy, yx);
- cast[1].position = sf::Vector2f(xy + (pdx * 180 / PI), yx + (pdy * 180 / PI));
- for (int i = 0; i < 60; i++) {
- ra = pa + (float(30-i)* dr);
- float dir_y = sin(ra);
- float dir_x = cos(ra);
- int mapx = floor(px / 64);
- int mapy = floor(py / 64);
- float sideDistX;
- float sideDistY;
- int step_x;
- int step_y;
- int hit = 0;
- float delta_x = sqrt(1.f + (pow(dir_y, 2) / pow(dir_x, 2))) * gridF;
- float delta_y = sqrt(1.f + (pow(dir_x, 2) / pow(dir_y, 2))) * gridF;
- if (dir_x < 0) {
- step_x = -1;
- sideDistX = ((xy / 64) - float(mapx)) * delta_x;
- } else {
- step_x = 1;
- sideDistX = ((float(mapx) + 1.f) - (xy / 64)) * delta_x;
- }
- if (dir_y < 0) {
- step_y = -1;
- sideDistY = ((yx / 64) - float(mapy)) * delta_y;
- } else {
- step_y = 1;
- sideDistY = ((float(mapy) + 1.f) - (yx / 64)) * delta_y;
- }
- while (hit == 0) {
- if (sideDistX < sideDistY) {
- a = sideDistX;
- mapx += step_x;
- sideDistX += delta_x;
- }
- else{
- a = sideDistY;
- mapy += step_y;
- sideDistY += delta_y;
- }
- //Check if ray has hit a wall
- if (mapy < 8 && mapx < 8 && map[mapy][mapx] > 0 ) {
- hit = 1;
- }
- }
- dist[i] = dir_x;
- io[i] = ra;
- fov[0].position = sf::Vector2f(xy, yx);
- fov[1 + i].position = sf::Vector2f(xy + dir_x * a, yx + dir_y * a);
- shape1[i].setSize(sf::Vector2f (5.f,5.f));
- shape1[i].setPosition(xy + dir_x * a, yx + dir_y * a);
- shape1[i].setFillColor(sf::Color::Green);
- rad[i] = a;
- }
- projection = 0.5f*64.f/ tan(deg_to_rad(0.5f*58.75f));
- window.clear();
- for(int i =0;i < 60;i++) {
- float shape_height = round(1000.f* projection /(rad[i] * dist[i]));
- sf::RectangleShape shape2(sf::Vector2f(16.666f, shape_height));
- shape2.setFillColor(sf::Color(0, 255 * (1 - rad[i] / 800), 0));
- shape2.setPosition(i * 16.666f,0.5f* (1000.f - shape_height));
- if(shape_height >= 900){
- std::cout << i << "\n";
- }
- window.draw(shape2);
- }
- for (int y = 0; y < mapY; y++) {
- for (int x = 0; x < mapX; x++) {
- window.draw(tile[y][x]);
- }
- }
- window.draw(fov);
- for(int i = 0;i < 60;i++) {
- window.draw(shape1[i]);
- }
- window.draw(shape);
- window.draw(cast);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement