From 9b6595477616a8904ade43ff472f9c18fafb3555 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Sun, 11 May 2025 01:41:19 +0200 Subject: [PATCH] feat: batched debug lines draws --- src/DebugDraw.cpp | 25 +++++++++++++++---------- src/DebugDraw.hpp | 14 +++++++++----- src/Game.cpp | 11 ++++++++--- src/Game.hpp | 2 ++ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/DebugDraw.cpp b/src/DebugDraw.cpp index 4366af7..75108cd 100644 --- a/src/DebugDraw.cpp +++ b/src/DebugDraw.cpp @@ -10,26 +10,26 @@ float clamp(float value, float minVal, float maxVal) { return std::max(minVal, std::min(value, maxVal)); } -void DebugDraw::drawLine(sf::RenderWindow& window, const sf::Vector2f& start, - const sf::Vector2f& direction, float length, - const sf::Color& color) { +void DebugDraw::addLine(sf::VertexArray& lines, const sf::Vector2f& start, + const sf::Vector2f& direction, float length, + const sf::Color& color) { sf::Vector2f endPoint = start + VectorMath::normalize(direction) * length; - sf::Vertex line[] = {{start, color}, {endPoint, color}}; - - window.draw(line, 2, sf::PrimitiveType::Lines); + lines.append(sf::Vertex({start, color})); + lines.append(sf::Vertex({endPoint, color})); } -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::Vector2i mousePixel = sf::Mouse::getPosition(window); sf::Vector2f mouseWorld(static_cast(mousePixel.x), static_cast(mousePixel.y)); 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; sf::Vector2f ballCenter = ball->getPosition(); @@ -37,5 +37,10 @@ void DebugDraw::drawVelocityLine(sf::RenderWindow& window, const Ball* ball) { float velLength = VectorMath::length(velocity); 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); } diff --git a/src/DebugDraw.hpp b/src/DebugDraw.hpp index a24f098..4c313c1 100644 --- a/src/DebugDraw.hpp +++ b/src/DebugDraw.hpp @@ -1,14 +1,18 @@ #pragma once #include #include +#include #include "Ball.hpp" class DebugDraw { public: - static void drawLine(sf::RenderWindow& window, const sf::Vector2f& start, - const sf::Vector2f& direction, float length, - const sf::Color& color); - static void drawDirectionLine(sf::RenderWindow& window, const Ball* ball); - static void drawVelocityLine(sf::RenderWindow& window, const Ball* ball); + static void addLine(sf::VertexArray& lines, const sf::Vector2f& start, + const sf::Vector2f& direction, float length, + const sf::Color& color); + static void addDirectionLine(sf::VertexArray& lines, 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); }; diff --git a/src/Game.cpp b/src/Game.cpp index 2013520..8fe1b8f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -11,7 +11,8 @@ Game::Game() : m_inputManager( 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}), "SFML Playground"); m_window.setVerticalSyncEnabled(true); @@ -58,16 +59,20 @@ void Game::update() { void Game::render() { m_window.clear(sf::Color::Black); + m_debugLines.clear(); + for (const auto &object : m_objects) { object->draw(m_window); auto *ball = dynamic_cast(object.get()); if (!ball) continue; - DebugDraw::drawDirectionLine(m_window, ball); - DebugDraw::drawVelocityLine(m_window, ball); + DebugDraw::addDirectionLine(m_debugLines, ball, m_window); + DebugDraw::addVelocityLine(m_debugLines, ball); } + DebugDraw::drawBatchedLines(m_window, m_debugLines); + m_window.display(); } diff --git a/src/Game.hpp b/src/Game.hpp index 68d9c4e..2a84a90 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -20,6 +21,7 @@ class Game { std::vector> m_objects; sf::Clock m_clock; InputManager m_inputManager; + sf::VertexArray m_debugLines; void processKeyPressed(const sf::Event::KeyPressed& keyPressed); void processMousePressed(const sf::Event::MouseButtonPressed& mousePressed); void handleMouseClick(const sf::Vector2i& mousePos);