refactor(渲染): 统一处理锚点计算逻辑

将锚点计算逻辑统一交由 RenderBackend 处理,简化各渲染组件的代码
添加窗口高级配置选项,支持自定义光标和DPI缩放
更新窗口初始化逻辑,使用配置中的宽高参数
This commit is contained in:
ChestnutYueyue 2026-02-12 14:29:50 +08:00
parent f1ec3f8eae
commit 5bab53aa2a
6 changed files with 20 additions and 19 deletions

View File

@ -39,6 +39,9 @@ struct AppConfig {
BackendType renderBackend = BackendType::OpenGL;
int msaaSamples = 0;
PlatformType platform = PlatformType::Auto;
// 窗口高级配置
bool enableCursors = true; // 是否启用光标
bool enableDpiScale = false; // 是否启用DPI缩放
};
// ============================================================================

View File

@ -155,9 +155,9 @@ void FrameRenderer::drawSpriteFrame(RenderBackend &renderer,
float flipScaleX = flipX ? -1.0f : 1.0f;
float flipScaleY = flipY ? -1.0f : 1.0f;
Rect destRect{
{finalPos.x - w * 0.5f * flipScaleX, finalPos.y - h * 0.5f * flipScaleY},
{w, h}};
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
// 使用中心锚点(0.5, 0.5),所以 destRect 从 finalPos 开始,不预偏移
Rect destRect{{finalPos.x, finalPos.y}, {w * flipScaleX, h * flipScaleY}};
Color finalColor{tint.r, tint.g, tint.b, tint.a * opacity};

View File

@ -93,20 +93,20 @@ bool Application::init(const AppConfig &config) {
window_ = makeUnique<Window>();
WindowConfig winConfig;
winConfig.title = config.title;
winConfig.width = 1280;
winConfig.height = 720;
winConfig.width = config.width;
winConfig.height = config.height;
if (platform == PlatformType::Switch) {
winConfig.fullscreen = true;
winConfig.fullscreenDesktop = false; // Switch 使用固定分辨率全屏
winConfig.resizable = false;
winConfig.enableCursors = false;
winConfig.enableDpiScale = false;
winConfig.enableCursors = config.enableCursors;
winConfig.enableDpiScale = config.enableDpiScale;
} else {
// PC 平台默认窗口模式
winConfig.fullscreen = config.fullscreen;
winConfig.resizable = true;
winConfig.enableCursors = true;
winConfig.enableDpiScale = true;
winConfig.resizable = config.resizable;
winConfig.enableCursors = config.enableCursors;
winConfig.enableDpiScale = config.enableDpiScale;
}
winConfig.vsync = config.vsync;
winConfig.msaaSamples = config.msaaSamples;

View File

@ -121,9 +121,8 @@ void ParticleEmitter::render(RenderBackend &renderer) {
continue;
// 计算目标矩形
float halfSize = p.size * 0.5f;
Rect destRect(p.position.x - halfSize, p.position.y - halfSize, p.size,
p.size);
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
Rect destRect(p.position.x, p.position.y, p.size, p.size);
renderer.drawSprite(
*config_.texture, destRect,

View File

@ -79,9 +79,8 @@ void Sprite::onDraw(RenderBackend &renderer) {
auto pos = getPosition();
auto anchor = getAnchor();
auto scale = getScale();
Rect destRect(pos.x - width * anchor.x * scale.x,
pos.y - height * anchor.y * scale.y, width * scale.x,
height * scale.y);
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
Rect destRect(pos.x, pos.y, width * scale.x, height * scale.y);
// Adjust source rect for flipping
Rect srcRect = textureRect_;
@ -111,9 +110,8 @@ void Sprite::generateRenderCommand(std::vector<RenderCommand> &commands,
auto pos = getPosition();
auto anchor = getAnchor();
auto scale = getScale();
Rect destRect(pos.x - width * anchor.x * scale.x,
pos.y - height * anchor.y * scale.y, width * scale.x,
height * scale.y);
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
Rect destRect(pos.x, pos.y, width * scale.x, height * scale.y);
// 调整源矩形(翻转)
Rect srcRect = textureRect_;

View File

@ -91,6 +91,7 @@ if is_config("examples","true") then
includes("examples/spatial_index_demo", {rootdir = "examples/spatial_index_demo"})
includes("examples/collision_demo", {rootdir = "examples/collision_demo"})
includes("examples/push_box", {rootdir = "examples/push_box"})
-- includes("examples/flappy_bird", {rootdir = "examples/flappy_bird"})
end
-- ==============================================