From 5bab53aa2a694be2424060f6fa5b32176782cc2c Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Thu, 12 Feb 2026 14:29:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B8=B2=E6=9F=93):=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E5=A4=84=E7=90=86=E9=94=9A=E7=82=B9=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将锚点计算逻辑统一交由 RenderBackend 处理,简化各渲染组件的代码 添加窗口高级配置选项,支持自定义光标和DPI缩放 更新窗口初始化逻辑,使用配置中的宽高参数 --- Extra2D/include/extra2d/app/application.h | 3 +++ Extra2D/src/animation/frame_renderer.cpp | 6 +++--- Extra2D/src/app/application.cpp | 14 +++++++------- Extra2D/src/effects/particle_system.cpp | 5 ++--- Extra2D/src/scene/sprite.cpp | 10 ++++------ xmake.lua | 1 + 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Extra2D/include/extra2d/app/application.h b/Extra2D/include/extra2d/app/application.h index f5138e1..ac23673 100644 --- a/Extra2D/include/extra2d/app/application.h +++ b/Extra2D/include/extra2d/app/application.h @@ -39,6 +39,9 @@ struct AppConfig { BackendType renderBackend = BackendType::OpenGL; int msaaSamples = 0; PlatformType platform = PlatformType::Auto; + // 窗口高级配置 + bool enableCursors = true; // 是否启用光标 + bool enableDpiScale = false; // 是否启用DPI缩放 }; // ============================================================================ diff --git a/Extra2D/src/animation/frame_renderer.cpp b/Extra2D/src/animation/frame_renderer.cpp index d9d25a4..5e4ce2d 100644 --- a/Extra2D/src/animation/frame_renderer.cpp +++ b/Extra2D/src/animation/frame_renderer.cpp @@ -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}; diff --git a/Extra2D/src/app/application.cpp b/Extra2D/src/app/application.cpp index ac77df3..304c4f6 100644 --- a/Extra2D/src/app/application.cpp +++ b/Extra2D/src/app/application.cpp @@ -93,20 +93,20 @@ bool Application::init(const AppConfig &config) { window_ = makeUnique(); 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; diff --git a/Extra2D/src/effects/particle_system.cpp b/Extra2D/src/effects/particle_system.cpp index 48bbb9b..5c9b18a 100644 --- a/Extra2D/src/effects/particle_system.cpp +++ b/Extra2D/src/effects/particle_system.cpp @@ -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, diff --git a/Extra2D/src/scene/sprite.cpp b/Extra2D/src/scene/sprite.cpp index 29d53a8..1d3e239 100644 --- a/Extra2D/src/scene/sprite.cpp +++ b/Extra2D/src/scene/sprite.cpp @@ -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 &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_; diff --git a/xmake.lua b/xmake.lua index 289a705..1d8b574 100644 --- a/xmake.lua +++ b/xmake.lua @@ -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 -- ==============================================