From 9e911db53cf63a41f930af1c3dd9257438574b59 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Fri, 20 Feb 2026 15:04:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B8=B2=E6=9F=93=E5=99=A8):=20?= =?UTF-8?q?=E5=B0=86RenderBackend=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BARende?= =?UTF-8?q?rer=E5=B9=B6=E9=87=8D=E6=9E=84=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构渲染器接口命名,将RenderBackend统一更名为Renderer,同时更新所有相关文件和文档引用。此变更旨在提供更简洁清晰的接口命名,并保持代码一致性。 - 重命名RenderBackend类为Renderer - 更新所有相关文件中的类型引用和文档 - 保持原有功能不变,仅进行命名和结构调整 - 修复因重命名导致的编译错误和警告 --- Extra2D/include/extra2d/app/application.h | 4 +- Extra2D/include/extra2d/extra2d.h | 2 +- .../extra2d/graphics/core/render_backend.h | 125 ---- .../extra2d/graphics/core/render_module.h | 89 ++- .../extra2d/graphics/opengl/gl_renderer.h | 168 ++--- .../extra2d/graphics/texture/texture_pool.h | 4 +- Extra2D/include/extra2d/scene/node.h | 11 +- Extra2D/include/extra2d/scene/scene.h | 6 +- Extra2D/include/extra2d/scene/scene_manager.h | 5 +- Extra2D/include/extra2d/scene/shape_node.h | 2 +- Extra2D/include/extra2d/scene/sprite.h | 2 +- .../extra2d/scene/transition_box_scene.h | 2 +- .../extra2d/scene/transition_fade_scene.h | 6 +- .../extra2d/scene/transition_flip_scene.h | 2 +- .../extra2d/scene/transition_scale_scene.h | 2 +- .../include/extra2d/scene/transition_scene.h | 16 +- .../extra2d/scene/transition_slide_scene.h | 2 +- .../include/extra2d/services/scene_service.h | 109 +-- Extra2D/src/app/application.cpp | 9 +- Extra2D/src/graphics/core/render_module.cpp | 2 +- Extra2D/src/graphics/opengl/gl_renderer.cpp | 123 ++- Extra2D/src/graphics/texture/texture_pool.cpp | 699 +++++++++--------- Extra2D/src/scene/node.cpp | 8 +- Extra2D/src/scene/scene.cpp | 7 +- Extra2D/src/scene/scene_manager.cpp | 55 +- Extra2D/src/scene/shape_node.cpp | 44 +- Extra2D/src/scene/sprite.cpp | 8 +- Extra2D/src/scene/transition_box_scene.cpp | 17 +- Extra2D/src/scene/transition_fade_scene.cpp | 4 +- Extra2D/src/scene/transition_flip_scene.cpp | 21 +- Extra2D/src/scene/transition_scale_scene.cpp | 18 +- Extra2D/src/scene/transition_scene.cpp | 8 +- Extra2D/src/scene/transition_slide_scene.cpp | 23 +- Extra2D/src/services/scene_service.cpp | 4 +- README.md | 2 +- docs/module_system.md | 8 +- examples/basic/main.cpp | 6 +- examples/hello_module/main.cpp | 6 +- examples/image_display/main.cpp | 18 +- examples/text_rendering/main.cpp | 16 +- 40 files changed, 749 insertions(+), 914 deletions(-) delete mode 100644 Extra2D/include/extra2d/graphics/core/render_backend.h diff --git a/Extra2D/include/extra2d/app/application.h b/Extra2D/include/extra2d/app/application.h index 6412a13..14a710f 100644 --- a/Extra2D/include/extra2d/app/application.h +++ b/Extra2D/include/extra2d/app/application.h @@ -9,7 +9,7 @@ namespace extra2d { class GLFWWindow; -class RenderBackend; +class Renderer; class WindowModule; class RenderModule; @@ -91,7 +91,7 @@ public: * @brief 获取渲染器 * @return 渲染器指针 */ - RenderBackend *renderer(); + Renderer *renderer(); /** * @brief 进入场景 diff --git a/Extra2D/include/extra2d/extra2d.h b/Extra2D/include/extra2d/extra2d.h index 3b22b54..d86f7d0 100644 --- a/Extra2D/include/extra2d/extra2d.h +++ b/Extra2D/include/extra2d/extra2d.h @@ -20,10 +20,10 @@ // Graphics #include #include -#include #include #include #include +#include #include #include #include diff --git a/Extra2D/include/extra2d/graphics/core/render_backend.h b/Extra2D/include/extra2d/graphics/core/render_backend.h deleted file mode 100644 index eb9a1b2..0000000 --- a/Extra2D/include/extra2d/graphics/core/render_backend.h +++ /dev/null @@ -1,125 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace extra2d { - -// 前向声明 -class GLFWWindow; -class Texture; -class FontAtlas; -class Shader; - -// BlendMode 定义在 pipeline.h 中 - -// ============================================================================ -// 渲染后端抽象接口 -// ============================================================================ -class RenderBackend { -public: - virtual ~RenderBackend() = default; - - // ------------------------------------------------------------------------ - // 生命周期 - // ------------------------------------------------------------------------ - virtual bool init(GLFWWindow* window) = 0; - virtual void shutdown() = 0; - - // ------------------------------------------------------------------------ - // 帧管理 - // ------------------------------------------------------------------------ - virtual void beginFrame(const Color &clearColor) = 0; - virtual void endFrame() = 0; - virtual void setViewport(int x, int y, int width, int height) = 0; - virtual void setVSync(bool enabled) = 0; - - // ------------------------------------------------------------------------ - // 状态设置 - // ------------------------------------------------------------------------ - virtual void setBlendMode(BlendMode mode) = 0; - virtual void setViewProjection(const glm::mat4 &matrix) = 0; - - // ------------------------------------------------------------------------ - // 变换矩阵栈 - // ------------------------------------------------------------------------ - virtual void pushTransform(const glm::mat4 &transform) = 0; - virtual void popTransform() = 0; - virtual glm::mat4 getCurrentTransform() const = 0; - - // ------------------------------------------------------------------------ - // 纹理 - // ------------------------------------------------------------------------ - virtual Ptr createTexture(int width, int height, - const uint8_t *pixels, int channels) = 0; - virtual Ptr loadTexture(const std::string &filepath) = 0; - - // ------------------------------------------------------------------------ - // 精灵批渲染 - // ------------------------------------------------------------------------ - /** - * @brief 开始手动精灵批处理(高级用法) - * @note 一般情况下不需要调用,drawSprite/drawText 会自动管理批处理 - */ - virtual void beginSpriteBatch() = 0; - virtual void drawSprite(const Texture &texture, const Rect &destRect, - const Rect &srcRect, const Color &tint, - float rotation, const Vec2 &anchor) = 0; - virtual void drawSprite(const Texture &texture, const Vec2 &position, - const Color &tint) = 0; - virtual void endSpriteBatch() = 0; - - /** - * @brief 立即提交当前批处理 - * @note 手动控制批处理提交时机,一般情况下不需要调用 - */ - virtual void flush() = 0; - - // ------------------------------------------------------------------------ - // 形状渲染 - // ------------------------------------------------------------------------ - virtual void drawLine(const Vec2 &start, const Vec2 &end, const Color &color, - float width = 1.0f) = 0; - virtual void drawRect(const Rect &rect, const Color &color, - float width = 1.0f) = 0; - virtual void fillRect(const Rect &rect, const Color &color) = 0; - virtual void drawCircle(const Vec2 ¢er, float radius, const Color &color, - int segments = 32, float width = 1.0f) = 0; - virtual void fillCircle(const Vec2 ¢er, float radius, const Color &color, - int segments = 32) = 0; - virtual void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, - const Color &color, float width = 1.0f) = 0; - virtual void fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, - const Color &color) = 0; - virtual void drawPolygon(const std::vector &points, const Color &color, - float width = 1.0f) = 0; - virtual void fillPolygon(const std::vector &points, - const Color &color) = 0; - - // ------------------------------------------------------------------------ - // 文字渲染 - // ------------------------------------------------------------------------ - virtual Ptr createFontAtlas(const std::string &filepath, - int fontSize, bool useSDF = false) = 0; - virtual void drawText(const FontAtlas &font, const std::string &text, - const Vec2 &position, const Color &color) = 0; - virtual void drawText(const FontAtlas &font, const std::string &text, float x, - float y, const Color &color) = 0; - - // ------------------------------------------------------------------------ - // 统计信息 - // ------------------------------------------------------------------------ - struct Stats { - uint32_t drawCalls = 0; - uint32_t triangleCount = 0; - uint32_t textureBinds = 0; - uint32_t shaderBinds = 0; - }; - virtual Stats getStats() const = 0; - virtual void resetStats() = 0; -}; - -} // namespace extra2d diff --git a/Extra2D/include/extra2d/graphics/core/render_module.h b/Extra2D/include/extra2d/graphics/core/render_module.h index 84cb08c..005916b 100644 --- a/Extra2D/include/extra2d/graphics/core/render_module.h +++ b/Extra2D/include/extra2d/graphics/core/render_module.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include @@ -12,67 +12,62 @@ namespace extra2d { * @brief 渲染模块配置结构 */ struct RenderCfg { - int targetFPS; - bool vsync; - int multisamples; - int priority; + int targetFPS; + bool vsync; + int multisamples; + int priority; - RenderCfg() - : targetFPS(60) - , vsync(true) - , multisamples(0) - , priority(10) - {} + RenderCfg() : targetFPS(60), vsync(true), multisamples(0), priority(10) {} }; /** * @brief 渲染模块 - * 管理 OpenGL 渲染后端 + * 管理 OpenGL 渲染器 */ class RenderModule : public Module { public: - /** - * @brief 构造函数(Lambda 配置) - * @param configFn 配置函数 - */ - explicit RenderModule(std::function configFn); + /** + * @brief 构造函数(Lambda 配置) + * @param configFn 配置函数 + */ + explicit RenderModule(std::function configFn); - /** - * @brief 析构函数 - */ - ~RenderModule() override; + /** + * @brief 析构函数 + */ + ~RenderModule() override; - bool init() override; - void shutdown() override; - bool ok() const override { return initialized_; } - const char* name() const override { return "render"; } - int priority() const override { return cfg_.priority; } + bool init() override; + void shutdown() override; + bool ok() const override { return initialized_; } + const char *name() const override { return "render"; } + int priority() const override { return cfg_.priority; } - /** - * @brief 获取依赖 - * @return 依赖模块类型列表 - */ - std::vector deps() const override { - return {std::type_index(typeid(WindowModule))}; - } + /** + * @brief 获取依赖 + * @return 依赖模块类型列表 + */ + std::vector deps() const override { + return {std::type_index(typeid(WindowModule))}; + } - /** - * @brief 是否允许并行初始化 - * RenderModule 需要 OpenGL 上下文,必须在主线程初始化 - * @return 不允许并行初始化返回 false - */ - bool allowParallelInit() const override { return false; } + /** + * @brief 是否允许并行初始化 + * RenderModule 需要 OpenGL 上下文,必须在主线程初始化 + * @return 不允许并行初始化返回 false + */ + bool allowParallelInit() const override { return false; } - /** - * @brief 获取渲染器 - * @return 渲染后端指针 - */ - RenderBackend* renderer() const { return renderer_.get(); } + /** + * @brief 获取渲染器 + * @return 渲染器指针 + */ + Renderer *renderer() const { return renderer_.get(); } private: - RenderCfg cfg_; - UniquePtr renderer_; - bool initialized_ = false; + RenderCfg cfg_; + UniquePtr renderer_; + bool initialized_ = false; }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/graphics/opengl/gl_renderer.h b/Extra2D/include/extra2d/graphics/opengl/gl_renderer.h index cfcf4ba..0d4fc9a 100644 --- a/Extra2D/include/extra2d/graphics/opengl/gl_renderer.h +++ b/Extra2D/include/extra2d/graphics/opengl/gl_renderer.h @@ -1,132 +1,111 @@ #pragma once -#include +#include +#include +#include #include #include #include #include +#include #include - #include #include +#include #include namespace extra2d { -// 前向声明 class GLFWWindow; class GLContext; class GLFramebuffer; +class FontAtlas; -// ============================================================================ -// OpenGL 渲染器实现 -// ============================================================================ -class GLRenderer : public RenderBackend { +/** + * @brief 渲染统计信息 + */ +struct RenderStats { + uint32_t drawCalls = 0; + uint32_t triangleCount = 0; + uint32_t textureBinds = 0; + uint32_t shaderBinds = 0; +}; + +/** + * @brief OpenGL 渲染器实现 + */ +class Renderer { public: - GLRenderer(); - ~GLRenderer() override; + Renderer(); + ~Renderer(); - // RenderBackend 接口实现 - bool init(GLFWWindow *window) override; - void shutdown() override; + bool init(GLFWWindow *window); + void shutdown(); - void beginFrame(const Color &clearColor) override; - void endFrame() override; - void setViewport(int x, int y, int width, int height) override; - void setVSync(bool enabled) override; + void beginFrame(const Color &clearColor); + void endFrame(); + void setViewport(int x, int y, int width, int height); + void setVSync(bool enabled); - void setBlendMode(BlendMode mode) override; - void setViewProjection(const glm::mat4 &matrix) override; + void setBlendMode(BlendMode mode); + void setViewProjection(const glm::mat4 &matrix); - // 变换矩阵栈 - void pushTransform(const glm::mat4 &transform) override; - void popTransform() override; - glm::mat4 getCurrentTransform() const override; + void pushTransform(const glm::mat4 &transform); + void popTransform(); + glm::mat4 getCurrentTransform() const; Ptr createTexture(int width, int height, const uint8_t *pixels, - int channels) override; - Ptr loadTexture(const std::string &filepath) override; + int channels); + Ptr loadTexture(const std::string &filepath); - void beginSpriteBatch() override; + void beginSpriteBatch(); void drawSprite(const Texture &texture, const Rect &destRect, const Rect &srcRect, const Color &tint, float rotation, - const Vec2 &anchor) override; + const Vec2 &anchor); void drawSprite(const Texture &texture, const Vec2 &position, - const Color &tint) override; - void endSpriteBatch() override; - void flush() override; + const Color &tint); + void endSpriteBatch(); + void flush(); void drawLine(const Vec2 &start, const Vec2 &end, const Color &color, - float width) override; - void drawRect(const Rect &rect, const Color &color, float width) override; - void fillRect(const Rect &rect, const Color &color) override; + float width = 1.0f); + void drawRect(const Rect &rect, const Color &color, float width = 1.0f); + void fillRect(const Rect &rect, const Color &color); void drawCircle(const Vec2 ¢er, float radius, const Color &color, - int segments, float width) override; + int segments = 32, float width = 1.0f); void fillCircle(const Vec2 ¢er, float radius, const Color &color, - int segments) override; + int segments = 32); void drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, - const Color &color, float width) override; + const Color &color, float width = 1.0f); void fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, - const Color &color) override; + const Color &color); void drawPolygon(const std::vector &points, const Color &color, - float width) override; - void fillPolygon(const std::vector &points, - const Color &color) override; + float width = 1.0f); + void fillPolygon(const std::vector &points, const Color &color); Ptr createFontAtlas(const std::string &filepath, int fontSize, - bool useSDF = false) override; + bool useSDF = false); void drawText(const FontAtlas &font, const std::string &text, - const Vec2 &position, const Color &color) override; + const Vec2 &position, const Color &color); void drawText(const FontAtlas &font, const std::string &text, float x, - float y, const Color &color) override; + float y, const Color &color); - Stats getStats() const override { return stats_; } - void resetStats() override; + RenderStats getStats() const { return stats_; } + void resetStats(); - // GLFramebuffer 相关方法 - - /** - * @brief 创建帧缓冲对象 - * @param desc 帧缓冲描述 - * @return 创建的帧缓冲智能指针 - */ Ptr createFramebuffer(const FramebufferDesc &desc); - - /** - * @brief 绑定帧缓冲(作为渲染目标) - * @param framebuffer 帧缓冲对象指针,传入 nullptr 则绑定默认帧缓冲 - */ void bindFramebuffer(GLFramebuffer *framebuffer); - - /** - * @brief 解绑帧缓冲(恢复到默认帧缓冲) - */ void unbindFramebuffer(); - - /** - * @brief 获取默认帧缓冲 - * @return 默认帧缓冲智能指针 - */ Ptr getDefaultFramebuffer() const; - - /** - * @brief 清除当前绑定的帧缓冲 - * @param color 清除颜色 - * @param clearColor 是否清除颜色缓冲 - * @param clearDepth 是否清除深度缓冲 - * @param clearStencil 是否清除模板缓冲 - */ void clearFramebuffer(const Color &color, bool clearColor = true, bool clearDepth = true, bool clearStencil = false); private: - // 形状批处理常量 static constexpr size_t MAX_CIRCLE_SEGMENTS = 128; - static constexpr size_t MAX_SHAPE_VERTICES = 8192; // 最大形状顶点数 - static constexpr size_t MAX_LINE_VERTICES = 16384; // 最大线条顶点数 + static constexpr size_t MAX_SHAPE_VERTICES = 8192; + static constexpr size_t MAX_LINE_VERTICES = 16384; - // 形状顶点结构(包含颜色) struct ShapeVertex { float x, y; float r, g, b, a; @@ -135,45 +114,40 @@ private: GLFWWindow *window_; GLSpriteBatch spriteBatch_; Ptr shapeShader_; - Ptr sdfFontShader_; // SDF字体专用着色器 + Ptr sdfFontShader_; - GLuint shapeVao_; // 形状 VAO(手动管理,用于顶点属性配置) - GLBuffer shapeBuffer_; // 形状 VBO(使用 GLBuffer 管理) - GLuint lineVao_; // 线条 VAO(手动管理,用于顶点属性配置) - GLBuffer lineBuffer_; // 线条 VBO(使用 GLBuffer 管理) + GLuint shapeVao_; + GLBuffer shapeBuffer_; + GLuint lineVao_; + GLBuffer lineBuffer_; glm::mat4 viewProjection_; std::vector transformStack_; - Stats stats_; + RenderStats stats_; bool vsync_; - // 形状批处理缓冲区(预分配,避免每帧内存分配) std::array shapeVertexCache_; size_t shapeVertexCount_ = 0; GLenum currentShapeMode_ = GL_TRIANGLES; - // 线条批处理缓冲区 std::array lineVertexCache_; size_t lineVertexCount_ = 0; float currentLineWidth_ = 1.0f; - // OpenGL 管线状态管理 GLPipeline pipeline_; - // 自动批处理状态 - bool batchActive_ = false; // 批处理是否激活 - bool autoBatchEnabled_ = true; // 是否启用自动批处理 - const Texture *currentBatchTexture_ = nullptr; // 当前批处理的纹理 - std::vector pendingSprites_; // 待提交的精灵 - static constexpr size_t MAX_BATCH_SPRITES = 1000; // 最大批处理精灵数 + bool batchActive_ = false; + bool autoBatchEnabled_ = true; + const Texture *currentBatchTexture_ = nullptr; + std::vector pendingSprites_; + static constexpr size_t MAX_BATCH_SPRITES = 1000; - // 帧缓冲管理 - mutable Ptr defaultFramebuffer_; // 默认帧缓冲(延迟创建) - GLFramebuffer *currentFramebuffer_ = nullptr; // 当前绑定的帧缓冲 + mutable Ptr defaultFramebuffer_; + GLFramebuffer *currentFramebuffer_ = nullptr; void initShapeRendering(); - void ensureBatchActive(); // 确保批处理已激活 - void submitPendingSprites(); // 提交待处理的精灵 + void ensureBatchActive(); + void submitPendingSprites(); void flushShapeBatch(); void flushLineBatch(); void addShapeVertex(float x, float y, const Color &color); diff --git a/Extra2D/include/extra2d/graphics/texture/texture_pool.h b/Extra2D/include/extra2d/graphics/texture/texture_pool.h index e2f9178..6f3f9f9 100644 --- a/Extra2D/include/extra2d/graphics/texture/texture_pool.h +++ b/Extra2D/include/extra2d/graphics/texture/texture_pool.h @@ -13,12 +13,10 @@ #include #include - namespace extra2d { -// 前向声明 class Scene; -class RenderBackend; +class Renderer; // ============================================================================ // 纹理加载选项 diff --git a/Extra2D/include/extra2d/scene/node.h b/Extra2D/include/extra2d/scene/node.h index 45ec042..ec42d51 100644 --- a/Extra2D/include/extra2d/scene/node.h +++ b/Extra2D/include/extra2d/scene/node.h @@ -4,16 +4,15 @@ #include #include #include -#include +#include #include #include #include namespace extra2d { -// 前向声明 class Scene; -class RenderBackend; +class Renderer; struct RenderCommand; // ============================================================================ @@ -137,7 +136,7 @@ public: virtual void onEnter(); virtual void onExit(); virtual void onUpdate(float dt); - virtual void onRender(RenderBackend &renderer); + virtual void onRender(Renderer &renderer); virtual void onAttachToScene(Scene *scene); virtual void onDetachFromScene(); @@ -155,7 +154,7 @@ public: // 内部方法 // ------------------------------------------------------------------------ void update(float dt); - void render(RenderBackend &renderer); + void render(Renderer &renderer); void sortChildren(); bool isRunning() const { return running_; } @@ -167,7 +166,7 @@ public: protected: // 子类重写 - virtual void onDraw(RenderBackend &renderer) {} + virtual void onDraw(Renderer &renderer) {} virtual void onUpdateNode(float dt) {} virtual void generateRenderCommand(std::vector &commands, int zOrder) {}; diff --git a/Extra2D/include/extra2d/scene/scene.h b/Extra2D/include/extra2d/scene/scene.h index aad87cf..6a50bf0 100644 --- a/Extra2D/include/extra2d/scene/scene.h +++ b/Extra2D/include/extra2d/scene/scene.h @@ -54,11 +54,11 @@ public: // ------------------------------------------------------------------------ // 渲染和更新 // ------------------------------------------------------------------------ - void renderScene(RenderBackend &renderer); - virtual void renderContent(RenderBackend &renderer); + void renderScene(Renderer &renderer); + virtual void renderContent(Renderer &renderer); void updateScene(float dt); void collectRenderCommands(std::vector &commands, - int parentZOrder = 0) override; + int parentZOrder = 0) override; // ------------------------------------------------------------------------ // 静态创建方法 diff --git a/Extra2D/include/extra2d/scene/scene_manager.h b/Extra2D/include/extra2d/scene/scene_manager.h index fc8341b..bc35cc5 100644 --- a/Extra2D/include/extra2d/scene/scene_manager.h +++ b/Extra2D/include/extra2d/scene/scene_manager.h @@ -102,7 +102,7 @@ public: // 更新和渲染 // ------------------------------------------------------------------------ void update(float dt); - void render(RenderBackend &renderer); + void render(Renderer &renderer); void collectRenderCommands(std::vector &commands); // ------------------------------------------------------------------------ @@ -143,7 +143,8 @@ private: * @param inScene 目标场景 * @return 过渡场景智能指针 */ - Ptr createTransitionScene(TransitionType type, float duration, + Ptr createTransitionScene(TransitionType type, + float duration, Ptr inScene); /** diff --git a/Extra2D/include/extra2d/scene/shape_node.h b/Extra2D/include/extra2d/scene/shape_node.h index e0af481..fa594fb 100644 --- a/Extra2D/include/extra2d/scene/shape_node.h +++ b/Extra2D/include/extra2d/scene/shape_node.h @@ -88,7 +88,7 @@ public: Rect getBounds() const override; protected: - void onDraw(RenderBackend &renderer) override; + void onDraw(Renderer &renderer) override; void generateRenderCommand(std::vector &commands, int zOrder) override; diff --git a/Extra2D/include/extra2d/scene/sprite.h b/Extra2D/include/extra2d/scene/sprite.h index 37c48ea..550d256 100644 --- a/Extra2D/include/extra2d/scene/sprite.h +++ b/Extra2D/include/extra2d/scene/sprite.h @@ -40,7 +40,7 @@ public: Rect getBounds() const override; protected: - void onDraw(RenderBackend &renderer) override; + void onDraw(Renderer &renderer) override; void generateRenderCommand(std::vector &commands, int zOrder) override; diff --git a/Extra2D/include/extra2d/scene/transition_box_scene.h b/Extra2D/include/extra2d/scene/transition_box_scene.h index c48b481..a65c95a 100644 --- a/Extra2D/include/extra2d/scene/transition_box_scene.h +++ b/Extra2D/include/extra2d/scene/transition_box_scene.h @@ -25,7 +25,7 @@ public: protected: void onTransitionStart() override; - void renderContent(RenderBackend &renderer) override; + void renderContent(Renderer &renderer) override; void updateTransition(float dt) override; private: diff --git a/Extra2D/include/extra2d/scene/transition_fade_scene.h b/Extra2D/include/extra2d/scene/transition_fade_scene.h index 541bf26..0953024 100644 --- a/Extra2D/include/extra2d/scene/transition_fade_scene.h +++ b/Extra2D/include/extra2d/scene/transition_fade_scene.h @@ -44,7 +44,7 @@ protected: * @brief 渲染内容 * 根据进度控制新旧场景的显示 */ - void renderContent(RenderBackend &renderer) override; + void renderContent(Renderer &renderer) override; private: /** @@ -52,8 +52,8 @@ private: */ void hideOutShowIn(); - Color maskColor_; // 遮罩颜色 - bool hasSwitched_ = false; // 是否已经切换场景 + Color maskColor_; // 遮罩颜色 + bool hasSwitched_ = false; // 是否已经切换场景 }; } // namespace extra2d diff --git a/Extra2D/include/extra2d/scene/transition_flip_scene.h b/Extra2D/include/extra2d/scene/transition_flip_scene.h index 116529e..ba19a7e 100644 --- a/Extra2D/include/extra2d/scene/transition_flip_scene.h +++ b/Extra2D/include/extra2d/scene/transition_flip_scene.h @@ -28,7 +28,7 @@ public: protected: void onTransitionStart() override; - void renderContent(RenderBackend &renderer) override; + void renderContent(Renderer &renderer) override; void updateTransition(float dt) override; private: diff --git a/Extra2D/include/extra2d/scene/transition_scale_scene.h b/Extra2D/include/extra2d/scene/transition_scale_scene.h index 59628a7..bda95d1 100644 --- a/Extra2D/include/extra2d/scene/transition_scale_scene.h +++ b/Extra2D/include/extra2d/scene/transition_scale_scene.h @@ -23,7 +23,7 @@ public: protected: void onTransitionStart() override; - void renderContent(RenderBackend &renderer) override; + void renderContent(Renderer &renderer) override; void updateTransition(float dt) override; }; diff --git a/Extra2D/include/extra2d/scene/transition_scene.h b/Extra2D/include/extra2d/scene/transition_scene.h index 1e15e47..d384efc 100644 --- a/Extra2D/include/extra2d/scene/transition_scene.h +++ b/Extra2D/include/extra2d/scene/transition_scene.h @@ -63,7 +63,9 @@ public: /** * @brief 设置过渡完成回调 */ - void setFinishCallback(FinishCallback callback) { finishCallback_ = callback; } + void setFinishCallback(FinishCallback callback) { + finishCallback_ = callback; + } /** * @brief 获取过渡持续时间 @@ -99,7 +101,7 @@ public: // ------------------------------------------------------------------------ // 渲染 - 在 TransitionScene 上渲染新旧两个子场景 // ------------------------------------------------------------------------ - void renderContent(RenderBackend &renderer) override; + void renderContent(Renderer &renderer) override; // ------------------------------------------------------------------------ // 生命周期 @@ -117,12 +119,12 @@ protected: /** * @brief 绘制源场景(旧场景) */ - virtual void drawOutScene(RenderBackend &renderer); + virtual void drawOutScene(Renderer &renderer); /** * @brief 绘制目标场景(新场景) */ - virtual void drawInScene(RenderBackend &renderer); + virtual void drawInScene(Renderer &renderer); /** * @brief 更新过渡进度(子类重写此方法更新动画) @@ -136,11 +138,11 @@ protected: bool isFinished_ = false; bool isCancelled_ = false; - Ptr inScene_; // 要进入的场景 - Ptr outScene_; // 要退出的场景 + Ptr inScene_; // 要进入的场景 + Ptr outScene_; // 要退出的场景 FinishCallback finishCallback_; - FinishCallback cancelCallback_; // 取消回调 + FinishCallback cancelCallback_; // 取消回调 friend class SceneManager; }; diff --git a/Extra2D/include/extra2d/scene/transition_slide_scene.h b/Extra2D/include/extra2d/scene/transition_slide_scene.h index 7457c64..e848620 100644 --- a/Extra2D/include/extra2d/scene/transition_slide_scene.h +++ b/Extra2D/include/extra2d/scene/transition_slide_scene.h @@ -26,7 +26,7 @@ public: protected: void onTransitionStart() override; - void renderContent(RenderBackend &renderer) override; + void renderContent(Renderer &renderer) override; void updateTransition(float dt) override; private: diff --git a/Extra2D/include/extra2d/services/scene_service.h b/Extra2D/include/extra2d/services/scene_service.h index 0f30608..bca3e7d 100644 --- a/Extra2D/include/extra2d/services/scene_service.h +++ b/Extra2D/include/extra2d/services/scene_service.h @@ -2,6 +2,7 @@ #include #include +#include #include namespace extra2d { @@ -12,33 +13,34 @@ namespace extra2d { */ class ISceneService : public IService { public: - virtual ~ISceneService() = default; + virtual ~ISceneService() = default; - virtual void runWithScene(Ptr scene) = 0; - virtual void replaceScene(Ptr scene) = 0; - virtual void pushScene(Ptr scene) = 0; - virtual void popScene() = 0; - virtual void popToRootScene() = 0; - virtual void popToScene(const std::string& name) = 0; + virtual void runWithScene(Ptr scene) = 0; + virtual void replaceScene(Ptr scene) = 0; + virtual void pushScene(Ptr scene) = 0; + virtual void popScene() = 0; + virtual void popToRootScene() = 0; + virtual void popToScene(const std::string &name) = 0; - virtual Ptr getCurrentScene() const = 0; - virtual Ptr getPreviousScene() const = 0; - virtual Ptr getRootScene() const = 0; - virtual Ptr getSceneByName(const std::string& name) const = 0; + virtual Ptr getCurrentScene() const = 0; + virtual Ptr getPreviousScene() const = 0; + virtual Ptr getRootScene() const = 0; + virtual Ptr getSceneByName(const std::string &name) const = 0; - virtual size_t getSceneCount() const = 0; - virtual bool isEmpty() const = 0; - virtual bool hasScene(const std::string& name) const = 0; + virtual size_t getSceneCount() const = 0; + virtual bool isEmpty() const = 0; + virtual bool hasScene(const std::string &name) const = 0; - virtual void render(RenderBackend& renderer) = 0; - virtual void collectRenderCommands(std::vector& commands) = 0; + virtual void render(Renderer &renderer) = 0; + virtual void collectRenderCommands(std::vector &commands) = 0; - virtual bool isTransitioning() const = 0; - virtual void setTransitionCallback(SceneManager::TransitionCallback callback) = 0; + virtual bool isTransitioning() const = 0; + virtual void + setTransitionCallback(SceneManager::TransitionCallback callback) = 0; - virtual void end() = 0; - virtual void purgeCachedScenes() = 0; - virtual void enterScene(Ptr scene) = 0; + virtual void end() = 0; + virtual void purgeCachedScenes() = 0; + virtual void enterScene(Ptr scene) = 0; }; /** @@ -47,49 +49,50 @@ public: */ class SceneService : public ISceneService { public: - SceneService(); - ~SceneService() override = default; + SceneService(); + ~SceneService() override = default; - ServiceInfo getServiceInfo() const override; + ServiceInfo getServiceInfo() const override; - bool initialize() override; - void shutdown() override; - void update(float deltaTime) override; + bool initialize() override; + void shutdown() override; + void update(float deltaTime) override; - void runWithScene(Ptr scene) override; - void replaceScene(Ptr scene) override; - void pushScene(Ptr scene) override; - void popScene() override; - void popToRootScene() override; - void popToScene(const std::string& name) override; + void runWithScene(Ptr scene) override; + void replaceScene(Ptr scene) override; + void pushScene(Ptr scene) override; + void popScene() override; + void popToRootScene() override; + void popToScene(const std::string &name) override; - Ptr getCurrentScene() const override; - Ptr getPreviousScene() const override; - Ptr getRootScene() const override; - Ptr getSceneByName(const std::string& name) const override; + Ptr getCurrentScene() const override; + Ptr getPreviousScene() const override; + Ptr getRootScene() const override; + Ptr getSceneByName(const std::string &name) const override; - size_t getSceneCount() const override; - bool isEmpty() const override; - bool hasScene(const std::string& name) const override; + size_t getSceneCount() const override; + bool isEmpty() const override; + bool hasScene(const std::string &name) const override; - void render(RenderBackend& renderer) override; - void collectRenderCommands(std::vector& commands) override; + void render(Renderer &renderer) override; + void collectRenderCommands(std::vector &commands) override; - bool isTransitioning() const override; - void setTransitionCallback(SceneManager::TransitionCallback callback) override; + bool isTransitioning() const override; + void + setTransitionCallback(SceneManager::TransitionCallback callback) override; - void end() override; - void purgeCachedScenes() override; - void enterScene(Ptr scene) override; + void end() override; + void purgeCachedScenes() override; + void enterScene(Ptr scene) override; - SceneManager& getManager() { return manager_; } - const SceneManager& getManager() const { return manager_; } + SceneManager &getManager() { return manager_; } + const SceneManager &getManager() const { return manager_; } private: - SceneManager manager_; + SceneManager manager_; - // 服务注册元数据 - E2D_AUTO_REGISTER_SERVICE(ISceneService, SceneService); + // 服务注册元数据 + E2D_AUTO_REGISTER_SERVICE(ISceneService, SceneService); }; -} +} // namespace extra2d diff --git a/Extra2D/src/app/application.cpp b/Extra2D/src/app/application.cpp index 203beff..8552240 100644 --- a/Extra2D/src/app/application.cpp +++ b/Extra2D/src/app/application.cpp @@ -1,8 +1,8 @@ #include #include -#include #include #include +#include #include #include #include @@ -11,7 +11,6 @@ #include #include - namespace extra2d { static double getTimeSeconds() { @@ -179,9 +178,7 @@ void Application::mainLoop() { } } -void Application::update() { - ServiceLocator::instance().updateAll(deltaTime_); -} +void Application::update() { ServiceLocator::instance().updateAll(deltaTime_); } void Application::render() { auto *renderMod = get(); @@ -218,7 +215,7 @@ GLFWWindow *Application::window() { return winMod ? winMod->win() : nullptr; } -RenderBackend *Application::renderer() { +Renderer *Application::renderer() { auto *renderMod = get(); return renderMod ? renderMod->renderer() : nullptr; } diff --git a/Extra2D/src/graphics/core/render_module.cpp b/Extra2D/src/graphics/core/render_module.cpp index 9a628fa..719a2ca 100644 --- a/Extra2D/src/graphics/core/render_module.cpp +++ b/Extra2D/src/graphics/core/render_module.cpp @@ -38,7 +38,7 @@ bool RenderModule::init() { } E2D_LOG_INFO("正在创建 OpenGL 渲染后端"); - renderer_ = makeUnique(); + renderer_ = makeUnique(); if (!renderer_) { E2D_LOG_ERROR("创建渲染后端失败"); diff --git a/Extra2D/src/graphics/opengl/gl_renderer.cpp b/Extra2D/src/graphics/opengl/gl_renderer.cpp index e5edef0..5bf2bec 100644 --- a/Extra2D/src/graphics/opengl/gl_renderer.cpp +++ b/Extra2D/src/graphics/opengl/gl_renderer.cpp @@ -13,7 +13,6 @@ #include #include - namespace extra2d { // VBO 初始大小(用于 VRAM 跟踪) @@ -22,7 +21,7 @@ static constexpr size_t SHAPE_VBO_SIZE = 1024 * sizeof(float); /** * @brief 构造函数,初始化OpenGL渲染器成员变量 */ -GLRenderer::GLRenderer() +Renderer::Renderer() : window_(nullptr), shapeVao_(0), lineVao_(0), vsync_(true), shapeVertexCount_(0), currentShapeMode_(GL_TRIANGLES), lineVertexCount_(0), currentLineWidth_(1.0f) { @@ -38,14 +37,14 @@ GLRenderer::GLRenderer() /** * @brief 析构函数,调用shutdown释放资源 */ -GLRenderer::~GLRenderer() { shutdown(); } +Renderer::~Renderer() { shutdown(); } /** * @brief 初始化OpenGL渲染器 * @param window 窗口指针 * @return 初始化成功返回true,失败返回false */ -bool GLRenderer::init(GLFWWindow *window) { +bool Renderer::init(GLFWWindow *window) { window_ = window; // 初始化 OpenGL 上下文(Switch 平台已通过 SDL2 + EGL 初始化,GLContext @@ -89,7 +88,7 @@ bool GLRenderer::init(GLFWWindow *window) { /** * @brief 关闭渲染器,释放所有GPU资源 */ -void GLRenderer::shutdown() { +void Renderer::shutdown() { // 标记 GPU 上下文为无效 // 这会在销毁 OpenGL 上下文之前通知所有 GPU 资源 GPUContext::get().markInvalid(); @@ -118,7 +117,7 @@ void GLRenderer::shutdown() { * @brief 开始新帧,清除颜色缓冲区并重置统计信息 * @param clearColor 清屏颜色 */ -void GLRenderer::beginFrame(const Color &clearColor) { +void Renderer::beginFrame(const Color &clearColor) { // 应用管线状态 pipeline_.applyAllStates(); @@ -130,7 +129,7 @@ void GLRenderer::beginFrame(const Color &clearColor) { /** * @brief 结束当前帧,刷新所有待处理的渲染批次 */ -void GLRenderer::endFrame() { +void Renderer::endFrame() { // 刷新所有待处理的精灵批次(自动批处理) if (autoBatchEnabled_ && batchActive_) { flush(); @@ -148,7 +147,7 @@ void GLRenderer::endFrame() { * @param width 视口宽度 * @param height 视口高度 */ -void GLRenderer::setViewport(int x, int y, int width, int height) { +void Renderer::setViewport(int x, int y, int width, int height) { // 使用 GLPipeline 管理视口状态 pipeline_.setViewport(x, y, width, height); } @@ -157,7 +156,7 @@ void GLRenderer::setViewport(int x, int y, int width, int height) { * @brief 设置垂直同步 * @param enabled true启用垂直同步,false禁用 */ -void GLRenderer::setVSync(bool enabled) { +void Renderer::setVSync(bool enabled) { vsync_ = enabled; // 通过窗口接口设置垂直同步 if (window_) { @@ -169,7 +168,7 @@ void GLRenderer::setVSync(bool enabled) { * @brief 设置混合模式 * @param mode 混合模式枚举值 */ -void GLRenderer::setBlendMode(BlendMode mode) { +void Renderer::setBlendMode(BlendMode mode) { // 使用 GLPipeline 管理混合状态 pipeline_.setBlendMode(mode); } @@ -178,7 +177,7 @@ void GLRenderer::setBlendMode(BlendMode mode) { * @brief 设置视图投影矩阵 * @param matrix 4x4视图投影矩阵 */ -void GLRenderer::setViewProjection(const glm::mat4 &matrix) { +void Renderer::setViewProjection(const glm::mat4 &matrix) { viewProjection_ = matrix; } @@ -186,7 +185,7 @@ void GLRenderer::setViewProjection(const glm::mat4 &matrix) { * @brief 压入变换矩阵到变换栈 * @param transform 变换矩阵 */ -void GLRenderer::pushTransform(const glm::mat4 &transform) { +void Renderer::pushTransform(const glm::mat4 &transform) { if (transformStack_.empty()) { transformStack_.push_back(transform); } else { @@ -197,7 +196,7 @@ void GLRenderer::pushTransform(const glm::mat4 &transform) { /** * @brief 从变换栈弹出顶部变换矩阵 */ -void GLRenderer::popTransform() { +void Renderer::popTransform() { if (!transformStack_.empty()) { transformStack_.pop_back(); } @@ -207,7 +206,7 @@ void GLRenderer::popTransform() { * @brief 获取当前累积的变换矩阵 * @return 当前变换矩阵,如果栈为空则返回单位矩阵 */ -glm::mat4 GLRenderer::getCurrentTransform() const { +glm::mat4 Renderer::getCurrentTransform() const { if (transformStack_.empty()) { return glm::mat4(1.0f); } @@ -222,8 +221,8 @@ glm::mat4 GLRenderer::getCurrentTransform() const { * @param channels 颜色通道数 * @return 创建的纹理智能指针 */ -Ptr GLRenderer::createTexture(int width, int height, - const uint8_t *pixels, int channels) { +Ptr Renderer::createTexture(int width, int height, + const uint8_t *pixels, int channels) { return makePtr(width, height, pixels, channels); } @@ -232,14 +231,14 @@ Ptr GLRenderer::createTexture(int width, int height, * @param filepath 纹理文件路径 * @return 加载的纹理智能指针 */ -Ptr GLRenderer::loadTexture(const std::string &filepath) { +Ptr Renderer::loadTexture(const std::string &filepath) { return makePtr(filepath); } /** * @brief 确保批处理已激活(自动批处理内部使用) */ -void GLRenderer::ensureBatchActive() { +void Renderer::ensureBatchActive() { if (!batchActive_) { spriteBatch_.begin(viewProjection_); batchActive_ = true; @@ -251,7 +250,7 @@ void GLRenderer::ensureBatchActive() { /** * @brief 提交待处理的精灵(自动批处理内部使用) */ -void GLRenderer::submitPendingSprites() { +void Renderer::submitPendingSprites() { if (pendingSprites_.empty()) { return; } @@ -266,7 +265,7 @@ void GLRenderer::submitPendingSprites() { * @brief 开始手动精灵批处理(高级用法) * @note 一般情况下不需要调用,drawSprite/drawText 会自动管理批处理 */ -void GLRenderer::beginSpriteBatch() { +void Renderer::beginSpriteBatch() { // 如果自动批处理已激活,先提交 if (autoBatchEnabled_ && batchActive_) { flush(); @@ -286,9 +285,9 @@ void GLRenderer::beginSpriteBatch() { * @param rotation 旋转角度(度) * @param anchor 锚点位置(0-1范围) */ -void GLRenderer::drawSprite(const Texture &texture, const Rect &destRect, - const Rect &srcRect, const Color &tint, - float rotation, const Vec2 &anchor) { +void Renderer::drawSprite(const Texture &texture, const Rect &destRect, + const Rect &srcRect, const Color &tint, + float rotation, const Vec2 &anchor) { // 自动批处理模式 if (autoBatchEnabled_) { ensureBatchActive(); @@ -356,8 +355,8 @@ void GLRenderer::drawSprite(const Texture &texture, const Rect &destRect, * @param position 绘制位置 * @param tint 着色颜色 */ -void GLRenderer::drawSprite(const Texture &texture, const Vec2 &position, - const Color &tint) { +void Renderer::drawSprite(const Texture &texture, const Vec2 &position, + const Color &tint) { Rect destRect(position.x, position.y, static_cast(texture.getWidth()), static_cast(texture.getHeight())); Rect srcRect(0, 0, static_cast(texture.getWidth()), @@ -369,7 +368,7 @@ void GLRenderer::drawSprite(const Texture &texture, const Vec2 &position, * @brief 结束手动精灵批处理并提交绘制 * @note 一般情况下不需要调用 */ -void GLRenderer::endSpriteBatch() { +void Renderer::endSpriteBatch() { if (autoBatchEnabled_) { // 自动模式下,只是标记批处理结束 flush(); @@ -386,7 +385,7 @@ void GLRenderer::endSpriteBatch() { * @brief 立即提交当前批处理 * @note 手动控制批处理提交时机,一般情况下不需要调用 */ -void GLRenderer::flush() { +void Renderer::flush() { if (autoBatchEnabled_ && batchActive_) { submitPendingSprites(); spriteBatch_.end(); @@ -403,8 +402,8 @@ void GLRenderer::flush() { * @param color 线条颜色 * @param width 线条宽度 */ -void GLRenderer::drawLine(const Vec2 &start, const Vec2 &end, - const Color &color, float width) { +void Renderer::drawLine(const Vec2 &start, const Vec2 &end, const Color &color, + float width) { // 如果线宽改变,需要先刷新线条批次 if (width != currentLineWidth_) { flushLineBatch(); @@ -416,7 +415,7 @@ void GLRenderer::drawLine(const Vec2 &start, const Vec2 &end, addLineVertex(end.x, end.y, color); } -void GLRenderer::drawRect(const Rect &rect, const Color &color, float width) { +void Renderer::drawRect(const Rect &rect, const Color &color, float width) { // 如果线宽改变,需要先刷新线条批次 if (width != currentLineWidth_) { flushLineBatch(); @@ -448,7 +447,7 @@ void GLRenderer::drawRect(const Rect &rect, const Color &color, float width) { * @param rect 矩形区域 * @param color 填充颜色 */ -void GLRenderer::fillRect(const Rect &rect, const Color &color) { +void Renderer::fillRect(const Rect &rect, const Color &color) { // 提交当前批次(如果模式不同) submitShapeBatch(GL_TRIANGLES); @@ -477,8 +476,8 @@ void GLRenderer::fillRect(const Rect &rect, const Color &color) { * @param segments 分段数 * @param width 线条宽度 */ -void GLRenderer::drawCircle(const Vec2 ¢er, float radius, - const Color &color, int segments, float width) { +void Renderer::drawCircle(const Vec2 ¢er, float radius, const Color &color, + int segments, float width) { // 限制段数不超过缓存大小 if (segments > static_cast(MAX_CIRCLE_SEGMENTS)) { segments = static_cast(MAX_CIRCLE_SEGMENTS); @@ -511,8 +510,8 @@ void GLRenderer::drawCircle(const Vec2 ¢er, float radius, * @param color 填充颜色 * @param segments 分段数 */ -void GLRenderer::fillCircle(const Vec2 ¢er, float radius, - const Color &color, int segments) { +void Renderer::fillCircle(const Vec2 ¢er, float radius, const Color &color, + int segments) { // 限制段数不超过缓存大小 if (segments > static_cast(MAX_CIRCLE_SEGMENTS)) { segments = static_cast(MAX_CIRCLE_SEGMENTS); @@ -546,8 +545,8 @@ void GLRenderer::fillCircle(const Vec2 ¢er, float radius, * @param color 边框颜色 * @param width 线条宽度 */ -void GLRenderer::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, - const Color &color, float width) { +void Renderer::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, + const Color &color, float width) { drawLine(p1, p2, color, width); drawLine(p2, p3, color, width); drawLine(p3, p1, color, width); @@ -560,8 +559,8 @@ void GLRenderer::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, * @param p3 第三个顶点 * @param color 填充颜色 */ -void GLRenderer::fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, - const Color &color) { +void Renderer::fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, + const Color &color) { submitShapeBatch(GL_TRIANGLES); addShapeVertex(p1.x, p1.y, color); @@ -575,8 +574,8 @@ void GLRenderer::fillTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, * @param color 边框颜色 * @param width 线条宽度 */ -void GLRenderer::drawPolygon(const std::vector &points, - const Color &color, float width) { +void Renderer::drawPolygon(const std::vector &points, const Color &color, + float width) { if (points.size() < 2) return; @@ -600,8 +599,8 @@ void GLRenderer::drawPolygon(const std::vector &points, * @param points 顶点数组 * @param color 填充颜色 */ -void GLRenderer::fillPolygon(const std::vector &points, - const Color &color) { +void Renderer::fillPolygon(const std::vector &points, + const Color &color) { if (points.size() < 3) return; @@ -623,8 +622,8 @@ void GLRenderer::fillPolygon(const std::vector &points, * @param useSDF 是否使用SDF渲染 * @return 创建的字体图集智能指针 */ -Ptr GLRenderer::createFontAtlas(const std::string &filepath, - int fontSize, bool useSDF) { +Ptr Renderer::createFontAtlas(const std::string &filepath, + int fontSize, bool useSDF) { return makePtr(filepath, fontSize, useSDF); } @@ -635,8 +634,8 @@ Ptr GLRenderer::createFontAtlas(const std::string &filepath, * @param position 绘制位置 * @param color 文本颜色 */ -void GLRenderer::drawText(const FontAtlas &font, const std::string &text, - const Vec2 &position, const Color &color) { +void Renderer::drawText(const FontAtlas &font, const std::string &text, + const Vec2 &position, const Color &color) { drawText(font, text, position.x, position.y, color); } @@ -648,8 +647,8 @@ void GLRenderer::drawText(const FontAtlas &font, const std::string &text, * @param y Y坐标 * @param color 文本颜色 */ -void GLRenderer::drawText(const FontAtlas &font, const std::string &text, - float x, float y, const Color &color) { +void Renderer::drawText(const FontAtlas &font, const std::string &text, float x, + float y, const Color &color) { float cursorX = x; float cursorY = y; float baselineY = cursorY + font.getAscent(); @@ -789,12 +788,12 @@ void GLRenderer::drawText(const FontAtlas &font, const std::string &text, /** * @brief 重置渲染统计信息 */ -void GLRenderer::resetStats() { stats_ = Stats{}; } +void Renderer::resetStats() { stats_ = RenderStats{}; } /** * @brief 初始化形状渲染所需的OpenGL资源(VAO、VBO、着色器) */ -void GLRenderer::initShapeRendering() { +void Renderer::initShapeRendering() { // 从ShaderManager获取形状着色器 shapeShader_ = ShaderManager::getInstance().getBuiltin("shape"); if (!shapeShader_) { @@ -871,7 +870,7 @@ void GLRenderer::initShapeRendering() { * @param y Y坐标 * @param color 顶点颜色 */ -void GLRenderer::addShapeVertex(float x, float y, const Color &color) { +void Renderer::addShapeVertex(float x, float y, const Color &color) { if (shapeVertexCount_ >= MAX_SHAPE_VERTICES) { flushShapeBatch(); } @@ -896,7 +895,7 @@ void GLRenderer::addShapeVertex(float x, float y, const Color &color) { * @param y Y坐标 * @param color 顶点颜色 */ -void GLRenderer::addLineVertex(float x, float y, const Color &color) { +void Renderer::addLineVertex(float x, float y, const Color &color) { if (lineVertexCount_ >= MAX_LINE_VERTICES) { flushLineBatch(); } @@ -919,7 +918,7 @@ void GLRenderer::addLineVertex(float x, float y, const Color &color) { * @brief 提交形状批次(如果需要切换绘制模式) * @param mode OpenGL绘制模式 */ -void GLRenderer::submitShapeBatch(GLenum mode) { +void Renderer::submitShapeBatch(GLenum mode) { if (shapeVertexCount_ == 0) return; @@ -933,7 +932,7 @@ void GLRenderer::submitShapeBatch(GLenum mode) { /** * @brief 刷新形状批次,执行实际的OpenGL绘制调用 */ -void GLRenderer::flushShapeBatch() { +void Renderer::flushShapeBatch() { if (shapeVertexCount_ == 0) return; @@ -966,7 +965,7 @@ void GLRenderer::flushShapeBatch() { /** * @brief 刷新线条批次,执行实际的OpenGL绘制调用 */ -void GLRenderer::flushLineBatch() { +void Renderer::flushLineBatch() { if (lineVertexCount_ == 0) return; @@ -1004,7 +1003,7 @@ void GLRenderer::flushLineBatch() { * @param desc 帧缓冲描述 * @return 创建的帧缓冲智能指针 */ -Ptr GLRenderer::createFramebuffer(const FramebufferDesc &desc) { +Ptr Renderer::createFramebuffer(const FramebufferDesc &desc) { auto framebuffer = makePtr(); if (!framebuffer->init(desc)) { E2D_LOG_ERROR("创建帧缓冲区失败"); @@ -1017,7 +1016,7 @@ Ptr GLRenderer::createFramebuffer(const FramebufferDesc &desc) { * @brief 绑定帧缓冲(作为渲染目标) * @param framebuffer 帧缓冲对象指针,传入 nullptr 则绑定默认帧缓冲 */ -void GLRenderer::bindFramebuffer(GLFramebuffer *framebuffer) { +void Renderer::bindFramebuffer(GLFramebuffer *framebuffer) { // 先刷新所有待处理的渲染批次 flush(); flushShapeBatch(); @@ -1039,13 +1038,13 @@ void GLRenderer::bindFramebuffer(GLFramebuffer *framebuffer) { /** * @brief 解绑帧缓冲(恢复到默认帧缓冲) */ -void GLRenderer::unbindFramebuffer() { bindFramebuffer(nullptr); } +void Renderer::unbindFramebuffer() { bindFramebuffer(nullptr); } /** * @brief 获取默认帧缓冲 * @return 默认帧缓冲智能指针 */ -Ptr GLRenderer::getDefaultFramebuffer() const { +Ptr Renderer::getDefaultFramebuffer() const { if (!defaultFramebuffer_) { // 延迟创建默认帧缓冲对象(代表系统默认帧缓冲,ID 为 0) defaultFramebuffer_ = makePtr(); @@ -1061,8 +1060,8 @@ Ptr GLRenderer::getDefaultFramebuffer() const { * @param clearDepth 是否清除深度缓冲 * @param clearStencil 是否清除模板缓冲 */ -void GLRenderer::clearFramebuffer(const Color &color, bool clearColor, - bool clearDepth, bool clearStencil) { +void Renderer::clearFramebuffer(const Color &color, bool clearColor, + bool clearDepth, bool clearStencil) { GLbitfield mask = 0; if (clearColor) { diff --git a/Extra2D/src/graphics/texture/texture_pool.cpp b/Extra2D/src/graphics/texture/texture_pool.cpp index d56a8df..2f6f2f5 100644 --- a/Extra2D/src/graphics/texture/texture_pool.cpp +++ b/Extra2D/src/graphics/texture/texture_pool.cpp @@ -1,5 +1,5 @@ +#include #include -#include #include #include @@ -17,13 +17,8 @@ namespace extra2d { * 创建一个未初始化的纹理池 */ TexturePool::TexturePool() - : scene_(nullptr) - , maxMemoryUsage_(0) - , currentMemoryUsage_(0) - , cacheHits_(0) - , cacheMisses_(0) - , evictionCount_(0) { -} + : scene_(nullptr), maxMemoryUsage_(0), currentMemoryUsage_(0), + cacheHits_(0), cacheMisses_(0), evictionCount_(0) {} /** * @brief 构造函数 @@ -32,14 +27,10 @@ TexturePool::TexturePool() * * 创建一个指定场景和内存限制的纹理池 */ -TexturePool::TexturePool(Scene* scene, size_t maxMemoryUsage) - : scene_(scene) - , maxMemoryUsage_(maxMemoryUsage) - , currentMemoryUsage_(0) - , cacheHits_(0) - , cacheMisses_(0) - , evictionCount_(0) { - E2D_LOG_INFO("TexturePool 已创建,最大内存: {} 字节", maxMemoryUsage); +TexturePool::TexturePool(Scene *scene, size_t maxMemoryUsage) + : scene_(scene), maxMemoryUsage_(maxMemoryUsage), currentMemoryUsage_(0), + cacheHits_(0), cacheMisses_(0), evictionCount_(0) { + E2D_LOG_INFO("TexturePool 已创建,最大内存: {} 字节", maxMemoryUsage); } /** @@ -49,10 +40,10 @@ TexturePool::TexturePool(Scene* scene, size_t maxMemoryUsage) * * 设置纹理池的场景和内存限制 */ -void TexturePool::init(Scene* scene, size_t maxMemoryUsage) { - scene_ = scene; - maxMemoryUsage_ = maxMemoryUsage; - E2D_LOG_INFO("TexturePool 已初始化,最大内存: {} 字节", maxMemoryUsage); +void TexturePool::init(Scene *scene, size_t maxMemoryUsage) { + scene_ = scene; + maxMemoryUsage_ = maxMemoryUsage; + E2D_LOG_INFO("TexturePool 已初始化,最大内存: {} 字节", maxMemoryUsage); } /** @@ -61,8 +52,8 @@ void TexturePool::init(Scene* scene, size_t maxMemoryUsage) { * 清理纹理池并释放所有资源 */ TexturePool::~TexturePool() { - clear(); - E2D_LOG_INFO("TexturePool 已销毁"); + clear(); + E2D_LOG_INFO("TexturePool 已销毁"); } // ============================================================================ @@ -77,8 +68,9 @@ TexturePool::~TexturePool() { * * 加载完整纹理文件到纹理池 */ -TextureRef TexturePool::load(const std::string& path, const TextureLoadOptions& options) { - return load(path, Rect::Zero(), options); +TextureRef TexturePool::load(const std::string &path, + const TextureLoadOptions &options) { + return load(path, Rect::Zero(), options); } /** @@ -90,75 +82,76 @@ TextureRef TexturePool::load(const std::string& path, const TextureLoadOptions& * * 加载纹理文件的指定区域到纹理池 */ -TextureRef TexturePool::load(const std::string& path, const Rect& region, - const TextureLoadOptions& options) { - TextureKey key(path, region); +TextureRef TexturePool::load(const std::string &path, const Rect ®ion, + const TextureLoadOptions &options) { + TextureKey key(path, region); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); - // 检查缓存 - auto it = cache_.find(key); - if (it != cache_.end()) { - // 缓存命中 - it->second.touch(); - it->second.refCount.fetch_add(1, std::memory_order_relaxed); - cacheHits_.fetch_add(1, std::memory_order_relaxed); + // 检查缓存 + auto it = cache_.find(key); + if (it != cache_.end()) { + // 缓存命中 + it->second.touch(); + it->second.refCount.fetch_add(1, std::memory_order_relaxed); + cacheHits_.fetch_add(1, std::memory_order_relaxed); - E2D_LOG_DEBUG("纹理缓存命中: {}", path); - return TextureRef(it->second.texture, &it->second, &mutex_); - } + E2D_LOG_DEBUG("纹理缓存命中: {}", path); + return TextureRef(it->second.texture, &it->second, &mutex_); + } - // 缓存未命中 - cacheMisses_.fetch_add(1, std::memory_order_relaxed); + // 缓存未命中 + cacheMisses_.fetch_add(1, std::memory_order_relaxed); - // 获取渲染后端 - RenderBackend* backend = nullptr; - if (scene_) { - // 假设 Scene 有获取 RenderBackend 的方法 - // 这里需要根据实际接口调整 - backend = nullptr; // TODO: 从 Scene 获取 RenderBackend - } - - if (!backend) { - E2D_LOG_ERROR("TexturePool: 渲染后端不可用"); - return TextureRef(); - } - - // 加载纹理 - Ptr texture = backend->loadTexture(path); - if (!texture) { - E2D_LOG_ERROR("TexturePool: 加载纹理失败: {}", path); - return TextureRef(); - } - - // 计算内存大小 - size_t memorySize = calculateTextureMemory(texture.get()); - - // 检查内存限制 - if (maxMemoryUsage_ > 0 && currentMemoryUsage_ + memorySize > maxMemoryUsage_) { - // 尝试淘汰 - evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_); - - // 再次检查 - if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) { - E2D_LOG_WARN("TexturePool: 内存限制超出,无法加载纹理: {}", path); - return TextureRef(); - } - } - - // 创建缓存条目 - auto result = cache_.emplace(key, TexturePoolEntry(nullptr, key, 0)); - if (result.second) { - result.first->second.texture = texture; - result.first->second.memorySize = memorySize; - result.first->second.refCount.store(1, std::memory_order_relaxed); - result.first->second.touch(); - currentMemoryUsage_ += memorySize; - E2D_LOG_INFO("TexturePool: 已加载纹理: {} ({} 字节)", path, memorySize); - return TextureRef(texture, &result.first->second, &mutex_); - } + // 获取渲染后端 + Renderer *backend = nullptr; + if (scene_) { + // 假设 Scene 有获取 Renderer 的方法 + // 这里需要根据实际接口调整 + backend = nullptr; // TODO: 从 Scene 获取 Renderer + } + if (!backend) { + E2D_LOG_ERROR("TexturePool: 渲染后端不可用"); return TextureRef(); + } + + // 加载纹理 + Ptr texture = backend->loadTexture(path); + if (!texture) { + E2D_LOG_ERROR("TexturePool: 加载纹理失败: {}", path); + return TextureRef(); + } + + // 计算内存大小 + size_t memorySize = calculateTextureMemory(texture.get()); + + // 检查内存限制 + if (maxMemoryUsage_ > 0 && + currentMemoryUsage_ + memorySize > maxMemoryUsage_) { + // 尝试淘汰 + evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_); + + // 再次检查 + if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) { + E2D_LOG_WARN("TexturePool: 内存限制超出,无法加载纹理: {}", path); + return TextureRef(); + } + } + + // 创建缓存条目 + auto result = cache_.emplace(key, TexturePoolEntry(nullptr, key, 0)); + if (result.second) { + result.first->second.texture = texture; + result.first->second.memorySize = memorySize; + result.first->second.refCount.store(1, std::memory_order_relaxed); + result.first->second.touch(); + currentMemoryUsage_ += memorySize; + E2D_LOG_INFO("TexturePool: 已加载纹理: {} ({} 字节)", path, memorySize); + return TextureRef(texture, &result.first->second, &mutex_); + } + + return TextureRef(); } /** @@ -172,67 +165,70 @@ TextureRef TexturePool::load(const std::string& path, const Rect& region, * * 从内存中的像素数据创建纹理并加入纹理池 */ -TextureRef TexturePool::loadFromMemory(const uint8_t* data, int width, int height, - int channels, const std::string& key) { - TextureKey textureKey(key); +TextureRef TexturePool::loadFromMemory(const uint8_t *data, int width, + int height, int channels, + const std::string &key) { + TextureKey textureKey(key); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); - // 检查缓存 - auto it = cache_.find(textureKey); - if (it != cache_.end()) { - it->second.touch(); - it->second.refCount.fetch_add(1, std::memory_order_relaxed); - cacheHits_.fetch_add(1, std::memory_order_relaxed); - return TextureRef(it->second.texture, &it->second, &mutex_); - } + // 检查缓存 + auto it = cache_.find(textureKey); + if (it != cache_.end()) { + it->second.touch(); + it->second.refCount.fetch_add(1, std::memory_order_relaxed); + cacheHits_.fetch_add(1, std::memory_order_relaxed); + return TextureRef(it->second.texture, &it->second, &mutex_); + } - cacheMisses_.fetch_add(1, std::memory_order_relaxed); + cacheMisses_.fetch_add(1, std::memory_order_relaxed); - // 获取渲染后端 - RenderBackend* backend = nullptr; - if (scene_) { - backend = nullptr; // TODO: 从 Scene 获取 RenderBackend - } - - if (!backend) { - E2D_LOG_ERROR("TexturePool: 渲染后端不可用"); - return TextureRef(); - } - - // 创建纹理 - Ptr texture = backend->createTexture(width, height, data, channels); - if (!texture) { - E2D_LOG_ERROR("TexturePool: 从内存创建纹理失败"); - return TextureRef(); - } - - // 计算内存大小 - size_t memorySize = calculateTextureMemory(texture.get()); - - // 检查内存限制 - if (maxMemoryUsage_ > 0 && currentMemoryUsage_ + memorySize > maxMemoryUsage_) { - evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_); - - if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) { - E2D_LOG_WARN("TexturePool: 内存限制超出"); - return TextureRef(); - } - } - - // 创建缓存条目 - auto result = cache_.emplace(textureKey, TexturePoolEntry(nullptr, textureKey, 0)); - if (result.second) { - result.first->second.texture = texture; - result.first->second.memorySize = memorySize; - result.first->second.refCount.store(1, std::memory_order_relaxed); - result.first->second.touch(); - currentMemoryUsage_ += memorySize; - E2D_LOG_INFO("TexturePool: 已从内存创建纹理 ({} 字节)", memorySize); - return TextureRef(texture, &result.first->second, &mutex_); - } + // 获取渲染后端 + Renderer *backend = nullptr; + if (scene_) { + backend = nullptr; // TODO: 从 Scene 获取 Renderer + } + if (!backend) { + E2D_LOG_ERROR("TexturePool: 渲染后端不可用"); return TextureRef(); + } + + // 创建纹理 + Ptr texture = backend->createTexture(width, height, data, channels); + if (!texture) { + E2D_LOG_ERROR("TexturePool: 从内存创建纹理失败"); + return TextureRef(); + } + + // 计算内存大小 + size_t memorySize = calculateTextureMemory(texture.get()); + + // 检查内存限制 + if (maxMemoryUsage_ > 0 && + currentMemoryUsage_ + memorySize > maxMemoryUsage_) { + evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_); + + if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) { + E2D_LOG_WARN("TexturePool: 内存限制超出"); + return TextureRef(); + } + } + + // 创建缓存条目 + auto result = + cache_.emplace(textureKey, TexturePoolEntry(nullptr, textureKey, 0)); + if (result.second) { + result.first->second.texture = texture; + result.first->second.memorySize = memorySize; + result.first->second.refCount.store(1, std::memory_order_relaxed); + result.first->second.touch(); + currentMemoryUsage_ += memorySize; + E2D_LOG_INFO("TexturePool: 已从内存创建纹理 ({} 字节)", memorySize); + return TextureRef(texture, &result.first->second, &mutex_); + } + + return TextureRef(); } /** @@ -243,8 +239,9 @@ TextureRef TexturePool::loadFromMemory(const uint8_t* data, int width, int heigh * * 如果纹理已缓存则返回缓存,否则加载纹理 */ -TextureRef TexturePool::getOrLoad(const std::string& path, const TextureLoadOptions& options) { - return getOrLoad(path, Rect::Zero(), options); +TextureRef TexturePool::getOrLoad(const std::string &path, + const TextureLoadOptions &options) { + return getOrLoad(path, Rect::Zero(), options); } /** @@ -256,65 +253,66 @@ TextureRef TexturePool::getOrLoad(const std::string& path, const TextureLoadOpti * * 如果纹理区域已缓存则返回缓存,否则加载纹理区域 */ -TextureRef TexturePool::getOrLoad(const std::string& path, const Rect& region, - const TextureLoadOptions& options) { - TextureKey key(path, region); +TextureRef TexturePool::getOrLoad(const std::string &path, const Rect ®ion, + const TextureLoadOptions &options) { + TextureKey key(path, region); - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); - // 检查缓存 - auto it = cache_.find(key); - if (it != cache_.end()) { - it->second.touch(); - it->second.refCount.fetch_add(1, std::memory_order_relaxed); - cacheHits_.fetch_add(1, std::memory_order_relaxed); - return TextureRef(it->second.texture, &it->second, &mutex_); - } + // 检查缓存 + auto it = cache_.find(key); + if (it != cache_.end()) { + it->second.touch(); + it->second.refCount.fetch_add(1, std::memory_order_relaxed); + cacheHits_.fetch_add(1, std::memory_order_relaxed); + return TextureRef(it->second.texture, &it->second, &mutex_); + } - // 释放锁后调用 load - // 注意:这里需要重新设计以避免死锁 - // 简化处理:直接在这里加载 + // 释放锁后调用 load + // 注意:这里需要重新设计以避免死锁 + // 简化处理:直接在这里加载 - cacheMisses_.fetch_add(1, std::memory_order_relaxed); + cacheMisses_.fetch_add(1, std::memory_order_relaxed); - RenderBackend* backend = nullptr; - if (scene_) { - backend = nullptr; // TODO: 从 Scene 获取 RenderBackend - } - - if (!backend) { - E2D_LOG_ERROR("TexturePool: 渲染后端不可用"); - return TextureRef(); - } - - Ptr texture = backend->loadTexture(path); - if (!texture) { - E2D_LOG_ERROR("TexturePool: 加载纹理失败: {}", path); - return TextureRef(); - } - - size_t memorySize = calculateTextureMemory(texture.get()); - - if (maxMemoryUsage_ > 0 && currentMemoryUsage_ + memorySize > maxMemoryUsage_) { - evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_); - - if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) { - E2D_LOG_WARN("TexturePool: 内存限制超出"); - return TextureRef(); - } - } - - auto result = cache_.emplace(key, TexturePoolEntry(nullptr, key, 0)); - if (result.second) { - result.first->second.texture = texture; - result.first->second.memorySize = memorySize; - result.first->second.refCount.store(1, std::memory_order_relaxed); - result.first->second.touch(); - currentMemoryUsage_ += memorySize; - return TextureRef(texture, &result.first->second, &mutex_); - } + Renderer *backend = nullptr; + if (scene_) { + backend = nullptr; // TODO: 从 Scene 获取 Renderer + } + if (!backend) { + E2D_LOG_ERROR("TexturePool: 渲染后端不可用"); return TextureRef(); + } + + Ptr texture = backend->loadTexture(path); + if (!texture) { + E2D_LOG_ERROR("TexturePool: 加载纹理失败: {}", path); + return TextureRef(); + } + + size_t memorySize = calculateTextureMemory(texture.get()); + + if (maxMemoryUsage_ > 0 && + currentMemoryUsage_ + memorySize > maxMemoryUsage_) { + evictLRU(currentMemoryUsage_ + memorySize - maxMemoryUsage_); + + if (currentMemoryUsage_ + memorySize > maxMemoryUsage_) { + E2D_LOG_WARN("TexturePool: 内存限制超出"); + return TextureRef(); + } + } + + auto result = cache_.emplace(key, TexturePoolEntry(nullptr, key, 0)); + if (result.second) { + result.first->second.texture = texture; + result.first->second.memorySize = memorySize; + result.first->second.refCount.store(1, std::memory_order_relaxed); + result.first->second.touch(); + currentMemoryUsage_ += memorySize; + return TextureRef(texture, &result.first->second, &mutex_); + } + + return TextureRef(); } // ============================================================================ @@ -328,16 +326,16 @@ TextureRef TexturePool::getOrLoad(const std::string& path, const Rect& region, * * 增加指定纹理的引用计数 */ -bool TexturePool::addRef(const TextureKey& key) { - std::lock_guard lock(mutex_); +bool TexturePool::addRef(const TextureKey &key) { + std::lock_guard lock(mutex_); - auto it = cache_.find(key); - if (it != cache_.end()) { - it->second.touch(); - it->second.refCount.fetch_add(1, std::memory_order_relaxed); - return true; - } - return false; + auto it = cache_.find(key); + if (it != cache_.end()) { + it->second.touch(); + it->second.refCount.fetch_add(1, std::memory_order_relaxed); + return true; + } + return false; } /** @@ -347,15 +345,16 @@ bool TexturePool::addRef(const TextureKey& key) { * * 减少指定纹理的引用计数并返回新值 */ -uint32_t TexturePool::release(const TextureKey& key) { - std::lock_guard lock(mutex_); +uint32_t TexturePool::release(const TextureKey &key) { + std::lock_guard lock(mutex_); - auto it = cache_.find(key); - if (it != cache_.end()) { - uint32_t count = it->second.refCount.fetch_sub(1, std::memory_order_relaxed); - return count > 0 ? count - 1 : 0; - } - return 0; + auto it = cache_.find(key); + if (it != cache_.end()) { + uint32_t count = + it->second.refCount.fetch_sub(1, std::memory_order_relaxed); + return count > 0 ? count - 1 : 0; + } + return 0; } /** @@ -365,14 +364,14 @@ uint32_t TexturePool::release(const TextureKey& key) { * * 获取指定纹理的当前引用计数 */ -uint32_t TexturePool::getRefCount(const TextureKey& key) const { - std::lock_guard lock(mutex_); +uint32_t TexturePool::getRefCount(const TextureKey &key) const { + std::lock_guard lock(mutex_); - auto it = cache_.find(key); - if (it != cache_.end()) { - return it->second.refCount.load(std::memory_order_relaxed); - } - return 0; + auto it = cache_.find(key); + if (it != cache_.end()) { + return it->second.refCount.load(std::memory_order_relaxed); + } + return 0; } // ============================================================================ @@ -386,9 +385,9 @@ uint32_t TexturePool::getRefCount(const TextureKey& key) const { * * 检查指定纹理是否存在于缓存中 */ -bool TexturePool::isCached(const TextureKey& key) const { - std::lock_guard lock(mutex_); - return cache_.find(key) != cache_.end(); +bool TexturePool::isCached(const TextureKey &key) const { + std::lock_guard lock(mutex_); + return cache_.find(key) != cache_.end(); } /** @@ -398,17 +397,17 @@ bool TexturePool::isCached(const TextureKey& key) const { * * 从缓存中移除指定的纹理 */ -bool TexturePool::removeFromCache(const TextureKey& key) { - std::lock_guard lock(mutex_); +bool TexturePool::removeFromCache(const TextureKey &key) { + std::lock_guard lock(mutex_); - auto it = cache_.find(key); - if (it != cache_.end()) { - currentMemoryUsage_ -= it->second.memorySize; - cache_.erase(it); - E2D_LOG_DEBUG("TexturePool: 已从缓存移除纹理"); - return true; - } - return false; + auto it = cache_.find(key); + if (it != cache_.end()) { + currentMemoryUsage_ -= it->second.memorySize; + cache_.erase(it); + E2D_LOG_DEBUG("TexturePool: 已从缓存移除纹理"); + return true; + } + return false; } /** @@ -418,24 +417,24 @@ bool TexturePool::removeFromCache(const TextureKey& key) { * 清理所有引用计数为0的纹理,释放内存 */ size_t TexturePool::collectGarbage() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); - size_t removed = 0; - for (auto it = cache_.begin(); it != cache_.end(); ) { - if (it->second.refCount.load(std::memory_order_relaxed) == 0) { - currentMemoryUsage_ -= it->second.memorySize; - it = cache_.erase(it); - ++removed; - } else { - ++it; - } + size_t removed = 0; + for (auto it = cache_.begin(); it != cache_.end();) { + if (it->second.refCount.load(std::memory_order_relaxed) == 0) { + currentMemoryUsage_ -= it->second.memorySize; + it = cache_.erase(it); + ++removed; + } else { + ++it; } + } - if (removed > 0) { - E2D_LOG_INFO("TexturePool: 垃圾回收 {} 个纹理", removed); - } + if (removed > 0) { + E2D_LOG_INFO("TexturePool: 垃圾回收 {} 个纹理", removed); + } - return removed; + return removed; } /** @@ -444,12 +443,12 @@ size_t TexturePool::collectGarbage() { * 移除纹理池中的所有纹理 */ void TexturePool::clear() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); - cache_.clear(); - currentMemoryUsage_ = 0; + cache_.clear(); + currentMemoryUsage_ = 0; - E2D_LOG_INFO("TexturePool: 已清除所有纹理"); + E2D_LOG_INFO("TexturePool: 已清除所有纹理"); } // ============================================================================ @@ -463,8 +462,8 @@ void TexturePool::clear() { * 返回纹理池当前的内存使用量 */ size_t TexturePool::getMemoryUsage() const { - std::lock_guard lock(mutex_); - return currentMemoryUsage_; + std::lock_guard lock(mutex_); + return currentMemoryUsage_; } /** @@ -474,15 +473,15 @@ size_t TexturePool::getMemoryUsage() const { * 设置纹理池的内存上限,如果当前使用量超过新上限则执行淘汰 */ void TexturePool::setMaxMemoryUsage(size_t maxMemory) { - std::lock_guard lock(mutex_); - maxMemoryUsage_ = maxMemory; + std::lock_guard lock(mutex_); + maxMemoryUsage_ = maxMemory; - // 如果当前内存超过新的限制,执行淘汰 - if (maxMemoryUsage_ > 0 && currentMemoryUsage_ > maxMemoryUsage_) { - evictLRU(maxMemoryUsage_); - } + // 如果当前内存超过新的限制,执行淘汰 + if (maxMemoryUsage_ > 0 && currentMemoryUsage_ > maxMemoryUsage_) { + evictLRU(maxMemoryUsage_); + } - E2D_LOG_INFO("TexturePool: 最大内存设置为 {} 字节", maxMemory); + E2D_LOG_INFO("TexturePool: 最大内存设置为 {} 字节", maxMemory); } /** @@ -493,49 +492,49 @@ void TexturePool::setMaxMemoryUsage(size_t maxMemory) { * 根据LRU算法淘汰最少使用的纹理以达到目标内存使用量 */ size_t TexturePool::evictLRU(size_t targetMemory) { - // 注意:调用者应该已持有锁 + // 注意:调用者应该已持有锁 - if (cache_.empty()) { - return 0; + if (cache_.empty()) { + return 0; + } + + // 收集所有条目并按最后访问时间排序 + std::vector> entries; + entries.reserve(cache_.size()); + + for (const auto &pair : cache_) { + // 只淘汰引用计数为 0 的纹理 + if (pair.second.refCount.load(std::memory_order_relaxed) == 0) { + entries.emplace_back(pair.first, pair.second.lastAccessTime); + } + } + + // 按访问时间升序排序(最旧的在前) + std::sort(entries.begin(), entries.end(), + [](const auto &a, const auto &b) { return a.second < b.second; }); + + size_t evicted = 0; + size_t target = targetMemory > 0 ? targetMemory : 0; + + for (const auto &entry : entries) { + if (targetMemory > 0 && currentMemoryUsage_ <= target) { + break; } - // 收集所有条目并按最后访问时间排序 - std::vector> entries; - entries.reserve(cache_.size()); - - for (const auto& pair : cache_) { - // 只淘汰引用计数为 0 的纹理 - if (pair.second.refCount.load(std::memory_order_relaxed) == 0) { - entries.emplace_back(pair.first, pair.second.lastAccessTime); - } + auto it = cache_.find(entry.first); + if (it != cache_.end()) { + currentMemoryUsage_ -= it->second.memorySize; + cache_.erase(it); + ++evicted; } + } - // 按访问时间升序排序(最旧的在前) - std::sort(entries.begin(), entries.end(), - [](const auto& a, const auto& b) { return a.second < b.second; }); + if (evicted > 0) { + evictionCount_.fetch_add(evicted, std::memory_order_relaxed); + E2D_LOG_INFO("TexturePool: LRU 淘汰 {} 个纹理", evicted); + } - size_t evicted = 0; - size_t target = targetMemory > 0 ? targetMemory : 0; - - for (const auto& entry : entries) { - if (targetMemory > 0 && currentMemoryUsage_ <= target) { - break; - } - - auto it = cache_.find(entry.first); - if (it != cache_.end()) { - currentMemoryUsage_ -= it->second.memorySize; - cache_.erase(it); - ++evicted; - } - } - - if (evicted > 0) { - evictionCount_.fetch_add(evicted, std::memory_order_relaxed); - E2D_LOG_INFO("TexturePool: LRU 淘汰 {} 个纹理", evicted); - } - - return evicted; + return evicted; } // ============================================================================ @@ -549,17 +548,17 @@ size_t TexturePool::evictLRU(size_t targetMemory) { * 返回纹理池的统计信息,包括纹理数量、内存使用、缓存命中率等 */ TexturePool::Stats TexturePool::getStats() const { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); - Stats stats; - stats.textureCount = cache_.size(); - stats.memoryUsage = currentMemoryUsage_; - stats.maxMemoryUsage = maxMemoryUsage_; - stats.cacheHits = cacheHits_.load(std::memory_order_relaxed); - stats.cacheMisses = cacheMisses_.load(std::memory_order_relaxed); - stats.evictionCount = evictionCount_.load(std::memory_order_relaxed); + Stats stats; + stats.textureCount = cache_.size(); + stats.memoryUsage = currentMemoryUsage_; + stats.maxMemoryUsage = maxMemoryUsage_; + stats.cacheHits = cacheHits_.load(std::memory_order_relaxed); + stats.cacheMisses = cacheMisses_.load(std::memory_order_relaxed); + stats.evictionCount = evictionCount_.load(std::memory_order_relaxed); - return stats; + return stats; } /** @@ -568,9 +567,9 @@ TexturePool::Stats TexturePool::getStats() const { * 清零缓存命中、未命中和淘汰计数 */ void TexturePool::resetStats() { - cacheHits_.store(0, std::memory_order_relaxed); - cacheMisses_.store(0, std::memory_order_relaxed); - evictionCount_.store(0, std::memory_order_relaxed); + cacheHits_.store(0, std::memory_order_relaxed); + cacheMisses_.store(0, std::memory_order_relaxed); + evictionCount_.store(0, std::memory_order_relaxed); } // ============================================================================ @@ -584,48 +583,48 @@ void TexturePool::resetStats() { * * 根据纹理的尺寸、通道数和像素格式计算内存占用 */ -size_t TexturePool::calculateTextureMemory(const Texture* texture) { - if (!texture) { - return 0; - } +size_t TexturePool::calculateTextureMemory(const Texture *texture) { + if (!texture) { + return 0; + } - int width = texture->getWidth(); - int height = texture->getHeight(); - int channels = texture->getChannels(); + int width = texture->getWidth(); + int height = texture->getHeight(); + int channels = texture->getChannels(); - // 基础内存计算 - size_t baseSize = static_cast(width) * height * channels; + // 基础内存计算 + size_t baseSize = static_cast(width) * height * channels; - // 根据像素格式调整 - PixelFormat format = texture->getFormat(); - switch (format) { - case PixelFormat::RGB16F: - case PixelFormat::RGBA16F: - baseSize *= 2; // 半精度浮点 - break; - case PixelFormat::RGB32F: - case PixelFormat::RGBA32F: - baseSize *= 4; // 全精度浮点 - break; - case PixelFormat::Depth16: - baseSize = static_cast(width) * height * 2; - break; - case PixelFormat::Depth24: - case PixelFormat::Depth24Stencil8: - baseSize = static_cast(width) * height * 4; - break; - case PixelFormat::Depth32F: - baseSize = static_cast(width) * height * 4; - break; - default: - break; - } + // 根据像素格式调整 + PixelFormat format = texture->getFormat(); + switch (format) { + case PixelFormat::RGB16F: + case PixelFormat::RGBA16F: + baseSize *= 2; // 半精度浮点 + break; + case PixelFormat::RGB32F: + case PixelFormat::RGBA32F: + baseSize *= 4; // 全精度浮点 + break; + case PixelFormat::Depth16: + baseSize = static_cast(width) * height * 2; + break; + case PixelFormat::Depth24: + case PixelFormat::Depth24Stencil8: + baseSize = static_cast(width) * height * 4; + break; + case PixelFormat::Depth32F: + baseSize = static_cast(width) * height * 4; + break; + default: + break; + } - // 考虑 Mipmaps(大约增加 33% 内存) - // 注意:这里假设生成了 mipmaps,实际应该根据 TextureLoadOptions 判断 - // baseSize = baseSize * 4 / 3; + // 考虑 Mipmaps(大约增加 33% 内存) + // 注意:这里假设生成了 mipmaps,实际应该根据 TextureLoadOptions 判断 + // baseSize = baseSize * 4 / 3; - return baseSize; + return baseSize; } /** @@ -635,7 +634,7 @@ size_t TexturePool::calculateTextureMemory(const Texture* texture) { * 检查当前内存使用量是否超过限制 */ bool TexturePool::needsEviction() const { - return maxMemoryUsage_ > 0 && currentMemoryUsage_ > maxMemoryUsage_; + return maxMemoryUsage_ > 0 && currentMemoryUsage_ > maxMemoryUsage_; } /** @@ -644,9 +643,9 @@ bool TexturePool::needsEviction() const { * 如果内存使用量超过限制,执行LRU淘汰 */ void TexturePool::tryAutoEvict() { - if (needsEviction()) { - evictLRU(maxMemoryUsage_); - } + if (needsEviction()) { + evictLRU(maxMemoryUsage_); + } } -} // namespace extra2d +} // namespace extra2d diff --git a/Extra2D/src/scene/node.cpp b/Extra2D/src/scene/node.cpp index 5af1ca2..6b5422a 100644 --- a/Extra2D/src/scene/node.cpp +++ b/Extra2D/src/scene/node.cpp @@ -511,12 +511,12 @@ void Node::onUpdate(float dt) { } /** - * @brief 渲染回调 - * @param renderer 渲染后端引用 + * @brief 渲染节点及其子节点 + * @param renderer 渲染器引用 * * 如果可见则绘制自身,然后递归渲染所有子节点 */ -void Node::onRender(RenderBackend &renderer) { +void Node::onRender(Renderer &renderer) { if (!visible_) return; @@ -579,7 +579,7 @@ void Node::update(float dt) { onUpdate(dt); } * * 如果需要则对子节点排序,然后调用onRender进行渲染 */ -void Node::render(RenderBackend &renderer) { +void Node::render(Renderer &renderer) { if (childrenOrderDirty_) { sortChildren(); } diff --git a/Extra2D/src/scene/scene.cpp b/Extra2D/src/scene/scene.cpp index 151acf1..0208ad8 100644 --- a/Extra2D/src/scene/scene.cpp +++ b/Extra2D/src/scene/scene.cpp @@ -1,8 +1,9 @@ -#include #include +#include #include #include + namespace extra2d { /** @@ -48,7 +49,7 @@ void Scene::setViewportSize(const Size &size) { * * 如果场景不可见则直接返回,否则开始帧渲染、渲染内容并结束帧 */ -void Scene::renderScene(RenderBackend &renderer) { +void Scene::renderScene(Renderer &renderer) { if (!isVisible()) return; @@ -65,7 +66,7 @@ void Scene::renderScene(RenderBackend &renderer) { * 批量更新节点变换,开始精灵批处理并渲染 * 注意:视图投影矩阵由 Application 通过 CameraService 设置 */ -void Scene::renderContent(RenderBackend &renderer) { +void Scene::renderContent(Renderer &renderer) { if (!isVisible()) return; diff --git a/Extra2D/src/scene/scene_manager.cpp b/Extra2D/src/scene/scene_manager.cpp index 58e389f..91758c0 100644 --- a/Extra2D/src/scene/scene_manager.cpp +++ b/Extra2D/src/scene/scene_manager.cpp @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include #include #include @@ -13,6 +13,7 @@ #include #include + namespace extra2d { namespace { @@ -89,32 +90,36 @@ void SceneManager::setupEventListeners() { return; } - mouseMoveListener_ = eventService->addListener(EventType::MouseMoved, [this](Event &e) { - auto &mouseEvent = std::get(e.data); - mousePos_ = mouseEvent.position; - mouseDelta_ = mouseEvent.delta; - }); + mouseMoveListener_ = + eventService->addListener(EventType::MouseMoved, [this](Event &e) { + auto &mouseEvent = std::get(e.data); + mousePos_ = mouseEvent.position; + mouseDelta_ = mouseEvent.delta; + }); - mousePressListener_ = eventService->addListener(EventType::MouseButtonPressed, [this](Event &e) { - auto &mouseEvent = std::get(e.data); - if (mouseEvent.button == static_cast(Mouse::Left)) { - mouseLeftPressed_ = true; - mouseLeftDown_ = true; - } - }); + mousePressListener_ = eventService->addListener( + EventType::MouseButtonPressed, [this](Event &e) { + auto &mouseEvent = std::get(e.data); + if (mouseEvent.button == static_cast(Mouse::Left)) { + mouseLeftPressed_ = true; + mouseLeftDown_ = true; + } + }); - mouseReleaseListener_ = eventService->addListener(EventType::MouseButtonReleased, [this](Event &e) { - auto &mouseEvent = std::get(e.data); - if (mouseEvent.button == static_cast(Mouse::Left)) { - mouseLeftReleased_ = true; - mouseLeftDown_ = false; - } - }); + mouseReleaseListener_ = eventService->addListener( + EventType::MouseButtonReleased, [this](Event &e) { + auto &mouseEvent = std::get(e.data); + if (mouseEvent.button == static_cast(Mouse::Left)) { + mouseLeftReleased_ = true; + mouseLeftDown_ = false; + } + }); - scrollListener_ = eventService->addListener(EventType::MouseScrolled, [this](Event &e) { - auto &scrollEvent = std::get(e.data); - scrollDelta_ = scrollEvent.offset.y; - }); + scrollListener_ = + eventService->addListener(EventType::MouseScrolled, [this](Event &e) { + auto &scrollEvent = std::get(e.data); + scrollDelta_ = scrollEvent.offset.y; + }); } /** @@ -610,7 +615,7 @@ void SceneManager::update(float dt) { * * 使用当前场景的背景色清除帧缓冲并渲染场景内容 */ -void SceneManager::render(RenderBackend &renderer) { +void SceneManager::render(Renderer &renderer) { Color clearColor = Colors::Black; if (!sceneStack_.empty()) { clearColor = sceneStack_.top()->getBackgroundColor(); diff --git a/Extra2D/src/scene/shape_node.cpp b/Extra2D/src/scene/shape_node.cpp index 676bc78..369d64c 100644 --- a/Extra2D/src/scene/shape_node.cpp +++ b/Extra2D/src/scene/shape_node.cpp @@ -1,10 +1,11 @@ #include #include -#include #include +#include #include #include + namespace extra2d { /** @@ -196,24 +197,18 @@ Ptr ShapeNode::createFilledPolygon(const std::vector &points, * @brief 设置形状的所有顶点 * @param points 顶点坐标数组 */ -void ShapeNode::setPoints(const std::vector &points) { - points_ = points; -} +void ShapeNode::setPoints(const std::vector &points) { points_ = points; } /** * @brief 添加一个顶点到形状 * @param point 要添加的顶点坐标 */ -void ShapeNode::addPoint(const Vec2 &point) { - points_.push_back(point); -} +void ShapeNode::addPoint(const Vec2 &point) { points_.push_back(point); } /** * @brief 清除所有顶点 */ -void ShapeNode::clearPoints() { - points_.clear(); -} +void ShapeNode::clearPoints() { points_.clear(); } /** * @brief 获取形状的边界矩形 @@ -271,7 +266,7 @@ Rect ShapeNode::getBounds() const { * 注意:变换矩阵已由 Node::onRender 通过 pushTransform 应用, * 此处直接使用本地坐标即可。 */ -void ShapeNode::onDraw(RenderBackend &renderer) { +void ShapeNode::onDraw(Renderer &renderer) { if (points_.empty()) { return; } @@ -361,16 +356,16 @@ void ShapeNode::generateRenderCommand(std::vector &commands, case ShapeType::Point: if (!points_.empty()) { cmd.type = RenderCommandType::FilledCircle; - cmd.data = - CircleCommandData{points_[0] + offset, lineWidth_ * 0.5f, color_, 8, 0.0f, true}; + cmd.data = CircleCommandData{ + points_[0] + offset, lineWidth_ * 0.5f, color_, 8, 0.0f, true}; } break; case ShapeType::Line: if (points_.size() >= 2) { cmd.type = RenderCommandType::Line; - cmd.data = LineCommandData{points_[0] + offset, points_[1] + offset, color_, - lineWidth_}; + cmd.data = LineCommandData{points_[0] + offset, points_[1] + offset, + color_, lineWidth_}; } break; @@ -380,14 +375,14 @@ void ShapeNode::generateRenderCommand(std::vector &commands, cmd.type = RenderCommandType::FilledRect; Rect rect(points_[0].x, points_[0].y, points_[2].x - points_[0].x, points_[2].y - points_[0].y); - cmd.data = - RectCommandData{Rect(rect.origin + offset, rect.size), color_, 0.0f, true}; + cmd.data = RectCommandData{Rect(rect.origin + offset, rect.size), + color_, 0.0f, true}; } else { cmd.type = RenderCommandType::Rect; Rect rect(points_[0].x, points_[0].y, points_[2].x - points_[0].x, points_[2].y - points_[0].y); - cmd.data = - RectCommandData{Rect(rect.origin + offset, rect.size), color_, lineWidth_, false}; + cmd.data = RectCommandData{Rect(rect.origin + offset, rect.size), + color_, lineWidth_, false}; } } break; @@ -397,12 +392,12 @@ void ShapeNode::generateRenderCommand(std::vector &commands, float radius = points_[1].x; if (filled_) { cmd.type = RenderCommandType::FilledCircle; - cmd.data = - CircleCommandData{points_[0] + offset, radius, color_, segments_, 0.0f, true}; + cmd.data = CircleCommandData{points_[0] + offset, radius, color_, + segments_, 0.0f, true}; } else { cmd.type = RenderCommandType::Circle; - cmd.data = CircleCommandData{points_[0] + offset, radius, color_, segments_, - lineWidth_, false}; + cmd.data = CircleCommandData{points_[0] + offset, radius, color_, + segments_, lineWidth_, false}; } } break; @@ -435,7 +430,8 @@ void ShapeNode::generateRenderCommand(std::vector &commands, cmd.data = PolygonCommandData{transformedPoints, color_, 0.0f, true}; } else { cmd.type = RenderCommandType::Polygon; - cmd.data = PolygonCommandData{transformedPoints, color_, lineWidth_, false}; + cmd.data = + PolygonCommandData{transformedPoints, color_, lineWidth_, false}; } } break; diff --git a/Extra2D/src/scene/sprite.cpp b/Extra2D/src/scene/sprite.cpp index eae5658..865d0eb 100644 --- a/Extra2D/src/scene/sprite.cpp +++ b/Extra2D/src/scene/sprite.cpp @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include #include @@ -127,7 +127,7 @@ Rect Sprite::getBounds() const { * * 使用世界变换计算最终位置、缩放和旋转,然后绘制精灵 */ -void Sprite::onDraw(RenderBackend &renderer) { +void Sprite::onDraw(Renderer &renderer) { if (!texture_ || !texture_->isValid()) { return; } @@ -151,7 +151,7 @@ void Sprite::onDraw(RenderBackend &renderer) { auto anchor = getAnchor(); - // 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸 + // 锚点由 Renderer 在绘制时处理,这里只传递位置和尺寸 Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY); // Adjust source rect for flipping @@ -204,7 +204,7 @@ void Sprite::generateRenderCommand(std::vector &commands, auto anchor = getAnchor(); - // 锚点由 RenderBackend 在绘制时处理,这里只传递位置和尺寸 + // 锚点由 Renderer 在绘制时处理,这里只传递位置和尺寸 Rect destRect(worldX, worldY, width * worldScaleX, height * worldScaleY); // 调整源矩形(翻转) diff --git a/Extra2D/src/scene/transition_box_scene.cpp b/Extra2D/src/scene/transition_box_scene.cpp index 53f31fa..b541d4c 100644 --- a/Extra2D/src/scene/transition_box_scene.cpp +++ b/Extra2D/src/scene/transition_box_scene.cpp @@ -1,11 +1,12 @@ -#include +#include #include #include -#include +#include #include -#include +#include #include + namespace extra2d { /** @@ -25,9 +26,8 @@ TransitionBoxScene::TransitionBoxScene(float duration, Ptr inScene, * @param divisions 方块分割数 * @return 过渡场景智能指针 */ -Ptr TransitionBoxScene::create(float duration, - Ptr inScene, - int divisions) { +Ptr +TransitionBoxScene::create(float duration, Ptr inScene, int divisions) { return makePtr(duration, inScene, divisions); } @@ -36,8 +36,7 @@ Ptr TransitionBoxScene::create(float duration, * * 方块过渡不需要特殊的初始化 */ -void TransitionBoxScene::onTransitionStart() { -} +void TransitionBoxScene::onTransitionStart() {} /** * @brief 更新过渡进度 @@ -53,7 +52,7 @@ void TransitionBoxScene::updateTransition(float dt) { * * 先渲染新场景,然后绘制方块遮罩逐渐消失 */ -void TransitionBoxScene::renderContent(RenderBackend &renderer) { +void TransitionBoxScene::renderContent(Renderer &renderer) { auto &app = Application::get(); float windowWidth = static_cast(app.window()->width()); float windowHeight = static_cast(app.window()->height()); diff --git a/Extra2D/src/scene/transition_fade_scene.cpp b/Extra2D/src/scene/transition_fade_scene.cpp index 24e6bf6..63dfd75 100644 --- a/Extra2D/src/scene/transition_fade_scene.cpp +++ b/Extra2D/src/scene/transition_fade_scene.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -50,7 +50,7 @@ void TransitionFadeScene::updateTransition(float dt) { } } -void TransitionFadeScene::renderContent(RenderBackend &renderer) { +void TransitionFadeScene::renderContent(Renderer &renderer) { auto &app = Application::get(); float windowWidth = static_cast(app.window()->width()); float windowHeight = static_cast(app.window()->height()); diff --git a/Extra2D/src/scene/transition_flip_scene.cpp b/Extra2D/src/scene/transition_flip_scene.cpp index 7b0119a..44763ab 100644 --- a/Extra2D/src/scene/transition_flip_scene.cpp +++ b/Extra2D/src/scene/transition_flip_scene.cpp @@ -1,7 +1,8 @@ -#include #include #include -#include +#include +#include + namespace extra2d { @@ -22,9 +23,8 @@ TransitionFlipScene::TransitionFlipScene(float duration, Ptr inScene, * @param axis 翻转轴 * @return 过渡场景智能指针 */ -Ptr TransitionFlipScene::create(float duration, - Ptr inScene, - Axis axis) { +Ptr +TransitionFlipScene::create(float duration, Ptr inScene, Axis axis) { return makePtr(duration, inScene, axis); } @@ -33,8 +33,7 @@ Ptr TransitionFlipScene::create(float duration, * * 翻页过渡不需要特殊的初始化 */ -void TransitionFlipScene::onTransitionStart() { -} +void TransitionFlipScene::onTransitionStart() {} /** * @brief 更新过渡进度 @@ -50,9 +49,10 @@ void TransitionFlipScene::updateTransition(float dt) { * * 根据进度控制新旧场景的翻转角度 */ -void TransitionFlipScene::renderContent(RenderBackend &renderer) { - float easeProgress = progress_ < 0.5f ? 2.0f * progress_ * progress_ - : -1.0f + (4.0f - 2.0f * progress_) * progress_; +void TransitionFlipScene::renderContent(Renderer &renderer) { + float easeProgress = progress_ < 0.5f + ? 2.0f * progress_ * progress_ + : -1.0f + (4.0f - 2.0f * progress_) * progress_; float angle = easeProgress * PI_F; @@ -103,7 +103,6 @@ void TransitionFlipScene::renderContent(RenderBackend &renderer) { } } } - } } // namespace extra2d diff --git a/Extra2D/src/scene/transition_scale_scene.cpp b/Extra2D/src/scene/transition_scale_scene.cpp index 7ca6de6..9da0e96 100644 --- a/Extra2D/src/scene/transition_scale_scene.cpp +++ b/Extra2D/src/scene/transition_scale_scene.cpp @@ -1,7 +1,8 @@ -#include -#include -#include #include +#include +#include +#include + namespace extra2d { @@ -29,8 +30,7 @@ Ptr TransitionScaleScene::create(float duration, * * 缩放过渡不需要特殊的初始化 */ -void TransitionScaleScene::onTransitionStart() { -} +void TransitionScaleScene::onTransitionStart() {} /** * @brief 更新过渡进度 @@ -46,9 +46,10 @@ void TransitionScaleScene::updateTransition(float dt) { * * 根据进度控制新旧场景的缩放比例 */ -void TransitionScaleScene::renderContent(RenderBackend &renderer) { - float easeProgress = progress_ < 0.5f ? 2.0f * progress_ * progress_ - : -1.0f + (4.0f - 2.0f * progress_) * progress_; +void TransitionScaleScene::renderContent(Renderer &renderer) { + float easeProgress = progress_ < 0.5f + ? 2.0f * progress_ * progress_ + : -1.0f + (4.0f - 2.0f * progress_) * progress_; if (outScene_) { float scale = std::max(0.01f, 1.0f - easeProgress); @@ -83,7 +84,6 @@ void TransitionScaleScene::renderContent(RenderBackend &renderer) { camera->setZoom(originalZoom); } } - } } // namespace extra2d diff --git a/Extra2D/src/scene/transition_scene.cpp b/Extra2D/src/scene/transition_scene.cpp index a5b6df2..9e2c54e 100644 --- a/Extra2D/src/scene/transition_scene.cpp +++ b/Extra2D/src/scene/transition_scene.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -119,7 +119,7 @@ void TransitionScene::cancel(bool immediate) { * * 默认先渲染退出场景,再渲染进入场景 */ -void TransitionScene::renderContent(RenderBackend &renderer) { +void TransitionScene::renderContent(Renderer &renderer) { drawOutScene(renderer); drawInScene(renderer); } @@ -128,7 +128,7 @@ void TransitionScene::renderContent(RenderBackend &renderer) { * @brief 绘制退出场景 * @param renderer 渲染后端引用 */ -void TransitionScene::drawOutScene(RenderBackend &renderer) { +void TransitionScene::drawOutScene(Renderer &renderer) { if (outScene_) { outScene_->renderContent(renderer); } @@ -138,7 +138,7 @@ void TransitionScene::drawOutScene(RenderBackend &renderer) { * @brief 绘制进入场景 * @param renderer 渲染后端引用 */ -void TransitionScene::drawInScene(RenderBackend &renderer) { +void TransitionScene::drawInScene(Renderer &renderer) { if (inScene_) { inScene_->renderContent(renderer); } diff --git a/Extra2D/src/scene/transition_slide_scene.cpp b/Extra2D/src/scene/transition_slide_scene.cpp index 85d7f0b..c001110 100644 --- a/Extra2D/src/scene/transition_slide_scene.cpp +++ b/Extra2D/src/scene/transition_slide_scene.cpp @@ -1,7 +1,8 @@ -#include -#include -#include #include +#include +#include +#include + namespace extra2d { @@ -22,8 +23,9 @@ TransitionSlideScene::TransitionSlideScene(float duration, Ptr inScene, * @param direction 滑动方向 * @return 过渡场景智能指针 */ -Ptr TransitionSlideScene::create( - float duration, Ptr inScene, TransitionDirection direction) { +Ptr +TransitionSlideScene::create(float duration, Ptr inScene, + TransitionDirection direction) { return makePtr(duration, inScene, direction); } @@ -32,8 +34,7 @@ Ptr TransitionSlideScene::create( * * 滑动过渡不需要特殊的初始化 */ -void TransitionSlideScene::onTransitionStart() { -} +void TransitionSlideScene::onTransitionStart() {} /** * @brief 更新过渡进度 @@ -49,7 +50,7 @@ void TransitionSlideScene::updateTransition(float dt) { * * 根据进度控制新旧场景的滑动位置 */ -void TransitionSlideScene::renderContent(RenderBackend &renderer) { +void TransitionSlideScene::renderContent(Renderer &renderer) { float screenWidth = 800.0f; float screenHeight = 600.0f; @@ -67,8 +68,9 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) { } } - float easeProgress = progress_ < 0.5f ? 2.0f * progress_ * progress_ - : -1.0f + (4.0f - 2.0f * progress_) * progress_; + float easeProgress = progress_ < 0.5f + ? 2.0f * progress_ * progress_ + : -1.0f + (4.0f - 2.0f * progress_) * progress_; if (outScene_) { float offsetX = 0.0f; @@ -135,7 +137,6 @@ void TransitionSlideScene::renderContent(RenderBackend &renderer) { camera->setPos(originalPos); } } - } } // namespace extra2d diff --git a/Extra2D/src/services/scene_service.cpp b/Extra2D/src/services/scene_service.cpp index 854f1bd..b59db17 100644 --- a/Extra2D/src/services/scene_service.cpp +++ b/Extra2D/src/services/scene_service.cpp @@ -68,9 +68,7 @@ bool SceneService::hasScene(const std::string &name) const { return manager_.hasScene(name); } -void SceneService::render(RenderBackend &renderer) { - manager_.render(renderer); -} +void SceneService::render(Renderer &renderer) { manager_.render(renderer); } void SceneService::collectRenderCommands(std::vector &commands) { manager_.collectRenderCommands(commands); diff --git a/README.md b/README.md index 075b41f..243e755 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ flowchart TB subgraph Graphics["Graphics (图形系统)"] direction TB RESOURCES[Resources
资源抽象层] - BACKEND[RenderBackend
渲染后端] + BACKEND[Renderer
渲染后端] BATCH[Batch Layer
批处理层] GL[OpenGL Backend
OpenGL 后端] VK[Vulkan Backend
Vulkan 后端] diff --git a/docs/module_system.md b/docs/module_system.md index ef8b3e6..1930003 100644 --- a/docs/module_system.md +++ b/docs/module_system.md @@ -229,9 +229,9 @@ cameraService->setZoom(2.0f); ### 渲染后端接口 ```cpp -class RenderBackend { +class Renderer { public: - virtual ~RenderBackend() = default; + virtual ~Renderer() = default; // 生命周期 virtual bool init(IWindow* window) = 0; @@ -276,7 +276,7 @@ public: virtual void drawText(const FontAtlas &font, const std::string &text, const Vec2 &position, const Color &color) = 0; // 工厂方法 - static UniquePtr create(BackendType type); + static UniquePtr create(BackendType type); }; ``` @@ -407,7 +407,7 @@ public: virtual void onEnter(); virtual void onExit(); virtual void onUpdate(float dt); - virtual void onRender(RenderBackend& renderer); + virtual void onRender(Renderer& renderer); }; ``` diff --git a/examples/basic/main.cpp b/examples/basic/main.cpp index 29f2913..70cb204 100644 --- a/examples/basic/main.cpp +++ b/examples/basic/main.cpp @@ -90,7 +90,7 @@ public: * @brief 自定义渲染逻辑 * @param renderer 渲染后端 */ - void onRender(RenderBackend &renderer) override { Scene::onRender(renderer); } + void onRender(Renderer &renderer) override { Scene::onRender(renderer); } }; int main(int argc, char *argv[]) { @@ -107,9 +107,7 @@ int main(int argc, char *argv[]) { cfg.priority = 0; }); - app.use([](auto &cfg) { - cfg.priority = 10; - }); + app.use([](auto &cfg) { cfg.priority = 10; }); std::cout << "Initializing application..." << std::endl; if (!app.init()) { diff --git a/examples/hello_module/main.cpp b/examples/hello_module/main.cpp index b0361ea..8236708 100644 --- a/examples/hello_module/main.cpp +++ b/examples/hello_module/main.cpp @@ -29,7 +29,7 @@ public: time_ = 0.0f; } } - void onRender(RenderBackend &renderer) override { Scene::onRender(renderer); } + void onRender(Renderer &renderer) override { Scene::onRender(renderer); } private: float time_ = 0.0f; @@ -44,9 +44,7 @@ int main(int argc, char *argv[]) { cfg.w = 800; cfg.h = 600; }); - app.use([](auto &cfg) { - cfg.priority = 10; - }); + app.use([](auto &cfg) { cfg.priority = 10; }); app.use([](auto &cfg) { cfg.greeting = "Hello from custom module!"; cfg.repeatCount = 3; diff --git a/examples/image_display/main.cpp b/examples/image_display/main.cpp index f27bfce..64949bf 100644 --- a/examples/image_display/main.cpp +++ b/examples/image_display/main.cpp @@ -2,7 +2,7 @@ * @file main.cpp * @brief Extra2D 图片显示示例 * - * 演示如何使用 RenderBackend 抽象接口加载和显示图片 + * 演示如何使用 Renderer 抽象接口加载和显示图片 * 此示例不依赖任何特定渲染后端(如 OpenGL) */ @@ -40,14 +40,14 @@ public: void onExit() override { texture_.reset(); } - void onRender(RenderBackend &renderer) override { + void onRender(Renderer &renderer) override { Scene::onRender(renderer); if (!texture_) { return; } - // 使用 RenderBackend 的抽象接口绘制图片 + // 使用 Renderer 的抽象接口绘制图片 // 不依赖任何特定后端(如 OpenGL) // 自动批处理:无需手动调用 begin/endSpriteBatch @@ -70,7 +70,7 @@ public: float x = (windowWidth - displayWidth) * 0.5f; float y = (windowHeight - displayHeight) * 0.5f; - // 使用 RenderBackend 的 drawSprite 方法绘制图片 + // 使用 Renderer 的 drawSprite 方法绘制图片 // 参数:纹理、目标矩形、源矩形、颜色、旋转角度、锚点 Rect destRect(x, y, displayWidth, displayHeight); Rect srcRect(0, 0, imgWidth, imgHeight); @@ -80,11 +80,11 @@ public: // 注意:无需手动调用 renderer.endSpriteBatch(),帧结束时会自动刷新 } - void setRenderer(RenderBackend *renderer) { renderer_ = renderer; } + void setRenderer(Renderer *renderer) { renderer_ = renderer; } private: Ptr texture_; - RenderBackend *renderer_ = nullptr; + Renderer *renderer_ = nullptr; }; int main(int argc, char *argv[]) { @@ -103,9 +103,7 @@ int main(int argc, char *argv[]) { cfg.priority = 0; }); - app.use([](auto &cfg) { - cfg.priority = 10; - }); + app.use([](auto &cfg) { cfg.priority = 10; }); if (!app.init()) { std::cerr << "Failed to initialize application!" << std::endl; @@ -127,7 +125,7 @@ int main(int argc, char *argv[]) { } // 获取渲染器 - RenderBackend *renderer = app.renderer(); + Renderer *renderer = app.renderer(); // 创建并配置场景 auto scene = makeShared(); diff --git a/examples/text_rendering/main.cpp b/examples/text_rendering/main.cpp index 2265dfd..004cdc4 100644 --- a/examples/text_rendering/main.cpp +++ b/examples/text_rendering/main.cpp @@ -2,7 +2,7 @@ * @file main.cpp * @brief Extra2D 文字渲染示例 * - * 演示如何使用 RenderBackend 抽象接口渲染文字 + * 演示如何使用 Renderer 抽象接口渲染文字 * 此示例不依赖任何特定渲染后端(如 OpenGL) */ @@ -40,7 +40,7 @@ public: void onExit() override { font_.reset(); } - void onRender(RenderBackend &renderer) override { + void onRender(Renderer &renderer) override { Scene::onRender(renderer); if (!font_) { @@ -85,21 +85,21 @@ public: Color(0.5f, 0.5f, 0.5f, 1.0f)); } - void setRenderer(RenderBackend *renderer) { renderer_ = renderer; } + void setRenderer(Renderer *renderer) { renderer_ = renderer; } private: - void renderText(RenderBackend &renderer, const std::string &text, float x, - float y, const Color &color) { + void renderText(Renderer &renderer, const std::string &text, float x, float y, + const Color &color) { if (!font_) { return; } - // 使用 RenderBackend 的 drawText 方法 + // 使用 Renderer 的 drawText 方法 renderer.drawText(*font_, text, Vec2(x, y), color); } Ptr font_; - RenderBackend *renderer_ = nullptr; + Renderer *renderer_ = nullptr; }; int main(int argc, char *argv[]) { @@ -139,7 +139,7 @@ int main(int argc, char *argv[]) { } // 获取渲染器 - RenderBackend *renderer = app.renderer(); + Renderer *renderer = app.renderer(); // 创建并配置场景 auto scene = makeShared();