refactor(渲染): 统一处理锚点计算逻辑
将锚点计算逻辑统一交由 RenderBackend 处理,简化各渲染组件的代码 添加窗口高级配置选项,支持自定义光标和DPI缩放 更新窗口初始化逻辑,使用配置中的宽高参数
This commit is contained in:
parent
f1ec3f8eae
commit
5bab53aa2a
|
|
@ -39,6 +39,9 @@ struct AppConfig {
|
||||||
BackendType renderBackend = BackendType::OpenGL;
|
BackendType renderBackend = BackendType::OpenGL;
|
||||||
int msaaSamples = 0;
|
int msaaSamples = 0;
|
||||||
PlatformType platform = PlatformType::Auto;
|
PlatformType platform = PlatformType::Auto;
|
||||||
|
// 窗口高级配置
|
||||||
|
bool enableCursors = true; // 是否启用光标
|
||||||
|
bool enableDpiScale = false; // 是否启用DPI缩放
|
||||||
};
|
};
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
|
||||||
|
|
@ -155,9 +155,9 @@ void FrameRenderer::drawSpriteFrame(RenderBackend &renderer,
|
||||||
float flipScaleX = flipX ? -1.0f : 1.0f;
|
float flipScaleX = flipX ? -1.0f : 1.0f;
|
||||||
float flipScaleY = flipY ? -1.0f : 1.0f;
|
float flipScaleY = flipY ? -1.0f : 1.0f;
|
||||||
|
|
||||||
Rect destRect{
|
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
|
||||||
{finalPos.x - w * 0.5f * flipScaleX, finalPos.y - h * 0.5f * flipScaleY},
|
// 使用中心锚点(0.5, 0.5),所以 destRect 从 finalPos 开始,不预偏移
|
||||||
{w, h}};
|
Rect destRect{{finalPos.x, finalPos.y}, {w * flipScaleX, h * flipScaleY}};
|
||||||
|
|
||||||
Color finalColor{tint.r, tint.g, tint.b, tint.a * opacity};
|
Color finalColor{tint.r, tint.g, tint.b, tint.a * opacity};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,20 +93,20 @@ bool Application::init(const AppConfig &config) {
|
||||||
window_ = makeUnique<Window>();
|
window_ = makeUnique<Window>();
|
||||||
WindowConfig winConfig;
|
WindowConfig winConfig;
|
||||||
winConfig.title = config.title;
|
winConfig.title = config.title;
|
||||||
winConfig.width = 1280;
|
winConfig.width = config.width;
|
||||||
winConfig.height = 720;
|
winConfig.height = config.height;
|
||||||
if (platform == PlatformType::Switch) {
|
if (platform == PlatformType::Switch) {
|
||||||
winConfig.fullscreen = true;
|
winConfig.fullscreen = true;
|
||||||
winConfig.fullscreenDesktop = false; // Switch 使用固定分辨率全屏
|
winConfig.fullscreenDesktop = false; // Switch 使用固定分辨率全屏
|
||||||
winConfig.resizable = false;
|
winConfig.resizable = false;
|
||||||
winConfig.enableCursors = false;
|
winConfig.enableCursors = config.enableCursors;
|
||||||
winConfig.enableDpiScale = false;
|
winConfig.enableDpiScale = config.enableDpiScale;
|
||||||
} else {
|
} else {
|
||||||
// PC 平台默认窗口模式
|
// PC 平台默认窗口模式
|
||||||
winConfig.fullscreen = config.fullscreen;
|
winConfig.fullscreen = config.fullscreen;
|
||||||
winConfig.resizable = true;
|
winConfig.resizable = config.resizable;
|
||||||
winConfig.enableCursors = true;
|
winConfig.enableCursors = config.enableCursors;
|
||||||
winConfig.enableDpiScale = true;
|
winConfig.enableDpiScale = config.enableDpiScale;
|
||||||
}
|
}
|
||||||
winConfig.vsync = config.vsync;
|
winConfig.vsync = config.vsync;
|
||||||
winConfig.msaaSamples = config.msaaSamples;
|
winConfig.msaaSamples = config.msaaSamples;
|
||||||
|
|
|
||||||
|
|
@ -121,9 +121,8 @@ void ParticleEmitter::render(RenderBackend &renderer) {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 计算目标矩形
|
// 计算目标矩形
|
||||||
float halfSize = p.size * 0.5f;
|
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
|
||||||
Rect destRect(p.position.x - halfSize, p.position.y - halfSize, p.size,
|
Rect destRect(p.position.x, p.position.y, p.size, p.size);
|
||||||
p.size);
|
|
||||||
|
|
||||||
renderer.drawSprite(
|
renderer.drawSprite(
|
||||||
*config_.texture, destRect,
|
*config_.texture, destRect,
|
||||||
|
|
|
||||||
|
|
@ -79,9 +79,8 @@ void Sprite::onDraw(RenderBackend &renderer) {
|
||||||
auto pos = getPosition();
|
auto pos = getPosition();
|
||||||
auto anchor = getAnchor();
|
auto anchor = getAnchor();
|
||||||
auto scale = getScale();
|
auto scale = getScale();
|
||||||
Rect destRect(pos.x - width * anchor.x * scale.x,
|
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
|
||||||
pos.y - height * anchor.y * scale.y, width * scale.x,
|
Rect destRect(pos.x, pos.y, width * scale.x, height * scale.y);
|
||||||
height * scale.y);
|
|
||||||
|
|
||||||
// Adjust source rect for flipping
|
// Adjust source rect for flipping
|
||||||
Rect srcRect = textureRect_;
|
Rect srcRect = textureRect_;
|
||||||
|
|
@ -111,9 +110,8 @@ void Sprite::generateRenderCommand(std::vector<RenderCommand> &commands,
|
||||||
auto pos = getPosition();
|
auto pos = getPosition();
|
||||||
auto anchor = getAnchor();
|
auto anchor = getAnchor();
|
||||||
auto scale = getScale();
|
auto scale = getScale();
|
||||||
Rect destRect(pos.x - width * anchor.x * scale.x,
|
// 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸
|
||||||
pos.y - height * anchor.y * scale.y, width * scale.x,
|
Rect destRect(pos.x, pos.y, width * scale.x, height * scale.y);
|
||||||
height * scale.y);
|
|
||||||
|
|
||||||
// 调整源矩形(翻转)
|
// 调整源矩形(翻转)
|
||||||
Rect srcRect = textureRect_;
|
Rect srcRect = textureRect_;
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,7 @@ if is_config("examples","true") then
|
||||||
includes("examples/spatial_index_demo", {rootdir = "examples/spatial_index_demo"})
|
includes("examples/spatial_index_demo", {rootdir = "examples/spatial_index_demo"})
|
||||||
includes("examples/collision_demo", {rootdir = "examples/collision_demo"})
|
includes("examples/collision_demo", {rootdir = "examples/collision_demo"})
|
||||||
includes("examples/push_box", {rootdir = "examples/push_box"})
|
includes("examples/push_box", {rootdir = "examples/push_box"})
|
||||||
|
-- includes("examples/flappy_bird", {rootdir = "examples/flappy_bird"})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ==============================================
|
-- ==============================================
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue