From 62655038f356137a78887aafd4f8bc0bace4aace Mon Sep 17 00:00:00 2001 From: Floriansylvain Date: Sun, 28 Dec 2025 04:18:53 +0100 Subject: [PATCH] feat: walls for future ray cast --- src/Game.cpp | 12 ++++++++++++ src/Game.hpp | 2 ++ src/GridLines.cpp | 12 ++++++------ src/GridLines.hpp | 2 +- src/Walls.cpp | 14 ++++++++++++++ src/Walls.hpp | 29 +++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/Walls.cpp create mode 100644 src/Walls.hpp diff --git a/src/Game.cpp b/src/Game.cpp index fbe9089..e1c24bc 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -10,6 +10,17 @@ Game::Game() : gridLines(WIDTH, HEIGHT, 20.f), player(15.f, sf::Color::Green) { worldView.setSize({(float)WIDTH, (float)HEIGHT}); worldView.setCenter({(float)WIDTH / 2.f, (float)HEIGHT / 2.f}); + + walls.add({1.f, 1.f}, {WIDTH - 1.f, 1.f}); + walls.add({1.f, 1.f}, {1.f, HEIGHT - 1.f}); + walls.add({WIDTH - 1.f, 1.f}, {WIDTH - 1.f, HEIGHT - 1.f}); + walls.add({1.f, HEIGHT - 1.f}, {WIDTH - 1.f, HEIGHT - 1.f}); + + walls.add({1000.f, 500.f}, {1500.f, 500.f}); + walls.add({500.f, 240.f}, {500.f, 760.f}); + walls.add({420.f, 240.f}, {420.f, 760.f}); + walls.add({420.f, 240.f}, {500.f, 240.f}); + walls.add({420.f, 760.f}, {500.f, 760.f}); } void Game::run() { @@ -64,6 +75,7 @@ void Game::render() { gridLines.render(window); debug.render(window); player.render(window); + walls.render(window); window.display(); }; diff --git a/src/Game.hpp b/src/Game.hpp index 9c47155..a22be1e 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -6,6 +6,7 @@ #include "DebugUI.hpp" #include "GridLines.hpp" #include "Player.hpp" +#include "Walls.hpp" constexpr bool VSYNC = true; constexpr int WIDTH = 1920; @@ -25,6 +26,7 @@ private: Player player; DebugUI debug; GridLines gridLines; + Walls walls; void onResize(); void processEvents(); diff --git a/src/GridLines.cpp b/src/GridLines.cpp index 67d00b0..60f5e09 100644 --- a/src/GridLines.cpp +++ b/src/GridLines.cpp @@ -2,18 +2,18 @@ #include GridLines::GridLines(float width, float height, float spacing) - : m_lines(sf::PrimitiveType::Lines) { + : lines(sf::PrimitiveType::Lines) { sf::Color gray(64, 64, 64); for (float x = 0; x <= width; x += spacing) { - m_lines.append({{x, 0.f}, gray}); - m_lines.append({{x, height}, gray}); + lines.append({{x, 0.f}, gray}); + lines.append({{x, height}, gray}); } for (float y = 0; y <= height; y += spacing) { - m_lines.append({{0.f, y}, gray}); - m_lines.append({{width, y}, gray}); + lines.append({{0.f, y}, gray}); + lines.append({{width, y}, gray}); } } -void GridLines::render(sf::RenderWindow &window) const { window.draw(m_lines); } +void GridLines::render(sf::RenderWindow &window) const { window.draw(lines); } diff --git a/src/GridLines.hpp b/src/GridLines.hpp index 255f3d5..7c61eb1 100644 --- a/src/GridLines.hpp +++ b/src/GridLines.hpp @@ -11,7 +11,7 @@ public: void render(sf::RenderWindow &window) const; private: - sf::VertexArray m_lines; + sf::VertexArray lines; }; #endif // GRIDLINES_HPP diff --git a/src/Walls.cpp b/src/Walls.cpp new file mode 100644 index 0000000..23cb9ad --- /dev/null +++ b/src/Walls.cpp @@ -0,0 +1,14 @@ +#include "Walls.hpp" +#include + +Walls::Walls() : walls(sf::PrimitiveType::Lines) {}; + +void Walls::add(sf::Vector2f pointA, sf::Vector2f pointB) { + walls.append({pointA, sf::Color::Red}); + walls.append({pointB, sf::Color::Red}); + vertices.emplace_back(Wall{pointA, pointB}); +}; + +std::vector Walls::get() { return vertices; }; + +void Walls::render(sf::RenderWindow &window) { window.draw(walls); }; diff --git a/src/Walls.hpp b/src/Walls.hpp new file mode 100644 index 0000000..19fb0ee --- /dev/null +++ b/src/Walls.hpp @@ -0,0 +1,29 @@ +#ifndef WALLS_HPP +#define WALLS_HPP + +#include +#include +#include +#include +#include + +struct Wall { + sf::Vector2f start; + sf::Vector2f end; +}; + +class Walls { +public: + Walls(); + + void add(sf::Vector2f pointA, sf::Vector2f pointB); + std::vector get(); + + void render(sf::RenderWindow &window); + +private: + sf::VertexArray walls; + std::vector vertices; +}; + +#endif // WALLS_HPP