From ea5ecd383f4573bbb0a91d98387ee553dce80abd Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Thu, 26 Feb 2026 00:55:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=9F=E4=B8=80=E8=BE=B9?= =?UTF-8?q?=E7=95=8C=E6=A1=86=E6=96=B9=E6=B3=95=E5=90=8D=E7=A7=B0=E4=B8=BA?= =?UTF-8?q?=20boundingBox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 getBoundingBox 方法重命名为 boundingBox,以遵循更简洁的命名规范 同时更新相关文档和示例代码中的调用 --- docs/API_Tutorial/03_Node_System.md | 4 +- docs/API_Tutorial/06_Collision_Detection.md | 4 +- examples/collision_demo/main.cpp | 23 +- examples/flappy_bird/GameScene.cpp | 5 +- examples/flappy_bird/StartScene.cpp | 4 +- examples/flappy_bird/bird.cpp | 266 +++++----- examples/flappy_bird/bird.h | 182 +++---- examples/flappy_bird/pipe.cpp | 158 +++--- examples/flappy_bird/pipe.h | 62 +-- examples/spatial_index_demo/main.cpp | 14 +- include/graphics/alpha_mask.h | 2 +- include/graphics/opengl/gl_texture.h | 91 ++-- include/graphics/render_target.h | 2 +- include/graphics/texture.h | 85 ++-- include/platform/window.h | 208 ++++---- include/scene/node.h | 69 +-- include/scene/shape_node.h | 2 +- include/scene/sprite.h | 2 +- include/ui/button.h | 2 +- include/ui/check_box.h | 132 ++--- include/ui/label.h | 223 ++++----- include/ui/progress_bar.h | 368 +++++++------- include/ui/radio_button.h | 162 +++---- include/ui/slider.h | 228 ++++----- include/ui/text.h | 7 +- include/ui/widget.h | 6 +- src/scene/node.cpp | 25 +- src/scene/scene_manager.cpp | 2 +- src/scene/shape_node.cpp | 32 +- src/scene/sprite.cpp | 4 +- src/spatial/spatial_manager.cpp | 2 +- src/ui/button.cpp | 28 +- src/ui/check_box.cpp | 159 +++--- src/ui/label.cpp | 429 ++++++++--------- src/ui/progress_bar.cpp | 506 +++++++++----------- src/ui/radio_button.cpp | 226 +++++---- src/ui/slider.cpp | 439 ++++++++--------- src/ui/text.cpp | 7 +- src/ui/widget.cpp | 10 +- 39 files changed, 2031 insertions(+), 2149 deletions(-) diff --git a/docs/API_Tutorial/03_Node_System.md b/docs/API_Tutorial/03_Node_System.md index 7c33dbf..0e65aa7 100644 --- a/docs/API_Tutorial/03_Node_System.md +++ b/docs/API_Tutorial/03_Node_System.md @@ -316,8 +316,8 @@ if (buttonFrame) { **按钮使用世界坐标空间(World Space)**: ```cpp -// Button::getBoundingBox() 返回世界坐标系的包围盒 -Rect Button::getBoundingBox() const { +// Button::boundingBox() 返回世界坐标系的包围盒 +Rect Button::boundingBox() const { auto pos = getRenderPosition(); // 获取世界坐标位置 // ... 计算包围盒 return Rect(x0, y0, w, h); // 世界坐标 diff --git a/docs/API_Tutorial/06_Collision_Detection.md b/docs/API_Tutorial/06_Collision_Detection.md index 9347d26..e4f188a 100644 --- a/docs/API_Tutorial/06_Collision_Detection.md +++ b/docs/API_Tutorial/06_Collision_Detection.md @@ -22,7 +22,7 @@ public: } // 必须实现 getBoundingBox 方法 - Rect getBoundingBox() const override { + Rect boundingBox() const override { Vec2 pos = getPosition(); return Rect(pos.x - width_ / 2, pos.y - height_ / 2, width_, height_); } @@ -212,7 +212,7 @@ private: ## 关键要点 1. **必须调用 `setSpatialIndexed(true)`** - 启用节点的空间索引 -2. **必须实现 `getBoundingBox()`** - 返回准确的边界框 +2. **必须实现 `boundingBox()`** - 返回准确的边界框 3. **在 `onEnter()` 中调用 `Scene::onEnter()`** - 确保节点正确注册到空间索引 4. **使用 `queryCollisions()`** - 自动利用空间索引优化检测 diff --git a/examples/collision_demo/main.cpp b/examples/collision_demo/main.cpp index 9ce6bba..7adad57 100644 --- a/examples/collision_demo/main.cpp +++ b/examples/collision_demo/main.cpp @@ -16,10 +16,11 @@ public: void setColliding(bool colliding) { isColliding_ = colliding; } - Rect getBoundingBox() const override { + Rect boundingBox() const override { // 返回实际的矩形边界 Vec2 position = pos(); - return Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_); + return Rect(position.x - width_ / 2, position.y - height_ / 2, width_, + height_); } void onRender(RenderBackend &renderer) override { @@ -27,17 +28,17 @@ public: // 绘制填充矩形 Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.8f) : color_; - renderer.fillRect( - Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_), - fillColor); + renderer.fillRect(Rect(position.x - width_ / 2, position.y - height_ / 2, + width_, height_), + fillColor); // 绘制边框 Color borderColor = isColliding_ ? Color(1.0f, 0.0f, 0.0f, 1.0f) : Color(1.0f, 1.0f, 1.0f, 0.5f); float borderWidth = isColliding_ ? 3.0f : 2.0f; - renderer.drawRect( - Rect(position.x - width_ / 2, position.y - height_ / 2, width_, height_), - borderColor, borderWidth); + renderer.drawRect(Rect(position.x - width_ / 2, position.y - height_ / 2, + width_, height_), + borderColor, borderWidth); } private: @@ -149,7 +150,8 @@ private: addChild(fpsText_); // 创建退出提示文本 - float screenHeight = static_cast(Application::instance().getConfig().height); + float screenHeight = + static_cast(Application::instance().getConfig().height); exitHintText_ = Text::create("按 + 键退出", infoFont_); exitHintText_->setPosition(50.0f, screenHeight - 50.0f); exitHintText_->setTextColor(Color(0.8f, 0.8f, 0.8f, 1.0f)); @@ -238,8 +240,7 @@ private: // 程序入口 // ============================================================================ -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { // 初始化日志系统 Logger::init(); Logger::setLevel(LogLevel::Debug); diff --git a/examples/flappy_bird/GameScene.cpp b/examples/flappy_bird/GameScene.cpp index b04d5df..5037220 100644 --- a/examples/flappy_bird/GameScene.cpp +++ b/examples/flappy_bird/GameScene.cpp @@ -123,8 +123,7 @@ void GameScene::onUpdate(float dt) { } if (bird_->isLiving() && GAME_HEIGHT - bird_->pos().y <= 123.0f) { - bird_->setPosition( - extra2d::Vec2(bird_->pos().x, GAME_HEIGHT - 123.0f)); + bird_->setPosition(extra2d::Vec2(bird_->pos().x, GAME_HEIGHT - 123.0f)); bird_->setStatus(Bird::Status::Still); onHit(); } @@ -159,7 +158,7 @@ bool GameScene::checkCollision() { if (!bird_ || !pipes_) return false; - extra2d::Rect birdBox = bird_->getBoundingBox(); + extra2d::Rect birdBox = bird_->boundingBox(); // 检查与每个水管的碰撞 for (int i = 0; i < 3; ++i) { diff --git a/examples/flappy_bird/StartScene.cpp b/examples/flappy_bird/StartScene.cpp index 83c9f67..84f8e43 100644 --- a/examples/flappy_bird/StartScene.cpp +++ b/examples/flappy_bird/StartScene.cpp @@ -74,7 +74,7 @@ void StartScene::onEnter() { playBtn_->setAnchor(0.5f, 0.5f); // PLAY 按钮在小鸟下方 playBtn_->setPosition(screenWidth / 2.0f, - screenHeight - playBtn_->getSize().height - 100.0f); + screenHeight - playBtn_->size().height - 100.0f); playBtn_->setOnClick([this]() { ResLoader::playMusic(MusicType::Click); startGame(); @@ -95,7 +95,7 @@ void StartScene::onEnter() { shareBtn_->setAnchor(0.5f, 0.5f); // SHARE 按钮在 PLAY 按钮下方,靠近地面 shareBtn_->setPosition(screenWidth / 2.0f, - screenHeight - shareBtn_->getSize().height - 80.0f); + screenHeight - shareBtn_->size().height - 80.0f); shareBtn_->setOnClick([this]() { ResLoader::playMusic(MusicType::Click); // 分享功能暂不实现 diff --git a/examples/flappy_bird/bird.cpp b/examples/flappy_bird/bird.cpp index e690736..e028939 100644 --- a/examples/flappy_bird/bird.cpp +++ b/examples/flappy_bird/bird.cpp @@ -7,189 +7,189 @@ namespace flappybird { -Bird::Bird() { - setStatus(Status::Idle); -} +Bird::Bird() { setStatus(Status::Idle); } Bird::~Bird() = default; void Bird::onEnter() { - Node::onEnter(); - if (!sprite_) { - initAnimations(); - } + Node::onEnter(); + if (!sprite_) { + initAnimations(); + } } void Bird::initAnimations() { - // 随机选择小鸟颜色(0-2) - int colorMode = extra2d::randomInt(0, 2); - std::string prefix = "bird" + std::to_string(colorMode) + "_"; + // 随机选择小鸟颜色(0-2) + int colorMode = extra2d::randomInt(0, 2); + std::string prefix = "bird" + std::to_string(colorMode) + "_"; - // 加载动画帧序列: 0 -> 1 -> 2 -> 1 - int frameSequence[] = {0, 1, 2, 1}; - for (int frameIndex : frameSequence) { - auto frameSprite = ResLoader::getKeyFrame(prefix + std::to_string(frameIndex)); - if (frameSprite) { - frames_.push_back(frameSprite); - } else { - E2D_LOG_WARN("无法加载动画帧: {}{}", prefix, frameIndex); - } - } - - // 创建精灵 - if (!frames_.empty()) { - sprite_ = extra2d::Sprite::create(); - setCurrentFrame(0); - addChild(sprite_); - E2D_LOG_INFO("小鸟动画创建成功: 颜色={}, 帧数={}", colorMode, frames_.size()); + // 加载动画帧序列: 0 -> 1 -> 2 -> 1 + int frameSequence[] = {0, 1, 2, 1}; + for (int frameIndex : frameSequence) { + auto frameSprite = + ResLoader::getKeyFrame(prefix + std::to_string(frameIndex)); + if (frameSprite) { + frames_.push_back(frameSprite); } else { - E2D_LOG_ERROR("小鸟动画创建失败: 没有找到任何动画帧"); + E2D_LOG_WARN("无法加载动画帧: {}{}", prefix, frameIndex); } + } + + // 创建精灵 + if (!frames_.empty()) { + sprite_ = extra2d::Sprite::create(); + setCurrentFrame(0); + addChild(sprite_); + E2D_LOG_INFO("小鸟动画创建成功: 颜色={}, 帧数={}", colorMode, + frames_.size()); + } else { + E2D_LOG_ERROR("小鸟动画创建失败: 没有找到任何动画帧"); + } } void Bird::setCurrentFrame(int frameIndex) { - if (frames_.empty() || !sprite_) return; - - frameIndex = frameIndex % static_cast(frames_.size()); - currentFrame_ = frameIndex; - - auto& frame = frames_[frameIndex]; - sprite_->setTexture(frame->getTexture()); - sprite_->setTextureRect(frame->getRect()); + if (frames_.empty() || !sprite_) + return; + + frameIndex = frameIndex % static_cast(frames_.size()); + currentFrame_ = frameIndex; + + auto &frame = frames_[frameIndex]; + sprite_->setTexture(frame->getTexture()); + sprite_->setTextureRect(frame->getRect()); } void Bird::updateFrameAnimation(float dt) { - if (frames_.empty() || status_ == Status::Still) return; + if (frames_.empty() || status_ == Status::Still) + return; - frameTimer_ += dt; - - float interval = frameInterval_; - if (status_ == Status::StartToFly) { - interval = 0.05f; // 2倍速度 - } - - while (frameTimer_ >= interval) { - frameTimer_ -= interval; - setCurrentFrame((currentFrame_ + 1) % static_cast(frames_.size())); - } + frameTimer_ += dt; + + float interval = frameInterval_; + if (status_ == Status::StartToFly) { + interval = 0.05f; // 2倍速度 + } + + while (frameTimer_ >= interval) { + frameTimer_ -= interval; + setCurrentFrame((currentFrame_ + 1) % static_cast(frames_.size())); + } } void Bird::onUpdate(float dt) { - extra2d::Node::onUpdate(dt); + extra2d::Node::onUpdate(dt); - // 更新帧动画 - updateFrameAnimation(dt); + // 更新帧动画 + updateFrameAnimation(dt); - // 处理闲置动画(上下浮动) - if (status_ == Status::Idle) { - idleTimer_ += dt; - idleOffset_ = std::sin(idleTimer_ * 5.0f) * 4.0f; - } + // 处理闲置动画(上下浮动) + if (status_ == Status::Idle) { + idleTimer_ += dt; + idleOffset_ = std::sin(idleTimer_ * 5.0f) * 4.0f; + } } -void Bird::onRender(extra2d::RenderBackend& renderer) { - // 精灵会自动渲染,这里只需要处理旋转和偏移 - if (sprite_) { - sprite_->setRotation(rotation_); +void Bird::onRender(extra2d::RenderBackend &renderer) { + // 精灵会自动渲染,这里只需要处理旋转和偏移 + if (sprite_) { + sprite_->setRotation(rotation_); - // 应用闲置偏移 - if (status_ == Status::Idle) { - sprite_->setPosition(extra2d::Vec2(0.0f, idleOffset_)); - } else { - sprite_->setPosition(extra2d::Vec2(0.0f, 0.0f)); - } + // 应用闲置偏移 + if (status_ == Status::Idle) { + sprite_->setPosition(extra2d::Vec2(0.0f, idleOffset_)); + } else { + sprite_->setPosition(extra2d::Vec2(0.0f, 0.0f)); } + } - // 调用父类的 onRender 来渲染子节点 - Node::onRender(renderer); + // 调用父类的 onRender 来渲染子节点 + Node::onRender(renderer); } void Bird::fall(float dt) { - if (!living_) return; + if (!living_) + return; - // 更新垂直位置 - extra2d::Vec2 position = pos(); - position.y += speed_ * dt; + // 更新垂直位置 + extra2d::Vec2 position = pos(); + position.y += speed_ * dt; + setPosition(position); + + // 应用重力 + speed_ += gravity * dt; + + // 限制顶部边界 + if (position.y < 0) { + position.y = 0; setPosition(position); + speed_ = 0; + } - // 应用重力 - speed_ += gravity * dt; - - // 限制顶部边界 - if (position.y < 0) { - position.y = 0; - setPosition(position); - speed_ = 0; - } - - // 根据速度计算旋转角度 - // 上升时抬头(-15度),下降时低头(最大90度) - if (speed_ < 0) { - rotation_ = -15.0f; - } else { - rotation_ = std::min(90.0f, speed_ * 0.15f); - } + // 根据速度计算旋转角度 + // 上升时抬头(-15度),下降时低头(最大90度) + if (speed_ < 0) { + rotation_ = -15.0f; + } else { + rotation_ = std::min(90.0f, speed_ * 0.15f); + } } void Bird::jump() { - if (!living_) return; + if (!living_) + return; - // 给小鸟向上的速度 - speed_ = -jumpSpeed; - - // 设置状态为飞行 - setStatus(Status::Fly); - - // 播放音效 - ResLoader::playMusic(MusicType::Fly); + // 给小鸟向上的速度 + speed_ = -jumpSpeed; + + // 设置状态为飞行 + setStatus(Status::Fly); + + // 播放音效 + ResLoader::playMusic(MusicType::Fly); } void Bird::die() { - living_ = false; - - // 播放死亡音效 - ResLoader::playMusic(MusicType::Hit); + living_ = false; + + // 播放死亡音效 + ResLoader::playMusic(MusicType::Hit); } void Bird::setStatus(Status status) { - status_ = status; + status_ = status; - switch (status) { - case Status::Still: - // 停止所有动画 - frameTimer_ = 0.0f; - break; + switch (status) { + case Status::Still: + // 停止所有动画 + frameTimer_ = 0.0f; + break; - case Status::Idle: - // 开始闲置动画 - frameInterval_ = 0.1f; // 正常速度 - idleTimer_ = 0.0f; - break; + case Status::Idle: + // 开始闲置动画 + frameInterval_ = 0.1f; // 正常速度 + idleTimer_ = 0.0f; + break; - case Status::StartToFly: - // 停止闲置动画,加速翅膀扇动 - idleOffset_ = 0.0f; - break; + case Status::StartToFly: + // 停止闲置动画,加速翅膀扇动 + idleOffset_ = 0.0f; + break; - case Status::Fly: - // 飞行状态 - break; + case Status::Fly: + // 飞行状态 + break; - default: - break; - } + default: + break; + } } -extra2d::Rect Bird::getBoundingBox() const { - extra2d::Vec2 position = pos(); - // 小鸟碰撞框大小约为 24x24 - float halfSize = 12.0f; - return extra2d::Rect( - position.x - halfSize, - position.y - halfSize, - halfSize * 2.0f, - halfSize * 2.0f - ); +extra2d::Rect Bird::boundingBox() const { + extra2d::Vec2 position = pos(); + // 小鸟碰撞框大小约为 24x24 + float halfSize = 12.0f; + return extra2d::Rect(position.x - halfSize, position.y - halfSize, + halfSize * 2.0f, halfSize * 2.0f); } } // namespace flappybird diff --git a/examples/flappy_bird/bird.h b/examples/flappy_bird/bird.h index 89a45c1..7676621 100644 --- a/examples/flappy_bird/bird.h +++ b/examples/flappy_bird/bird.h @@ -15,116 +15,116 @@ namespace flappybird { */ class Bird : public extra2d::Node { public: - /** - * @brief 小鸟状态枚举 - */ - enum class Status { - Still, // 静止不动 - Idle, // 上下浮动(菜单展示) - StartToFly, // 开始飞行 - Fly // 飞行中 - }; + /** + * @brief 小鸟状态枚举 + */ + enum class Status { + Still, // 静止不动 + Idle, // 上下浮动(菜单展示) + StartToFly, // 开始飞行 + Fly // 飞行中 + }; - /** - * @brief 构造函数 - */ - Bird(); + /** + * @brief 构造函数 + */ + Bird(); - /** - * @brief 析构函数 - */ - ~Bird(); + /** + * @brief 析构函数 + */ + ~Bird(); - /** - * @brief 每帧更新 - * @param dt 时间间隔(秒) - */ - void onUpdate(float dt) override; + /** + * @brief 每帧更新 + * @param dt 时间间隔(秒) + */ + void onUpdate(float dt) override; - /** - * @brief 渲染小鸟 - * @param renderer 渲染后端 - */ - void onRender(extra2d::RenderBackend& renderer) override; + /** + * @brief 渲染小鸟 + * @param renderer 渲染后端 + */ + void onRender(extra2d::RenderBackend &renderer) override; - /** - * @brief 进入场景时调用 - */ - void onEnter() override; + /** + * @brief 进入场景时调用 + */ + void onEnter() override; - /** - * @brief 模拟下落 - * @param dt 时间间隔(秒) - */ - void fall(float dt); + /** + * @brief 模拟下落 + * @param dt 时间间隔(秒) + */ + void fall(float dt); - /** - * @brief 跳跃 - */ - void jump(); + /** + * @brief 跳跃 + */ + void jump(); - /** - * @brief 死亡 - */ - void die(); + /** + * @brief 死亡 + */ + void die(); - /** - * @brief 设置小鸟状态 - * @param status 新状态 - */ - void setStatus(Status status); + /** + * @brief 设置小鸟状态 + * @param status 新状态 + */ + void setStatus(Status status); - /** - * @brief 获取当前状态 - * @return 当前状态 - */ - Status getStatus() const { return status_; } + /** + * @brief 获取当前状态 + * @return 当前状态 + */ + Status getStatus() const { return status_; } - /** - * @brief 检查是否存活 - * @return 是否存活 - */ - bool isLiving() const { return living_; } + /** + * @brief 检查是否存活 + * @return 是否存活 + */ + bool isLiving() const { return living_; } - /** - * @brief 获取边界框(用于碰撞检测) - * @return 边界框 - */ - extra2d::Rect getBoundingBox() const override; + /** + * @brief 获取边界框(用于碰撞检测) + * @return 边界框 + */ + extra2d::Rect boundingBox() const override; private: - /** - * @brief 初始化动画 - */ - void initAnimations(); + /** + * @brief 初始化动画 + */ + void initAnimations(); - /** - * @brief 更新帧动画 - */ - void updateFrameAnimation(float dt); + /** + * @brief 更新帧动画 + */ + void updateFrameAnimation(float dt); - /** - * @brief 设置当前帧 - */ - void setCurrentFrame(int frameIndex); + /** + * @brief 设置当前帧 + */ + void setCurrentFrame(int frameIndex); - bool living_ = true; // 是否存活 - float speed_ = 0.0f; // 垂直速度 - float rotation_ = 0.0f; // 旋转角度 - Status status_ = Status::Idle; // 当前状态 + bool living_ = true; // 是否存活 + float speed_ = 0.0f; // 垂直速度 + float rotation_ = 0.0f; // 旋转角度 + Status status_ = Status::Idle; // 当前状态 - // 动画相关 - extra2d::Ptr sprite_; // 精灵 - std::vector> frames_; // 动画帧 - int currentFrame_ = 0; // 当前帧索引 - float frameTimer_ = 0.0f; // 帧计时器 - float frameInterval_ = 0.1f; // 帧间隔(秒) - float idleTimer_ = 0.0f; // 闲置动画计时器 - float idleOffset_ = 0.0f; // 闲置偏移量 + // 动画相关 + extra2d::Ptr sprite_; // 精灵 + std::vector> frames_; // 动画帧 + int currentFrame_ = 0; // 当前帧索引 + float frameTimer_ = 0.0f; // 帧计时器 + float frameInterval_ = 0.1f; // 帧间隔(秒) + float idleTimer_ = 0.0f; // 闲置动画计时器 + float idleOffset_ = 0.0f; // 闲置偏移量 - // 物理常量 - static constexpr float gravity = 1440.0f; // 重力加速度 - static constexpr float jumpSpeed = 432.0f; // 跳跃初速度 + // 物理常量 + static constexpr float gravity = 1440.0f; // 重力加速度 + static constexpr float jumpSpeed = 432.0f; // 跳跃初速度 }; } // namespace flappybird diff --git a/examples/flappy_bird/pipe.cpp b/examples/flappy_bird/pipe.cpp index a68d7e5..9f8b261 100644 --- a/examples/flappy_bird/pipe.cpp +++ b/examples/flappy_bird/pipe.cpp @@ -3,111 +3,105 @@ // ============================================================================ #include "Pipe.h" -#include "ResLoader.h" #include "BaseScene.h" +#include "ResLoader.h" + namespace flappybird { Pipe::Pipe() { - scored = false; - // 注意:不要在构造函数中创建子节点 - // 因为此时 weak_from_this() 还不能使用 + scored = false; + // 注意:不要在构造函数中创建子节点 + // 因为此时 weak_from_this() 还不能使用 } void Pipe::onEnter() { - Node::onEnter(); - - // 在 onEnter 中创建子节点,此时 weak_from_this() 可用 - if (!topPipe_ && !bottomPipe_) { - // 使用游戏逻辑高度 - float screenHeight = GAME_HEIGHT; + Node::onEnter(); - // 获取地面高度 - auto landFrame = ResLoader::getKeyFrame("land"); - float landHeight = landFrame ? landFrame->getRect().size.height : 112.0f; + // 在 onEnter 中创建子节点,此时 weak_from_this() 可用 + if (!topPipe_ && !bottomPipe_) { + // 使用游戏逻辑高度 + float screenHeight = GAME_HEIGHT; - // 随机生成水管高度 - // 范围:与屏幕顶部最小距离不小于 100 像素 - // 与屏幕底部最小距离不小于地面上方 100 像素 - float minHeight = 100.0f; - float maxHeight = screenHeight - landHeight - 100.0f - gapHeight_; - float height = static_cast(extra2d::randomInt(static_cast(minHeight), static_cast(maxHeight))); + // 获取地面高度 + auto landFrame = ResLoader::getKeyFrame("land"); + float landHeight = landFrame ? landFrame->getRect().size.height : 112.0f; - // 创建上水管 - auto topFrame = ResLoader::getKeyFrame("pipe_above"); - if (topFrame) { - topPipe_ = extra2d::Sprite::create(topFrame->getTexture(), topFrame->getRect()); - topPipe_->setAnchor(extra2d::Vec2(0.5f, 1.0f)); // 锚点设在底部中心 - topPipe_->setPosition(extra2d::Vec2(0.0f, height - gapHeight_ / 2.0f)); - addChild(topPipe_); - } + // 随机生成水管高度 + // 范围:与屏幕顶部最小距离不小于 100 像素 + // 与屏幕底部最小距离不小于地面上方 100 像素 + float minHeight = 100.0f; + float maxHeight = screenHeight - landHeight - 100.0f - gapHeight_; + float height = static_cast(extra2d::randomInt( + static_cast(minHeight), static_cast(maxHeight))); - // 创建下水管 - auto bottomFrame = ResLoader::getKeyFrame("pipe_below"); - if (bottomFrame) { - bottomPipe_ = extra2d::Sprite::create(bottomFrame->getTexture(), bottomFrame->getRect()); - bottomPipe_->setAnchor(extra2d::Vec2(0.5f, 0.0f)); // 锚点设在顶部中心 - bottomPipe_->setPosition(extra2d::Vec2(0.0f, height + gapHeight_ / 2.0f)); - addChild(bottomPipe_); - } + // 创建上水管 + auto topFrame = ResLoader::getKeyFrame("pipe_above"); + if (topFrame) { + topPipe_ = + extra2d::Sprite::create(topFrame->getTexture(), topFrame->getRect()); + topPipe_->setAnchor(extra2d::Vec2(0.5f, 1.0f)); // 锚点设在底部中心 + topPipe_->setPosition(extra2d::Vec2(0.0f, height - gapHeight_ / 2.0f)); + addChild(topPipe_); } + + // 创建下水管 + auto bottomFrame = ResLoader::getKeyFrame("pipe_below"); + if (bottomFrame) { + bottomPipe_ = extra2d::Sprite::create(bottomFrame->getTexture(), + bottomFrame->getRect()); + bottomPipe_->setAnchor(extra2d::Vec2(0.5f, 0.0f)); // 锚点设在顶部中心 + bottomPipe_->setPosition(extra2d::Vec2(0.0f, height + gapHeight_ / 2.0f)); + addChild(bottomPipe_); + } + } } Pipe::~Pipe() = default; -extra2d::Rect Pipe::getBoundingBox() const { - // 返回整个水管的边界框(包含上下两根) - extra2d::Vec2 position = pos(); - - // 水管宽度约为 52 - float pipeWidth = 52.0f; - float halfWidth = pipeWidth / 2.0f; - - // 使用游戏逻辑高度 - float screenHeight = GAME_HEIGHT; - - return extra2d::Rect( - position.x - halfWidth, - 0.0f, - pipeWidth, - screenHeight - ); +extra2d::Rect Pipe::boundingBox() const { + // 返回整个水管的边界框(包含上下两根) + extra2d::Vec2 position = pos(); + + // 水管宽度约为 52 + float pipeWidth = 52.0f; + float halfWidth = pipeWidth / 2.0f; + + // 使用游戏逻辑高度 + float screenHeight = GAME_HEIGHT; + + return extra2d::Rect(position.x - halfWidth, 0.0f, pipeWidth, screenHeight); } extra2d::Rect Pipe::getTopPipeBox() const { - if (!topPipe_) return extra2d::Rect(); - - extra2d::Vec2 position = pos(); - extra2d::Vec2 topPos = topPipe_->pos(); - - // 上水管尺寸 - float pipeWidth = 52.0f; - float pipeHeight = 320.0f; - - return extra2d::Rect( - position.x - pipeWidth / 2.0f, - position.y + topPos.y - pipeHeight, - pipeWidth, - pipeHeight - ); + if (!topPipe_) + return extra2d::Rect(); + + extra2d::Vec2 position = pos(); + extra2d::Vec2 topPos = topPipe_->pos(); + + // 上水管尺寸 + float pipeWidth = 52.0f; + float pipeHeight = 320.0f; + + return extra2d::Rect(position.x - pipeWidth / 2.0f, + position.y + topPos.y - pipeHeight, pipeWidth, + pipeHeight); } extra2d::Rect Pipe::getBottomPipeBox() const { - if (!bottomPipe_) return extra2d::Rect(); - - extra2d::Vec2 position = pos(); - extra2d::Vec2 bottomPos = bottomPipe_->pos(); - - // 下水管尺寸 - float pipeWidth = 52.0f; - float pipeHeight = 320.0f; - - return extra2d::Rect( - position.x - pipeWidth / 2.0f, - position.y + bottomPos.y, - pipeWidth, - pipeHeight - ); + if (!bottomPipe_) + return extra2d::Rect(); + + extra2d::Vec2 position = pos(); + extra2d::Vec2 bottomPos = bottomPipe_->pos(); + + // 下水管尺寸 + float pipeWidth = 52.0f; + float pipeHeight = 320.0f; + + return extra2d::Rect(position.x - pipeWidth / 2.0f, position.y + bottomPos.y, + pipeWidth, pipeHeight); } } // namespace flappybird diff --git a/examples/flappy_bird/pipe.h b/examples/flappy_bird/pipe.h index 74c1862..67c81c7 100644 --- a/examples/flappy_bird/pipe.h +++ b/examples/flappy_bird/pipe.h @@ -15,45 +15,45 @@ namespace flappybird { */ class Pipe : public extra2d::Node { public: - /** - * @brief 构造函数 - */ - Pipe(); + /** + * @brief 构造函数 + */ + Pipe(); - /** - * @brief 析构函数 - */ - ~Pipe(); + /** + * @brief 析构函数 + */ + ~Pipe(); - /** - * @brief 进入场景时调用 - */ - void onEnter() override; + /** + * @brief 进入场景时调用 + */ + void onEnter() override; - /** - * @brief 获取边界框(用于碰撞检测) - * @return 边界框 - */ - extra2d::Rect getBoundingBox() const override; + /** + * @brief 获取边界框(用于碰撞检测) + * @return 边界框 + */ + extra2d::Rect boundingBox() const override; - /** - * @brief 获取上水管边界框 - * @return 边界框 - */ - extra2d::Rect getTopPipeBox() const; + /** + * @brief 获取上水管边界框 + * @return 边界框 + */ + extra2d::Rect getTopPipeBox() const; - /** - * @brief 获取下水管边界框 - * @return 边界框 - */ - extra2d::Rect getBottomPipeBox() const; + /** + * @brief 获取下水管边界框 + * @return 边界框 + */ + extra2d::Rect getBottomPipeBox() const; - bool scored = false; // 是否已计分 + bool scored = false; // 是否已计分 private: - extra2d::Ptr topPipe_; // 上水管 - extra2d::Ptr bottomPipe_; // 下水管 - float gapHeight_ = 120.0f; // 间隙高度 + extra2d::Ptr topPipe_; // 上水管 + extra2d::Ptr bottomPipe_; // 下水管 + float gapHeight_ = 120.0f; // 间隙高度 }; } // namespace flappybird diff --git a/examples/spatial_index_demo/main.cpp b/examples/spatial_index_demo/main.cpp index 5b67af8..e339864 100644 --- a/examples/spatial_index_demo/main.cpp +++ b/examples/spatial_index_demo/main.cpp @@ -37,8 +37,8 @@ public: bool isColliding() const { return isColliding_; } int getId() const { return id_; } - // 必须实现 getBoundingBox() 才能参与空间索引碰撞检测 - Rect getBoundingBox() const override { + // 必须实现 boundingBox() 才能参与空间索引碰撞检测 + Rect boundingBox() const override { Vec2 position = pos(); return Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_); } @@ -65,14 +65,16 @@ public: // 碰撞时变红色 Color fillColor = isColliding_ ? Color(1.0f, 0.2f, 0.2f, 0.9f) : color_; - renderer.fillRect(Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_), - fillColor); + renderer.fillRect( + Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_), + fillColor); // 绘制边框 Color borderColor = isColliding_ ? Color(1.0f, 0.0f, 0.0f, 1.0f) : Color(0.3f, 0.3f, 0.3f, 0.5f); - renderer.drawRect(Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_), - borderColor, 1.0f); + renderer.drawRect( + Rect(position.x - size_ / 2, position.y - size_ / 2, size_, size_), + borderColor, 1.0f); } private: diff --git a/include/graphics/alpha_mask.h b/include/graphics/alpha_mask.h index ba79480..131f357 100644 --- a/include/graphics/alpha_mask.h +++ b/include/graphics/alpha_mask.h @@ -30,7 +30,7 @@ public: /// 获取遮罩尺寸 int getWidth() const { return width_; } int getHeight() const { return height_; } - Size getSize() const { + Size size() const { return Size(static_cast(width_), static_cast(height_)); } diff --git a/include/graphics/opengl/gl_texture.h b/include/graphics/opengl/gl_texture.h index b2768ec..f04da31 100644 --- a/include/graphics/opengl/gl_texture.h +++ b/include/graphics/opengl/gl_texture.h @@ -1,7 +1,8 @@ #pragma once -#include #include +#include + #include @@ -15,58 +16,64 @@ namespace extra2d { // ============================================================================ class GLTexture : public Texture { public: - GLTexture(int width, int height, const uint8_t* pixels, int channels); - GLTexture(const std::string& filepath); - ~GLTexture(); + GLTexture(int width, int height, const uint8_t *pixels, int channels); + GLTexture(const std::string &filepath); + ~GLTexture(); - // Texture 接口实现 - int getWidth() const override { return width_; } - int getHeight() const override { return height_; } - Size getSize() const override { return Size(static_cast(width_), static_cast(height_)); } - int getChannels() const override { return channels_; } - PixelFormat getFormat() const override; - void* getNativeHandle() const override { return reinterpret_cast(static_cast(textureID_)); } - bool isValid() const override { return textureID_ != 0; } - void setFilter(bool linear) override; - void setWrap(bool repeat) override; + // Texture 接口实现 + int getWidth() const override { return width_; } + int getHeight() const override { return height_; } + Size size() const override { + return Size(static_cast(width_), static_cast(height_)); + } + int getChannels() const override { return channels_; } + PixelFormat getFormat() const override; + void *getNativeHandle() const override { + return reinterpret_cast(static_cast(textureID_)); + } + bool isValid() const override { return textureID_ != 0; } + void setFilter(bool linear) override; + void setWrap(bool repeat) override; - // 从参数创建纹理的工厂方法 - static Ptr create(int width, int height, PixelFormat format); + // 从参数创建纹理的工厂方法 + static Ptr create(int width, int height, PixelFormat format); - // 加载压缩纹理(KTX/DDS 格式) - bool loadCompressed(const std::string& filepath); + // 加载压缩纹理(KTX/DDS 格式) + bool loadCompressed(const std::string &filepath); - // OpenGL 特定 - GLuint getTextureID() const { return textureID_; } - void bind(unsigned int slot = 0) const; - void unbind() const; + // OpenGL 特定 + GLuint getTextureID() const { return textureID_; } + void bind(unsigned int slot = 0) const; + void unbind() const; - // 获取纹理数据大小(字节),用于 VRAM 跟踪 - size_t getDataSize() const { return dataSize_; } + // 获取纹理数据大小(字节),用于 VRAM 跟踪 + size_t getDataSize() const { return dataSize_; } - // Alpha 遮罩 - bool hasAlphaMask() const { return alphaMask_ != nullptr && alphaMask_->isValid(); } - const AlphaMask* getAlphaMask() const { return alphaMask_.get(); } - void generateAlphaMask(); // 从当前纹理数据生成遮罩 + // Alpha 遮罩 + bool hasAlphaMask() const { + return alphaMask_ != nullptr && alphaMask_->isValid(); + } + const AlphaMask *getAlphaMask() const { return alphaMask_.get(); } + void generateAlphaMask(); // 从当前纹理数据生成遮罩 private: - GLuint textureID_; - int width_; - int height_; - int channels_; - PixelFormat format_; - size_t dataSize_; + GLuint textureID_; + int width_; + int height_; + int channels_; + PixelFormat format_; + size_t dataSize_; - // 原始像素数据(用于生成遮罩) - std::vector pixelData_; - std::unique_ptr alphaMask_; + // 原始像素数据(用于生成遮罩) + std::vector pixelData_; + std::unique_ptr alphaMask_; - void createTexture(const uint8_t* pixels); + void createTexture(const uint8_t *pixels); - // KTX 文件加载 - bool loadKTX(const std::string& filepath); - // DDS 文件加载 - bool loadDDS(const std::string& filepath); + // KTX 文件加载 + bool loadKTX(const std::string &filepath); + // DDS 文件加载 + bool loadDDS(const std::string &filepath); }; } // namespace extra2d diff --git a/include/graphics/render_target.h b/include/graphics/render_target.h index aa48e1a..45b3394 100644 --- a/include/graphics/render_target.h +++ b/include/graphics/render_target.h @@ -78,7 +78,7 @@ public: int getWidth() const { return width_; } int getHeight() const { return height_; } - Vec2 getSize() const { + Vec2 size() const { return Vec2(static_cast(width_), static_cast(height_)); } PixelFormat getColorFormat() const { return colorFormat_; } diff --git a/include/graphics/texture.h b/include/graphics/texture.h index 2ceacec..b372505 100644 --- a/include/graphics/texture.h +++ b/include/graphics/texture.h @@ -1,7 +1,8 @@ #pragma once -#include #include +#include + namespace extra2d { @@ -9,25 +10,25 @@ namespace extra2d { // 像素格式枚举 // ============================================================================ enum class PixelFormat { - R8, // 单通道灰度 - RG8, // 双通道 - RGB8, // RGB 24位 - RGBA8, // RGBA 32位(默认) - RGB16F, // RGB 半精度浮点 - RGBA16F, // RGBA 半精度浮点 - RGB32F, // RGB 全精度浮点 - RGBA32F, // RGBA 全精度浮点 - Depth16, // 16位深度 - Depth24, // 24位深度 - Depth32F, // 32位浮点深度 - Depth24Stencil8, // 24位深度 + 8位模板 + R8, // 单通道灰度 + RG8, // 双通道 + RGB8, // RGB 24位 + RGBA8, // RGBA 32位(默认) + RGB16F, // RGB 半精度浮点 + RGBA16F, // RGBA 半精度浮点 + RGB32F, // RGB 全精度浮点 + RGBA32F, // RGBA 全精度浮点 + Depth16, // 16位深度 + Depth24, // 24位深度 + Depth32F, // 32位浮点深度 + Depth24Stencil8, // 24位深度 + 8位模板 - // 压缩纹理格式 - ETC2_RGB8, // ETC2 RGB 压缩 - ETC2_RGBA8, // ETC2 RGBA 压缩 - ASTC_4x4, // ASTC 4x4 压缩 - ASTC_6x6, // ASTC 6x6 压缩 - ASTC_8x8 // ASTC 8x8 压缩 + // 压缩纹理格式 + ETC2_RGB8, // ETC2 RGB 压缩 + ETC2_RGBA8, // ETC2 RGBA 压缩 + ASTC_4x4, // ASTC 4x4 压缩 + ASTC_6x6, // ASTC 6x6 压缩 + ASTC_8x8 // ASTC 8x8 压缩 }; // ============================================================================ @@ -35,30 +36,30 @@ enum class PixelFormat { // ============================================================================ class Texture { public: - virtual ~Texture() = default; + virtual ~Texture() = default; - // 获取尺寸 - virtual int getWidth() const = 0; - virtual int getHeight() const = 0; - virtual Size getSize() const = 0; - - // 获取通道数 - virtual int getChannels() const = 0; - - // 获取像素格式 - virtual PixelFormat getFormat() const = 0; - - // 获取原始句柄(用于底层渲染) - virtual void* getNativeHandle() const = 0; - - // 是否有效 - virtual bool isValid() const = 0; - - // 设置过滤模式 - virtual void setFilter(bool linear) = 0; - - // 设置环绕模式 - virtual void setWrap(bool repeat) = 0; + // 获取尺寸 + virtual int getWidth() const = 0; + virtual int getHeight() const = 0; + virtual Size size() const = 0; + + // 获取通道数 + virtual int getChannels() const = 0; + + // 获取像素格式 + virtual PixelFormat getFormat() const = 0; + + // 获取原始句柄(用于底层渲染) + virtual void *getNativeHandle() const = 0; + + // 是否有效 + virtual bool isValid() const = 0; + + // 设置过滤模式 + virtual void setFilter(bool linear) = 0; + + // 设置环绕模式 + virtual void setWrap(bool repeat) = 0; }; } // namespace extra2d diff --git a/include/platform/window.h b/include/platform/window.h index 1fb3efd..546db42 100644 --- a/include/platform/window.h +++ b/include/platform/window.h @@ -1,10 +1,11 @@ #pragma once -#include -#include #include +#include +#include #include + #include namespace extra2d { @@ -17,32 +18,33 @@ class Input; // 窗口配置 // ============================================================================ struct WindowConfig { - std::string title = "Extra2D Application"; - int width = 1280; - int height = 720; - bool fullscreen = true; - bool resizable = false; - bool vsync = true; - int msaaSamples = 0; - bool centerWindow = true; - bool enableCursors = true; - bool enableDpiScale = true; - bool fullscreenDesktop = true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN + std::string title = "Extra2D Application"; + int width = 1280; + int height = 720; + bool fullscreen = true; + bool resizable = false; + bool vsync = true; + int msaaSamples = 0; + bool centerWindow = true; + bool enableCursors = true; + bool enableDpiScale = true; + bool fullscreenDesktop = + true; // true: SDL_WINDOW_FULLSCREEN_DESKTOP, false: SDL_WINDOW_FULLSCREEN }; // ============================================================================ // 鼠标光标形状枚举 // ============================================================================ enum class CursorShape { - Arrow, - IBeam, - Crosshair, - Hand, - HResize, - VResize, - ResizeAll, - ResizeNWSE, - ResizeNESW + Arrow, + IBeam, + Crosshair, + Hand, + HResize, + VResize, + ResizeAll, + ResizeNWSE, + ResizeNESW }; // ============================================================================ @@ -51,103 +53,107 @@ enum class CursorShape { // ============================================================================ class Window { public: - Window(); - ~Window(); + Window(); + ~Window(); - // 创建窗口 - bool create(const WindowConfig& config); - void destroy(); + // 创建窗口 + bool create(const WindowConfig &config); + void destroy(); - // 窗口操作 - void pollEvents(); - void swapBuffers(); - bool shouldClose() const; - void setShouldClose(bool close); + // 窗口操作 + void pollEvents(); + void swapBuffers(); + bool shouldClose() const; + void setShouldClose(bool close); - // 窗口属性 - void setTitle(const std::string& title); - void setSize(int width, int height); - void setPosition(int x, int y); - void setFullscreen(bool fullscreen); - void setVSync(bool enabled); - void setResizable(bool resizable); + // 窗口属性 + void setTitle(const std::string &title); + void setSize(int width, int height); + void setPosition(int x, int y); + void setFullscreen(bool fullscreen); + void setVSync(bool enabled); + void setResizable(bool resizable); - // 获取窗口属性 - int getWidth() const { return width_; } - int getHeight() const { return height_; } - Size getSize() const { return Size(static_cast(width_), static_cast(height_)); } - Vec2 pos() const; - bool isFullscreen() const { return fullscreen_; } - bool isVSync() const { return vsync_; } + // 获取窗口属性 + int getWidth() const { return width_; } + int getHeight() const { return height_; } + Size size() const { + return Size(static_cast(width_), static_cast(height_)); + } + Vec2 pos() const; + bool isFullscreen() const { return fullscreen_; } + bool isVSync() const { return vsync_; } - // DPI 缩放 (PC 端自动检测,Switch 固定 1.0) - float getContentScaleX() const; - float getContentScaleY() const; - Vec2 getContentScale() const; + // DPI 缩放 (PC 端自动检测,Switch 固定 1.0) + float getContentScaleX() const; + float getContentScaleY() const; + Vec2 getContentScale() const; - // 窗口状态 - bool isFocused() const { return focused_; } - bool isMinimized() const; - bool isMaximized() const; + // 窗口状态 + bool isFocused() const { return focused_; } + bool isMinimized() const; + bool isMaximized() const; - // 获取 SDL2 窗口和 GL 上下文 - SDL_Window* getSDLWindow() const { return sdlWindow_; } - SDL_GLContext getGLContext() const { return glContext_; } + // 获取 SDL2 窗口和 GL 上下文 + SDL_Window *getSDLWindow() const { return sdlWindow_; } + SDL_GLContext getGLContext() const { return glContext_; } - // 设置/获取用户数据 - void setUserData(void* data) { userData_ = data; } - void* getUserData() const { return userData_; } + // 设置/获取用户数据 + void setUserData(void *data) { userData_ = data; } + void *getUserData() const { return userData_; } - // 事件队列 - void setEventQueue(EventQueue* queue) { eventQueue_ = queue; } - EventQueue* getEventQueue() const { return eventQueue_; } + // 事件队列 + void setEventQueue(EventQueue *queue) { eventQueue_ = queue; } + EventQueue *getEventQueue() const { return eventQueue_; } - // 获取输入管理器 - Input* getInput() const { return input_.get(); } + // 获取输入管理器 + Input *getInput() const { return input_.get(); } - // 光标操作 (PC 端有效,Switch 上为空操作) - void setCursor(CursorShape shape); - void resetCursor(); - void setMouseVisible(bool visible); + // 光标操作 (PC 端有效,Switch 上为空操作) + void setCursor(CursorShape shape); + void resetCursor(); + void setMouseVisible(bool visible); - // 窗口回调 - using ResizeCallback = std::function; - using FocusCallback = std::function; - using CloseCallback = std::function; + // 窗口回调 + using ResizeCallback = std::function; + using FocusCallback = std::function; + using CloseCallback = std::function; - void setResizeCallback(ResizeCallback callback) { resizeCallback_ = callback; } - void setFocusCallback(FocusCallback callback) { focusCallback_ = callback; } - void setCloseCallback(CloseCallback callback) { closeCallback_ = callback; } + void setResizeCallback(ResizeCallback callback) { + resizeCallback_ = callback; + } + void setFocusCallback(FocusCallback callback) { focusCallback_ = callback; } + void setCloseCallback(CloseCallback callback) { closeCallback_ = callback; } private: - // SDL2 状态 - SDL_Window* sdlWindow_; - SDL_GLContext glContext_; - SDL_Cursor* sdlCursors_[9]; // 光标缓存 - SDL_Cursor* currentCursor_; + // SDL2 状态 + SDL_Window *sdlWindow_; + SDL_GLContext glContext_; + SDL_Cursor *sdlCursors_[9]; // 光标缓存 + SDL_Cursor *currentCursor_; - int width_; - int height_; - bool vsync_; - bool shouldClose_; - bool fullscreen_; - bool focused_; - float contentScaleX_; - float contentScaleY_; - bool enableDpiScale_; - void* userData_; - EventQueue* eventQueue_; - UniquePtr input_; + int width_; + int height_; + bool vsync_; + bool shouldClose_; + bool fullscreen_; + bool focused_; + float contentScaleX_; + float contentScaleY_; + bool enableDpiScale_; + void *userData_; + EventQueue *eventQueue_; + UniquePtr input_; - ResizeCallback resizeCallback_; - FocusCallback focusCallback_; - CloseCallback closeCallback_; + ResizeCallback resizeCallback_; + FocusCallback focusCallback_; + CloseCallback closeCallback_; - bool initSDL(const WindowConfig& config); - void deinitSDL(); - void initCursors(); - void deinitCursors(); - void updateContentScale(); + bool initSDL(const WindowConfig &config); + void deinitSDL(); + void initCursors(); + void deinitCursors(); + void updateContentScale(); }; } // namespace extra2d diff --git a/include/scene/node.h b/include/scene/node.h index bacaebe..82d549b 100644 --- a/include/scene/node.h +++ b/include/scene/node.h @@ -5,12 +5,13 @@ #include #include #include -#include #include +#include #include #include #include + namespace extra2d { // 前向声明 @@ -82,7 +83,7 @@ public: * @brief 设置颜色 * @param color RGB颜色 */ - void setColor(const Color3B& color); + void setColor(const Color3B &color); Color3B getColor() const { return color_; } /** @@ -148,7 +149,7 @@ public: // ------------------------------------------------------------------------ // 边界框(用于空间索引) // ------------------------------------------------------------------------ - virtual Rect getBoundingBox() const; + virtual Rect boundingBox() const; // 是否需要参与空间索引(默认 true) void setSpatialIndexed(bool indexed) { spatialIndexed_ = indexed; } @@ -165,17 +166,17 @@ public: // ------------------------------------------------------------------------ // Tween 动画系统 // ------------------------------------------------------------------------ - + /** * @brief 创建 Tween 动画 * @return Tween 对象引用,支持链式调用 */ - Tween& tween(); + Tween &tween(); /** * @brief 创建具名 Tween 动画 */ - Tween& tween(const std::string &name); + Tween &tween(const std::string &name); /** * @brief 移动到目标位置 @@ -216,12 +217,14 @@ public: /** * @brief 淡入(透明度从当前到1) */ - Tween &fadeIn(float duration, TweenEasing easing = static_cast(0)); + Tween &fadeIn(float duration, + TweenEasing easing = static_cast(0)); /** * @brief 淡出(透明度从当前到0) */ - Tween &fadeOut(float duration, TweenEasing easing = static_cast(0)); + Tween &fadeOut(float duration, + TweenEasing easing = static_cast(0)); /** * @brief 透明度变化到目标值 @@ -270,62 +273,62 @@ protected: private: // ========================================================================== // 成员变量按类型大小降序排列,减少内存对齐填充 - // 64位系统对齐:std::string(32) > glm::mat4(64) > std::vector(24) > + // 64位系统对齐:std::string(32) > glm::mat4(64) > std::vector(24) > // double(8) > float(4) > int(4) > bool(1) // ========================================================================== // 1. 大块内存(64字节) - mutable glm::mat4 localTransform_; // 64 bytes - mutable glm::mat4 worldTransform_; // 64 bytes + mutable glm::mat4 localTransform_; // 64 bytes + mutable glm::mat4 worldTransform_; // 64 bytes // 2. 字符串和容器(24-32字节) - std::string name_; // 32 bytes - std::vector> children_; // 24 bytes + std::string name_; // 32 bytes + std::vector> children_; // 24 bytes // 3. 子节点索引(加速查找) std::unordered_map> nameIndex_; // 56 bytes std::unordered_map> tagIndex_; // 56 bytes // 4. 事件分发器 - EventDispatcher eventDispatcher_; // 大小取决于实现 + EventDispatcher eventDispatcher_; // 大小取决于实现 // 5. 父节点引用 - WeakPtr parent_; // 16 bytes + WeakPtr parent_; // 16 bytes // 7. 变换属性(按访问频率分组) - Vec2 position_ = Vec2::Zero(); // 8 bytes - Vec2 scale_ = Vec2(1.0f, 1.0f); // 8 bytes - Vec2 anchor_ = Vec2(0.5f, 0.5f); // 8 bytes - Vec2 skew_ = Vec2::Zero(); // 8 bytes + Vec2 position_ = Vec2::Zero(); // 8 bytes + Vec2 scale_ = Vec2(1.0f, 1.0f); // 8 bytes + Vec2 anchor_ = Vec2(0.5f, 0.5f); // 8 bytes + Vec2 skew_ = Vec2::Zero(); // 8 bytes // 8. 边界框(用于空间索引) - Rect lastSpatialBounds_; // 16 bytes + Rect lastSpatialBounds_; // 16 bytes // 9. 浮点属性 - float rotation_ = 0.0f; // 4 bytes - float opacity_ = 1.0f; // 4 bytes + float rotation_ = 0.0f; // 4 bytes + float opacity_ = 1.0f; // 4 bytes // 10. 颜色属性 Color3B color_ = Color3B(255, 255, 255); // 3 bytes // 11. 整数属性 - int zOrder_ = 0; // 4 bytes - int tag_ = -1; // 4 bytes + int zOrder_ = 0; // 4 bytes + int tag_ = -1; // 4 bytes // 12. 布尔属性 - bool flipX_ = false; // 1 byte - bool flipY_ = false; // 1 byte + bool flipX_ = false; // 1 byte + bool flipY_ = false; // 1 byte // 11. 场景指针 - Scene *scene_ = nullptr; // 8 bytes + Scene *scene_ = nullptr; // 8 bytes // 12. 布尔标志(打包在一起) - mutable bool transformDirty_ = true; // 1 byte - mutable bool worldTransformDirty_ = true; // 1 byte - bool childrenOrderDirty_ = false; // 1 byte - bool visible_ = true; // 1 byte - bool running_ = false; // 1 byte - bool spatialIndexed_ = true; // 1 byte + mutable bool transformDirty_ = true; // 1 byte + mutable bool worldTransformDirty_ = true; // 1 byte + bool childrenOrderDirty_ = false; // 1 byte + bool visible_ = true; // 1 byte + bool running_ = false; // 1 byte + bool spatialIndexed_ = true; // 1 byte // 13. Tween 动画列表 std::vector> tweens_; diff --git a/include/scene/shape_node.h b/include/scene/shape_node.h index 3203fe3..3c7f670 100644 --- a/include/scene/shape_node.h +++ b/include/scene/shape_node.h @@ -85,7 +85,7 @@ public: void addPoint(const Vec2 &point); void clearPoints(); - Rect getBoundingBox() const override; + Rect boundingBox() const override; protected: void onDraw(RenderBackend &renderer) override; diff --git a/include/scene/sprite.h b/include/scene/sprite.h index dc212db..43d55c6 100644 --- a/include/scene/sprite.h +++ b/include/scene/sprite.h @@ -37,7 +37,7 @@ public: static Ptr create(Ptr texture); static Ptr create(Ptr texture, const Rect &rect); - Rect getBoundingBox() const override; + Rect boundingBox() const override; protected: void onDraw(RenderBackend &renderer) override; diff --git a/include/ui/button.h b/include/ui/button.h index d98c5e4..cc87669 100644 --- a/include/ui/button.h +++ b/include/ui/button.h @@ -178,7 +178,7 @@ public: */ void setStateTextColor(const Color &colorOff, const Color &colorOn); - Rect getBoundingBox() const override; + Rect boundingBox() const override; protected: void onDrawWidget(RenderBackend &renderer) override; diff --git a/include/ui/check_box.h b/include/ui/check_box.h index 9a38520..1ad3b31 100644 --- a/include/ui/check_box.h +++ b/include/ui/check_box.h @@ -11,88 +11,88 @@ namespace extra2d { // ============================================================================ class CheckBox : public Widget { public: - CheckBox(); - ~CheckBox() override = default; + CheckBox(); + ~CheckBox() override = default; - static Ptr create(); - static Ptr create(const std::string &label); + static Ptr create(); + static Ptr create(const std::string &label); - // ------------------------------------------------------------------------ - // 选中状态 - // ------------------------------------------------------------------------ - void setChecked(bool checked); - bool isChecked() const { return checked_; } - void toggle(); + // ------------------------------------------------------------------------ + // 选中状态 + // ------------------------------------------------------------------------ + void setChecked(bool checked); + bool isChecked() const { return checked_; } + void toggle(); - // ------------------------------------------------------------------------ - // 标签设置 - // ------------------------------------------------------------------------ - void setLabel(const std::string &label); - const std::string &getLabel() const { return label_; } + // ------------------------------------------------------------------------ + // 标签设置 + // ------------------------------------------------------------------------ + void setLabel(const std::string &label); + const std::string &getLabel() const { return label_; } - // ------------------------------------------------------------------------ - // 字体设置 - // ------------------------------------------------------------------------ - void setFont(Ptr font); - Ptr getFont() const { return font_; } + // ------------------------------------------------------------------------ + // 字体设置 + // ------------------------------------------------------------------------ + void setFont(Ptr font); + Ptr getFont() const { return font_; } - // ------------------------------------------------------------------------ - // 文字颜色 - // ------------------------------------------------------------------------ - void setTextColor(const Color &color); - Color getTextColor() const { return textColor_; } + // ------------------------------------------------------------------------ + // 文字颜色 + // ------------------------------------------------------------------------ + void setTextColor(const Color &color); + Color getTextColor() const { return textColor_; } - // ------------------------------------------------------------------------ - // 复选框尺寸 - // ------------------------------------------------------------------------ - void setBoxSize(float size); - float getBoxSize() const { return boxSize_; } + // ------------------------------------------------------------------------ + // 复选框尺寸 + // ------------------------------------------------------------------------ + void setBoxSize(float size); + float getBoxSize() const { return boxSize_; } - // ------------------------------------------------------------------------ - // 间距 - // ------------------------------------------------------------------------ - void setSpacing(float spacing); - float getSpacing() const { return spacing_; } + // ------------------------------------------------------------------------ + // 间距 + // ------------------------------------------------------------------------ + void setSpacing(float spacing); + float getSpacing() const { return spacing_; } - // ------------------------------------------------------------------------ - // 颜色设置 - // ------------------------------------------------------------------------ - void setCheckedColor(const Color &color); - Color getCheckedColor() const { return checkedColor_; } + // ------------------------------------------------------------------------ + // 颜色设置 + // ------------------------------------------------------------------------ + void setCheckedColor(const Color &color); + Color getCheckedColor() const { return checkedColor_; } - void setUncheckedColor(const Color &color); - Color getUncheckedColor() const { return uncheckedColor_; } + void setUncheckedColor(const Color &color); + Color getUncheckedColor() const { return uncheckedColor_; } - void setCheckMarkColor(const Color &color); - Color getCheckMarkColor() const { return checkMarkColor_; } + void setCheckMarkColor(const Color &color); + Color getCheckMarkColor() const { return checkMarkColor_; } - // ------------------------------------------------------------------------ - // 回调设置 - // ------------------------------------------------------------------------ - void setOnStateChange(Function callback); + // ------------------------------------------------------------------------ + // 回调设置 + // ------------------------------------------------------------------------ + void setOnStateChange(Function callback); - Rect getBoundingBox() const override; + Rect boundingBox() const override; protected: - void onDrawWidget(RenderBackend &renderer) override; - bool onMousePress(const MouseEvent &event) override; - bool onMouseRelease(const MouseEvent &event) override; + void onDrawWidget(RenderBackend &renderer) override; + bool onMousePress(const MouseEvent &event) override; + bool onMouseRelease(const MouseEvent &event) override; private: - bool checked_ = false; - std::string label_; - Ptr font_; - Color textColor_ = Colors::White; - - float boxSize_ = 20.0f; - float spacing_ = 8.0f; - - Color checkedColor_ = Color(0.2f, 0.6f, 1.0f, 1.0f); - Color uncheckedColor_ = Color(0.3f, 0.3f, 0.3f, 1.0f); - Color checkMarkColor_ = Colors::White; - - bool pressed_ = false; - Function onStateChange_; + bool checked_ = false; + std::string label_; + Ptr font_; + Color textColor_ = Colors::White; + + float boxSize_ = 20.0f; + float spacing_ = 8.0f; + + Color checkedColor_ = Color(0.2f, 0.6f, 1.0f, 1.0f); + Color uncheckedColor_ = Color(0.3f, 0.3f, 0.3f, 1.0f); + Color checkMarkColor_ = Colors::White; + + bool pressed_ = false; + Function onStateChange_; }; } // namespace extra2d diff --git a/include/ui/label.h b/include/ui/label.h index a27c68b..97cd420 100644 --- a/include/ui/label.h +++ b/include/ui/label.h @@ -12,135 +12,136 @@ namespace extra2d { // ============================================================================ class Label : public Widget { public: - Label(); - explicit Label(const std::string &text); - ~Label() override = default; + Label(); + explicit Label(const std::string &text); + ~Label() override = default; - // ------------------------------------------------------------------------ - // 静态创建方法 - // ------------------------------------------------------------------------ - static Ptr