feat: batched debug lines draws

This commit is contained in:
Florian Sylvain
2025-05-11 01:41:19 +02:00
parent f276d243ec
commit 9b65954776
4 changed files with 34 additions and 18 deletions
+15 -10
View File
@@ -10,26 +10,26 @@ float clamp(float value, float minVal, float maxVal) {
return std::max(minVal, std::min(value, maxVal)); return std::max(minVal, std::min(value, maxVal));
} }
void DebugDraw::drawLine(sf::RenderWindow& window, const sf::Vector2f& start, void DebugDraw::addLine(sf::VertexArray& lines, const sf::Vector2f& start,
const sf::Vector2f& direction, float length, const sf::Vector2f& direction, float length,
const sf::Color& color) { const sf::Color& color) {
sf::Vector2f endPoint = start + VectorMath::normalize(direction) * length; sf::Vector2f endPoint = start + VectorMath::normalize(direction) * length;
sf::Vertex line[] = {{start, color}, {endPoint, color}}; lines.append(sf::Vertex({start, color}));
lines.append(sf::Vertex({endPoint, color}));
window.draw(line, 2, sf::PrimitiveType::Lines);
} }
void DebugDraw::drawDirectionLine(sf::RenderWindow& window, const Ball* ball) { void DebugDraw::addDirectionLine(sf::VertexArray& lines, const Ball* ball,
const sf::RenderWindow& window) {
sf::Vector2f ballCenter = ball->getPosition(); sf::Vector2f ballCenter = ball->getPosition();
sf::Vector2i mousePixel = sf::Mouse::getPosition(window); sf::Vector2i mousePixel = sf::Mouse::getPosition(window);
sf::Vector2f mouseWorld(static_cast<float>(mousePixel.x), sf::Vector2f mouseWorld(static_cast<float>(mousePixel.x),
static_cast<float>(mousePixel.y)); static_cast<float>(mousePixel.y));
sf::Vector2f dir = mouseWorld - ballCenter; sf::Vector2f dir = mouseWorld - ballCenter;
drawLine(window, ballCenter, dir, 100.f, sf::Color::Green); addLine(lines, ballCenter, dir, 100.f, sf::Color::Green);
} }
void DebugDraw::drawVelocityLine(sf::RenderWindow& window, const Ball* ball) { void DebugDraw::addVelocityLine(sf::VertexArray& lines, const Ball* ball) {
if (ball->isAtRest()) return; if (ball->isAtRest()) return;
sf::Vector2f ballCenter = ball->getPosition(); sf::Vector2f ballCenter = ball->getPosition();
@@ -37,5 +37,10 @@ void DebugDraw::drawVelocityLine(sf::RenderWindow& window, const Ball* ball) {
float velLength = VectorMath::length(velocity); float velLength = VectorMath::length(velocity);
float clampedLength = std::max(0.f, std::min(velLength, 100.f)); float clampedLength = std::max(0.f, std::min(velLength, 100.f));
drawLine(window, ballCenter, velocity, clampedLength, sf::Color::Blue); addLine(lines, ballCenter, velocity, clampedLength, sf::Color::Blue);
}
void DebugDraw::drawBatchedLines(sf::RenderWindow& window,
const sf::VertexArray& lines) {
if (lines.getVertexCount() > 0) window.draw(lines);
} }
+9 -5
View File
@@ -1,14 +1,18 @@
#pragma once #pragma once
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include "Ball.hpp" #include "Ball.hpp"
class DebugDraw { class DebugDraw {
public: public:
static void drawLine(sf::RenderWindow& window, const sf::Vector2f& start, static void addLine(sf::VertexArray& lines, const sf::Vector2f& start,
const sf::Vector2f& direction, float length, const sf::Vector2f& direction, float length,
const sf::Color& color); const sf::Color& color);
static void drawDirectionLine(sf::RenderWindow& window, const Ball* ball); static void addDirectionLine(sf::VertexArray& lines, const Ball* ball,
static void drawVelocityLine(sf::RenderWindow& window, const Ball* ball); const sf::RenderWindow& window);
static void addVelocityLine(sf::VertexArray& lines, const Ball* ball);
static void drawBatchedLines(sf::RenderWindow& window,
const sf::VertexArray& lines);
}; };
+8 -3
View File
@@ -11,7 +11,8 @@
Game::Game() Game::Game()
: m_inputManager( : m_inputManager(
std::bind(&Game::processKeyPressed, this, std::placeholders::_1), std::bind(&Game::processKeyPressed, this, std::placeholders::_1),
std::bind(&Game::processMousePressed, this, std::placeholders::_1)) { std::bind(&Game::processMousePressed, this, std::placeholders::_1)),
m_debugLines(sf::PrimitiveType::Lines) {
m_window.create(sf::VideoMode({Constants::WIDTH, Constants::HEIGHT}), m_window.create(sf::VideoMode({Constants::WIDTH, Constants::HEIGHT}),
"SFML Playground"); "SFML Playground");
m_window.setVerticalSyncEnabled(true); m_window.setVerticalSyncEnabled(true);
@@ -58,16 +59,20 @@ void Game::update() {
void Game::render() { void Game::render() {
m_window.clear(sf::Color::Black); m_window.clear(sf::Color::Black);
m_debugLines.clear();
for (const auto &object : m_objects) { for (const auto &object : m_objects) {
object->draw(m_window); object->draw(m_window);
auto *ball = dynamic_cast<Ball *>(object.get()); auto *ball = dynamic_cast<Ball *>(object.get());
if (!ball) continue; if (!ball) continue;
DebugDraw::drawDirectionLine(m_window, ball); DebugDraw::addDirectionLine(m_debugLines, ball, m_window);
DebugDraw::drawVelocityLine(m_window, ball); DebugDraw::addVelocityLine(m_debugLines, ball);
} }
DebugDraw::drawBatchedLines(m_window, m_debugLines);
m_window.display(); m_window.display();
} }
+2
View File
@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include <SFML/System/Clock.hpp> #include <SFML/System/Clock.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
@@ -20,6 +21,7 @@ class Game {
std::vector<std::unique_ptr<PhysicalObject>> m_objects; std::vector<std::unique_ptr<PhysicalObject>> m_objects;
sf::Clock m_clock; sf::Clock m_clock;
InputManager m_inputManager; InputManager m_inputManager;
sf::VertexArray m_debugLines;
void processKeyPressed(const sf::Event::KeyPressed& keyPressed); void processKeyPressed(const sf::Event::KeyPressed& keyPressed);
void processMousePressed(const sf::Event::MouseButtonPressed& mousePressed); void processMousePressed(const sf::Event::MouseButtonPressed& mousePressed);
void handleMouseClick(const sf::Vector2i& mousePos); void handleMouseClick(const sf::Vector2i& mousePos);