Split stage rendering and debug overlay

This commit is contained in:
2026-06-09 00:39:15 +08:00
parent ce815ed58b
commit cc75b94c44
9 changed files with 199 additions and 47 deletions
+11
View File
@@ -0,0 +1,11 @@
#pragma once
namespace ns_game {
struct DebugFlags {
bool showStageGrid = true;
bool showCollision = true;
bool showActorBounds = true;
};
} // namespace ns_game
+49
View File
@@ -0,0 +1,49 @@
#include "debug_overlay.h"
#include <frostbite2D/graphics/renderer.h>
namespace ns_game {
namespace {
void drawRect(const frostbite2D::Rect& rect, const frostbite2D::Color& color) {
frostbite2D::Renderer::get().drawQuad(rect, color);
}
void drawOutline(const frostbite2D::Rect& rect, const frostbite2D::Color& color,
float thickness = 2.0f) {
drawRect(frostbite2D::Rect(rect.left(), rect.top(), rect.width(), thickness),
color);
drawRect(frostbite2D::Rect(rect.left(), rect.bottom() - thickness,
rect.width(), thickness),
color);
drawRect(frostbite2D::Rect(rect.left(), rect.top(), thickness, rect.height()),
color);
drawRect(frostbite2D::Rect(rect.right() - thickness, rect.top(), thickness,
rect.height()),
color);
}
} // namespace
void DebugOverlay::Render(const LevelDefinition& level, const DebugFlags& flags,
const frostbite2D::Rect* playerBounds) const {
if (flags.showCollision) {
drawCollision(level);
}
if (flags.showActorBounds && playerBounds) {
drawActorBounds(*playerBounds);
}
}
void DebugOverlay::drawCollision(const LevelDefinition& level) const {
for (const auto& platform : level.collision.platforms) {
drawOutline(platform, frostbite2D::Color(0.1f, 0.95f, 0.45f, 0.85f));
}
}
void DebugOverlay::drawActorBounds(const frostbite2D::Rect& bounds) const {
drawOutline(bounds, frostbite2D::Color(1.0f, 0.78f, 0.1f, 0.9f));
}
} // namespace ns_game
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include "../core/debug_flags.h"
#include "../data/level_definition.h"
#include <frostbite2D/types/type_math.h>
namespace ns_game {
class DebugOverlay {
public:
void Render(const LevelDefinition& level, const DebugFlags& flags,
const frostbite2D::Rect* playerBounds) const;
private:
void drawCollision(const LevelDefinition& level) const;
void drawActorBounds(const frostbite2D::Rect& bounds) const;
};
} // namespace ns_game
+4 -36
View File
@@ -9,14 +9,6 @@
namespace ns_game {
namespace {
void drawRect(const frostbite2D::Rect& rect, const frostbite2D::Color& color) {
frostbite2D::Renderer::get().drawQuad(rect, color);
}
} // namespace
void WhiteboxScene::onEnter() {
frostbite2D::Scene::onEnter();
level_ = CreateWhiteboxLevel();
@@ -32,35 +24,11 @@ void WhiteboxScene::OnUpdate(float deltaTime) {
}
void WhiteboxScene::Render() {
drawWhitebox();
stageRenderer_.Render(level_, debugFlags_);
frostbite2D::Scene::Render();
}
void WhiteboxScene::drawWhitebox() const {
const float worldWidth = level_.collision.levelRight - level_.collision.levelLeft;
drawRect(frostbite2D::Rect(level_.collision.levelLeft, 0.0f, worldWidth,
config::kVirtualHeight),
frostbite2D::Color(0.10f, 0.12f, 0.14f, 1.0f));
for (float x = level_.collision.levelLeft; x <= level_.collision.levelRight;
x += 80.0f) {
drawRect(frostbite2D::Rect(x, 0.0f, 1.0f, config::kVirtualHeight),
frostbite2D::Color(0.20f, 0.22f, 0.25f, 1.0f));
}
for (float y = 0.0f; y <= config::kVirtualHeight; y += 80.0f) {
drawRect(frostbite2D::Rect(level_.collision.levelLeft, y, worldWidth, 1.0f),
frostbite2D::Color(0.20f, 0.22f, 0.25f, 1.0f));
}
for (const auto& prop : level_.props) {
drawRect(prop.bounds, prop.color);
}
for (const auto& platform : level_.collision.platforms) {
drawRect(platform, frostbite2D::Color(0.62f, 0.68f, 0.73f, 1.0f));
drawRect(frostbite2D::Rect(platform.left(), platform.top(), platform.width(), 4.0f),
frostbite2D::Color(0.91f, 0.95f, 0.98f, 1.0f));
}
const frostbite2D::Rect playerBounds =
player_ ? player_->GetWorldBounds() : frostbite2D::Rect::Zero();
debugOverlay_.Render(level_, debugFlags_, player_ ? &playerBounds : nullptr);
}
void WhiteboxScene::updateCamera() {
+6 -1
View File
@@ -1,7 +1,10 @@
#pragma once
#include "../actor/player_actor.h"
#include "../core/debug_flags.h"
#include "../data/level_definition.h"
#include "../debug/debug_overlay.h"
#include "../stage/stage_renderer.h"
#include <frostbite2D/scene/scene.h>
#include <frostbite2D/types/type_math.h>
@@ -15,10 +18,12 @@ public:
void Render() override;
private:
void drawWhitebox() const;
void updateCamera();
LevelDefinition level_;
DebugFlags debugFlags_;
StageRenderer stageRenderer_;
DebugOverlay debugOverlay_;
frostbite2D::Ptr<PlayerActor> player_;
};
+65
View File
@@ -0,0 +1,65 @@
#include "stage_renderer.h"
#include "../core/game_config.h"
#include <frostbite2D/graphics/renderer.h>
namespace ns_game {
namespace {
void drawRect(const frostbite2D::Rect& rect, const frostbite2D::Color& color) {
frostbite2D::Renderer::get().drawQuad(rect, color);
}
float worldWidthOf(const LevelDefinition& level) {
return level.collision.levelRight - level.collision.levelLeft;
}
} // namespace
void StageRenderer::Render(const LevelDefinition& level,
const DebugFlags& debugFlags) const {
drawBackground(level);
if (debugFlags.showStageGrid) {
drawGrid(level);
}
drawProps(level);
drawPlatforms(level);
}
void StageRenderer::drawBackground(const LevelDefinition& level) const {
drawRect(frostbite2D::Rect(level.collision.levelLeft, 0.0f,
worldWidthOf(level), config::kVirtualHeight),
frostbite2D::Color(0.10f, 0.12f, 0.14f, 1.0f));
}
void StageRenderer::drawGrid(const LevelDefinition& level) const {
const float worldWidth = worldWidthOf(level);
for (float x = level.collision.levelLeft; x <= level.collision.levelRight;
x += 80.0f) {
drawRect(frostbite2D::Rect(x, 0.0f, 1.0f, config::kVirtualHeight),
frostbite2D::Color(0.20f, 0.22f, 0.25f, 1.0f));
}
for (float y = 0.0f; y <= config::kVirtualHeight; y += 80.0f) {
drawRect(frostbite2D::Rect(level.collision.levelLeft, y, worldWidth, 1.0f),
frostbite2D::Color(0.20f, 0.22f, 0.25f, 1.0f));
}
}
void StageRenderer::drawProps(const LevelDefinition& level) const {
for (const auto& prop : level.props) {
drawRect(prop.bounds, prop.color);
}
}
void StageRenderer::drawPlatforms(const LevelDefinition& level) const {
for (const auto& platform : level.collision.platforms) {
drawRect(platform, frostbite2D::Color(0.62f, 0.68f, 0.73f, 1.0f));
drawRect(frostbite2D::Rect(platform.left(), platform.top(),
platform.width(), 4.0f),
frostbite2D::Color(0.91f, 0.95f, 0.98f, 1.0f));
}
}
} // namespace ns_game
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include "../data/level_definition.h"
#include "../core/debug_flags.h"
namespace ns_game {
class StageRenderer {
public:
void Render(const LevelDefinition& level, const DebugFlags& debugFlags) const;
private:
void drawBackground(const LevelDefinition& level) const;
void drawGrid(const LevelDefinition& level) const;
void drawProps(const LevelDefinition& level) const;
void drawPlatforms(const LevelDefinition& level) const;
};
} // namespace ns_game