mirror of
https://github.com/Floriansylvain/SFMLplayground.git
synced 2026-08-19 11:43:24 +02:00
feat: DebugOverlay for rendering debug information
This commit is contained in:
@@ -18,3 +18,9 @@ file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS src/*.hpp)
|
|||||||
add_executable(SFMLplayground ${SOURCES} ${HEADERS})
|
add_executable(SFMLplayground ${SOURCES} ${HEADERS})
|
||||||
target_compile_features(SFMLplayground PRIVATE cxx_std_17)
|
target_compile_features(SFMLplayground PRIVATE cxx_std_17)
|
||||||
target_link_libraries(SFMLplayground PRIVATE SFML::Graphics)
|
target_link_libraries(SFMLplayground PRIVATE SFML::Graphics)
|
||||||
|
|
||||||
|
add_custom_command(
|
||||||
|
TARGET SFMLplayground POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||||
|
${CMAKE_SOURCE_DIR}/src/assets $<TARGET_FILE_DIR:SFMLplayground>/assets
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include "DebugOverlay.hpp"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
DebugOverlay::DebugOverlay(const std::string& fontPath)
|
||||||
|
: m_font(fontPath), m_text(m_font) {
|
||||||
|
m_text.setCharacterSize(16);
|
||||||
|
m_text.setFillColor(sf::Color::White);
|
||||||
|
m_text.setPosition(sf::Vector2f(5.f, 5.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
void DebugOverlay::update(int drawCalls, float timescale,
|
||||||
|
const sf::RenderWindow& window) {
|
||||||
|
float elapsed = m_fpsClock.restart().asSeconds();
|
||||||
|
if (elapsed > 0.f) m_fps = static_cast<int>(1.f / elapsed);
|
||||||
|
|
||||||
|
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "Draw calls: " << drawCalls << "\n";
|
||||||
|
oss << "Framerate: " << m_fps << " FPS\n";
|
||||||
|
oss << "Frametime: " << (elapsed * 1000.f) << " ms\n";
|
||||||
|
oss << "Mouse: " << mousePos.x << ", " << mousePos.y << "\n";
|
||||||
|
oss << "Time scale: " << timescale << "\n";
|
||||||
|
|
||||||
|
m_text.setString(oss.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void DebugOverlay::draw(sf::RenderWindow& window) { window.draw(m_text); }
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
#include <SFML/System/Clock.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class DebugOverlay {
|
||||||
|
public:
|
||||||
|
DebugOverlay(const std::string& fontPath);
|
||||||
|
|
||||||
|
void update(int drawCalls, float timescale, const sf::RenderWindow& window);
|
||||||
|
void draw(sf::RenderWindow& window);
|
||||||
|
|
||||||
|
private:
|
||||||
|
sf::Font m_font;
|
||||||
|
sf::Text m_text;
|
||||||
|
sf::Clock m_fpsClock;
|
||||||
|
float m_fps = 0.f;
|
||||||
|
};
|
||||||
+13
-4
@@ -12,7 +12,9 @@ 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_debugLines(sf::PrimitiveType::Lines),
|
||||||
|
m_drawCallCount(0),
|
||||||
|
m_debugOverlay("assets/consolas.ttf") {
|
||||||
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);
|
||||||
@@ -27,10 +29,11 @@ Game::Game()
|
|||||||
|
|
||||||
void Game::processKeyPressed(const sf::Event::KeyPressed &kP) {
|
void Game::processKeyPressed(const sf::Event::KeyPressed &kP) {
|
||||||
if (kP.code == sf::Keyboard::Key::Equal) {
|
if (kP.code == sf::Keyboard::Key::Equal) {
|
||||||
m_timeScale *= 1.1f;
|
m_timeScale += 0.25f;
|
||||||
|
if (m_timeScale > 10.0f) m_timeScale = 10.0f;
|
||||||
} else if (kP.code == sf::Keyboard::Key::Hyphen) {
|
} else if (kP.code == sf::Keyboard::Key::Hyphen) {
|
||||||
m_timeScale /= 1.1f;
|
m_timeScale -= 0.25f;
|
||||||
if (m_timeScale < 0.1f) m_timeScale = 0.1f;
|
if (m_timeScale < 0.25f) m_timeScale = 0.25f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,9 +63,11 @@ void Game::render() {
|
|||||||
m_window.clear(sf::Color::Black);
|
m_window.clear(sf::Color::Black);
|
||||||
|
|
||||||
m_debugLines.clear();
|
m_debugLines.clear();
|
||||||
|
m_drawCallCount = 0;
|
||||||
|
|
||||||
for (const auto &object : m_objects) {
|
for (const auto &object : m_objects) {
|
||||||
object->draw(m_window);
|
object->draw(m_window);
|
||||||
|
m_drawCallCount++;
|
||||||
|
|
||||||
auto *ball = dynamic_cast<Ball *>(object.get());
|
auto *ball = dynamic_cast<Ball *>(object.get());
|
||||||
if (!ball) continue;
|
if (!ball) continue;
|
||||||
@@ -72,6 +77,10 @@ void Game::render() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DebugDraw::drawBatchedLines(m_window, m_debugLines);
|
DebugDraw::drawBatchedLines(m_window, m_debugLines);
|
||||||
|
m_drawCallCount++;
|
||||||
|
|
||||||
|
m_debugOverlay.update(m_drawCallCount, m_timeScale, m_window);
|
||||||
|
m_debugOverlay.draw(m_window);
|
||||||
|
|
||||||
m_window.display();
|
m_window.display();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
#include <SFML/Graphics/VertexArray.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>
|
||||||
@@ -8,6 +10,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Ball.hpp"
|
#include "Ball.hpp"
|
||||||
|
#include "DebugOverlay.hpp"
|
||||||
#include "InputManager.hpp"
|
#include "InputManager.hpp"
|
||||||
#include "PhysicalObject.hpp"
|
#include "PhysicalObject.hpp"
|
||||||
|
|
||||||
@@ -22,6 +25,8 @@ class Game {
|
|||||||
sf::Clock m_clock;
|
sf::Clock m_clock;
|
||||||
InputManager m_inputManager;
|
InputManager m_inputManager;
|
||||||
sf::VertexArray m_debugLines;
|
sf::VertexArray m_debugLines;
|
||||||
|
int m_drawCallCount = 0;
|
||||||
|
DebugOverlay m_debugOverlay;
|
||||||
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);
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user