Add stage layer asset sources

This commit is contained in:
2026-06-09 15:02:04 +08:00
parent 3e2e25eb93
commit cf7b7dfb5a
12 changed files with 140 additions and 6 deletions
+10
View File
@@ -16,10 +16,20 @@ enum class StageLayerRole {
PropsFront
};
struct StageLayerSource {
std::string texturePath;
frostbite2D::Rect sourceRect;
bool useSourceRect = false;
frostbite2D::Color tint = frostbite2D::Color(1.0f, 1.0f, 1.0f, 1.0f);
bool hasTexture() const { return !texturePath.empty(); }
};
struct StageRect {
frostbite2D::Rect bounds;
frostbite2D::Color color;
StageLayerRole layer = StageLayerRole::PropsBack;
StageLayerSource source;
};
struct SpawnPoint {
+18
View File
@@ -0,0 +1,18 @@
#pragma once
namespace ns_game::stage_assets::stage_01 {
constexpr const char* kBackgroundFar =
"assets/stage/stage_01/background/background_far.png";
constexpr const char* kBackgroundMid =
"assets/stage/stage_01/background/background_mid.png";
constexpr const char* kTileset =
"assets/stage/stage_01/tiles/stage_01_tileset.png";
constexpr const char* kProps =
"assets/stage/stage_01/props/stage_01_props.png";
constexpr const char* kHitSpark =
"assets/stage/stage_01/effects/hit_spark.png";
constexpr const char* kDust =
"assets/stage/stage_01/effects/dust.png";
} // namespace ns_game::stage_assets::stage_01
+64 -1
View File
@@ -4,6 +4,8 @@
#include <frostbite2D/graphics/camera.h>
#include <frostbite2D/graphics/renderer.h>
#include <frostbite2D/graphics/types.h>
#include <frostbite2D/graphics/texture.h>
namespace ns_game {
@@ -13,6 +15,25 @@ void drawRect(const frostbite2D::Rect& rect, const frostbite2D::Color& color) {
frostbite2D::Renderer::get().drawQuad(rect, color);
}
void drawTexture(const frostbite2D::Rect& bounds,
const frostbite2D::Rect& sourceRect,
frostbite2D::Ptr<frostbite2D::Texture> texture,
const frostbite2D::Color& tint) {
frostbite2D::Renderer& renderer = frostbite2D::Renderer::get();
frostbite2D::Quad quad = frostbite2D::Quad::createTextured(
bounds, sourceRect,
frostbite2D::Vec2(static_cast<float>(texture->getWidth()),
static_cast<float>(texture->getHeight())),
tint.r, tint.g, tint.b, tint.a, renderer.shouldShrinkSubTextureUVs());
auto* shader = renderer.getShaderManager().getShader("sprite");
if (shader) {
shader->use();
}
renderer.getBatch().submitQuad(quad, frostbite2D::Transform2D::identity(),
texture, shader,
frostbite2D::BlendMode::Normal);
}
float worldWidthOf(const LevelDefinition& level) {
return level.collision.levelRight - level.collision.levelLeft;
}
@@ -63,7 +84,7 @@ void StageRenderer::drawLayer(const LevelDefinition& level,
frostbite2D::Rect bounds(rect.bounds.left() + parallaxOffset.x,
rect.bounds.top() + parallaxOffset.y,
rect.bounds.width(), rect.bounds.height());
drawRect(bounds, rect.color);
drawStageRect(rect, bounds);
if (layer.role == StageLayerRole::LevelVisual) {
drawRect(frostbite2D::Rect(bounds.left(), bounds.top(), bounds.width(),
4.0f),
@@ -72,4 +93,46 @@ void StageRenderer::drawLayer(const LevelDefinition& level,
}
}
void StageRenderer::drawStageRect(const StageRect& rect,
const frostbite2D::Rect& bounds) const {
if (!rect.source.hasTexture()) {
drawRect(bounds, rect.color);
return;
}
frostbite2D::Ptr<frostbite2D::Texture> texture =
getTexture(rect.source.texturePath);
if (!texture) {
drawRect(bounds, rect.color);
return;
}
if (rect.source.useSourceRect) {
drawTexture(bounds, rect.source.sourceRect, texture, rect.source.tint);
return;
}
drawTexture(bounds,
frostbite2D::Rect(0.0f, 0.0f,
static_cast<float>(texture->getWidth()),
static_cast<float>(texture->getHeight())),
texture, rect.source.tint);
}
frostbite2D::Ptr<frostbite2D::Texture>
StageRenderer::getTexture(const std::string& texturePath) const {
const auto existing = textureCache_.find(texturePath);
if (existing != textureCache_.end()) {
return existing->second;
}
frostbite2D::Ptr<frostbite2D::Texture> texture =
frostbite2D::Texture::loadFromFile(texturePath);
if (texture) {
texture->setSampling(frostbite2D::TextureSampling::PixelArt);
}
textureCache_[texturePath] = texture;
return texture;
}
} // namespace ns_game
+11
View File
@@ -3,6 +3,10 @@
#include "../data/level_definition.h"
#include "../core/debug_flags.h"
#include <frostbite2D/graphics/texture.h>
#include <string>
#include <unordered_map>
namespace ns_game {
class StageRenderer {
@@ -13,6 +17,13 @@ private:
void drawBackground(const LevelDefinition& level) const;
void drawGrid(const LevelDefinition& level) const;
void drawLayer(const LevelDefinition& level, const StageLayer& layer) const;
void drawStageRect(const StageRect& rect,
const frostbite2D::Rect& bounds) const;
frostbite2D::Ptr<frostbite2D::Texture>
getTexture(const std::string& texturePath) const;
mutable std::unordered_map<std::string, frostbite2D::Ptr<frostbite2D::Texture>>
textureCache_;
};
} // namespace ns_game