Added DebugInfo

This commit is contained in:
2025-12-25 15:58:57 +08:00
parent ad83a4f70d
commit 8a78bfef14
9 changed files with 290 additions and 7 deletions

View File

@@ -43,6 +43,9 @@ set(SOURCE_FILES
src/graphics/ui/UIRenderer.cpp
src/graphics/font/BitmapFont.cpp
src/utils/ConfigLoader.cpp
src/core/DebugManager.cpp
src/ui/managers/debug/DebugOverlay.cpp
src/core/Time.cpp
)

52
src/core/DebugManager.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include "DebugManager.h"
#include "Time.h"
#include <iostream>
DebugManager::DebugManager(
const SDL_Renderer* renderer,
const SDL_Window* window,
//const InputState& inputState,
UIRenderer* uiRenderer
):
m_renderer(renderer),
m_window(window),
//m_inputState(inputState),
m_uiRenderer(uiRenderer)
{
}
DebugManager::~DebugManager() {
}
void DebugManager::initialize() {
m_debugOverlay = std::make_unique<DebugOverlay>(m_debugData);
m_debugOverlay->initialize();
m_isDebugInfoVisible = true;
}
void DebugManager::updateMousePos(int logicalX, int logicalY, const InputState& inputState) {
m_debugData.mousePosition = {
static_cast<int>(inputState.mouseCurrentPosition.first),
static_cast<int>(inputState.mouseCurrentPosition.second)
};
m_debugData.mouseLogicalPostion = {logicalX, logicalY};
}
void DebugManager::updateDebugInfo() {
m_debugData.currentFPS = static_cast<int>(Time::fps());
//std::cout << "FPS: " << m_debugData.currentFPS << std::endl;
m_debugOverlay->updateDebugInfo();
}
void DebugManager::showDebugInfo() {
//m_isDebugInfoVisible = !m_isDebugInfoVisible;
if (m_isDebugInfoVisible) {
m_uiRenderer->renderUI(m_debugOverlay->getUIRenderData());
}
}

35
src/core/DebugManager.h Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include "ui/managers/debug/DebugOverlay.h"
#include <SDL3/SDL.h>
#include "input/InputState.h"
#include "graphics/ui/UIRenderer.h"
class DebugManager {
public:
DebugManager(
const SDL_Renderer* renderer,
const SDL_Window* window,
//const InputState& inputState,
UIRenderer* uiRenderer
);
~DebugManager();
void initialize();
//void shutdown();
void showDebugInfo();
void updateMousePos(int logicalX, int logicalY, const InputState& inputState);
void updateDebugInfo();
private:
std::unique_ptr<DebugOverlay> m_debugOverlay;
DebugData m_debugData;
const SDL_Renderer* m_renderer = nullptr;
const SDL_Window* m_window = nullptr;
//const InputState& m_inputState;
bool m_isDebugInfoVisible = false;
UIRenderer* m_uiRenderer = nullptr;
};

View File

@@ -1,5 +1,6 @@
#include "GameApplication.h"
#include "utils/Tools.h"
#include "Time.h"
GameApplication::GameApplication() {
}
@@ -21,6 +22,7 @@ bool GameApplication::initialize() {
if (!ConfigLoader::load("assets/config.json", m_config)) {
SDL_Log("无法加载json");
}
Time::init();
// 输入管理
m_inputManager = std::make_unique<InputManager>();
// 窗口管理
@@ -36,7 +38,14 @@ bool GameApplication::initialize() {
m_textRenderer = std::make_unique<TextRenderer>(m_windowManager->GetRenderer(), m_fontManager.get(), m_windowManager->getViewport());
m_uiRenderer = std:: make_unique<UIRenderer>(m_windowManager->GetRenderer(), m_textRenderer.get());
// 调试管理
m_debugManager = std::make_unique<DebugManager>(
m_windowManager->GetRenderer(),
m_windowManager->GetWindow(),
//m_inputManager->GetInputState(),
m_uiRenderer.get()
);
m_debugManager->initialize();
// 场景管理,传入窗口句柄以便 SceneManager 能获取窗口尺寸
m_sceneManager = std::make_unique<SceneManager>(m_windowManager->GetRenderer(), m_uiRenderer.get(), m_windowManager->GetWindow());
if (!m_sceneManager->initialize()) {
@@ -56,8 +65,11 @@ SDL_AppResult GameApplication::handleInputEvent(SDL_Event* event) {
m_sceneManager->handleClickCurrent(pos);
}
auto pos = Tools::physicalToLogical(input.mouseCurrentPosition.first, input.mouseCurrentPosition.second, m_windowManager->getViewport());
m_debugManager->updateMousePos(pos.first, pos.second, input);
m_sceneManager->handleMousePosition(pos);
m_windowManager->setFullscreen(input.isFullscreen);
// 改变窗口时清理旧的缓存
if (event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
m_windowManager->onWindowResize();
@@ -69,16 +81,17 @@ SDL_AppResult GameApplication::handleInputEvent(SDL_Event* event) {
}
void GameApplication::run() {
Time::update();
m_sceneManager->updateCurrent();
m_debugManager->updateDebugInfo();
m_windowManager->Clear();
m_windowManager->beginWorld();
m_sceneManager->renderWorld();
m_windowManager->endWorld();
m_windowManager->beginUI();
m_sceneManager->renderUI();
m_sceneManager->renderUI();
m_debugManager->showDebugInfo();
m_windowManager->endUI();
m_windowManager->Present();

View File

@@ -8,7 +8,7 @@
#include "utils/ConfigLoader.h"
#include "graphics/font/TextRenderer.h"
#include "graphics/font/FontManager.h"
#include "core/DebugManager.h"
class GameApplication {
private:
@@ -20,7 +20,7 @@ private:
std::unique_ptr<FontManager> m_fontManager;
std::unique_ptr<TextRenderer> m_textRenderer;
std::unique_ptr<UIRenderer> m_uiRenderer;
std::unique_ptr<DebugManager> m_debugManager;
Config m_config;
public:

View File

@@ -20,6 +20,7 @@ public:
label->setRect(x, y, UI::ButtonSize * 4, UI::ButtonSize * 2);
label->setText(text, {"SourceHanSansSC-Regular.otf", UI::UI_NORMAL_FONT_SIZE, {0, 0, 0, 255}});
label->setName(name);
label->setVisible(true);
return label;
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include <string>
#include <utility>
struct DebugData {
bool showFPS = true;
bool showMousePosition = true;
std::pair<int, int> mousePosition = {0, 0};
std::pair<int, int> mouseLogicalPostion = {0, 0};
int currentFPS = 0;
};

View File

@@ -0,0 +1,128 @@
#include "DebugOverlay.h"
#include "ui/base/UIWidgetFactory.h"
#include <iostream>
DebugOverlay::DebugOverlay(DebugData& debugData)
: m_debugData(debugData) {
}
DebugOverlay::~DebugOverlay() {
}
void DebugOverlay::initialize() {
auto fpsLabel = UIWidgetFactory::createStandardLabel(
"FPS",
"FPS: 0",
0,
0
);
m_labels.emplace(fpsLabel->getNameHash(), std::move(fpsLabel));
auto mousePhyicalPos = UIWidgetFactory::createStandardLabel(
"MousePhyscialPosition",
"PhyscialPos: 0, 0",
0,
40
);
m_labels.emplace(mousePhyicalPos->getNameHash(), std::move(mousePhyicalPos));
auto mouseLogicalPos = UIWidgetFactory::createStandardLabel(
"MouseLogicalPosition",
"LogicalPos: 0, 0",
0,
80
);
m_labels.emplace(mouseLogicalPos->getNameHash(), std::move(mouseLogicalPos));
}
const UIRenderData& DebugOverlay::getUIRenderData() {
collectRenderData();
return m_uiRenderData;
}
void DebugOverlay::collectRenderData() {
// 清理上一帧的数据
m_uiRenderData.buttons.clear();
m_uiRenderData.labels.clear();
for (auto& label : m_labels) {
if(!label.second->isVisible()) {
continue;
}
m_uiRenderData.labels.push_back(label.second->getLabelDate());
//std::cout << "Collect label: " << label.second->getNameHash() << "\n";
}
}
void DebugOverlay::updateDebugInfo() {
const auto key = makeHash("FPS");
auto fpsLabelIt = m_labels.find(key);
if (fpsLabelIt != m_labels.end()) {
auto fpsLabel = fpsLabelIt->second.get();
if (m_debugData.showFPS) {
fpsLabel->setText("FPS: " + std::to_string(m_debugData.currentFPS));
fpsLabel->setVisible(true);
} else {
fpsLabel->setVisible(false);
}
} else {
std::cerr << "FPS Label not found!\n";
}
const auto mouseKey = makeHash("MousePhyscialPosition");
auto mousePosLabelIt = m_labels.find(mouseKey);
if (mousePosLabelIt != m_labels.end()) {
auto mousePosLabel = mousePosLabelIt->second.get();
if (m_debugData.showMousePosition) {
if (!mousePosLabel) {
std::cerr << "Mouse PhyscialPosition Label not found!\n";
return;
}
mousePosLabel->setText(
"PhyscialPos: " +
std::to_string(m_debugData.mousePosition.first) + ", " +
std::to_string(m_debugData.mousePosition.second)
);
mousePosLabel->setVisible(true);
} else {
mousePosLabel->setVisible(false);
}
} else {
std::cerr << "Mouse PhyscialPosition Label not found!\n";
}
const auto mouseLogicalKey = makeHash("MouseLogicalPosition");
auto mouseLogicalPosLabelIt = m_labels.find(mouseLogicalKey);
if (mouseLogicalPosLabelIt != m_labels.end()) {
auto mouseLogicalPosLabel = mouseLogicalPosLabelIt->second.get();
if (m_debugData.showMousePosition) {
if (!mouseLogicalPosLabel) {
std::cerr << "Mouse LogicalPosition Label not found!\n";
return;
}
mouseLogicalPosLabel->setText(
"LogicalPos: " +
std::to_string(m_debugData.mouseLogicalPostion.first) + ", " +
std::to_string(m_debugData.mouseLogicalPostion.second)
);
mouseLogicalPosLabel->setVisible(true);
} else {
mouseLogicalPosLabel->setVisible(false);
}
} else {
std::cerr << "Mouse LogicalPosition Label not found!\n";
}
}

View File

@@ -0,0 +1,38 @@
#pragma once
#include "ui/components/Label.h"
#include "DebugData.h"
#include <memory>
#include <unordered_map>
class DebugOverlay {
public:
DebugOverlay(DebugData& debugData);
~DebugOverlay();
void initialize();
//void shutdown();
void collectRenderData();
const UIRenderData& getUIRenderData();
void updateDebugInfo();
// 应该添加:
DebugOverlay(const DebugOverlay&) = delete;
DebugOverlay& operator=(const DebugOverlay&) = delete;
DebugOverlay(DebugOverlay&&) = delete;
DebugOverlay& operator=(DebugOverlay&&) = delete;
private:
DebugData& m_debugData;
std::unordered_map<int, std::unique_ptr<Label>> m_labels;
UIRenderData m_uiRenderData;
size_t makeHash(const std::string& name) {
return std::hash<std::string>{}(name);
}
};