diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_font_atlas.h b/Extra2D/include/extra2d/graphics/backends/opengl/gl_font_atlas.h deleted file mode 100644 index 72d286c..0000000 --- a/Extra2D/include/extra2d/graphics/backends/opengl/gl_font_atlas.h +++ /dev/null @@ -1,84 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -#include -#include - -namespace extra2d { - -// ============================================================================ -// OpenGL 字体图集实现 (使用 STB 库) -// 使用 stb_rect_pack 进行动态矩形打包,支持动态缓存字形 -// ============================================================================ -class GLFontAtlas : public FontAtlas { -public: - GLFontAtlas(const std::string& filepath, int fontSize, bool useSDF = false); - ~GLFontAtlas() override; - - // FontAtlas 接口实现 - const Glyph* getGlyph(char32_t codepoint) const override; - Texture* getTexture() const override { return texture_.get(); } - int getFontSize() const override { return fontSize_; } - float getAscent() const override { return ascent_; } - float getDescent() const override { return descent_; } - float getLineGap() const override { return lineGap_; } - float getLineHeight() const override { return lineHeight_; } - bool isSDF() const override { return useSDF_; } - Vec2 measureText(const std::string& text) override; - -private: - // 字形数据内部结构 - struct GlyphData { - float width; - float height; - float bearingX; - float bearingY; - float advance; - float u0, v0, u1, v1; - }; - - // 图集配置 - 增大尺寸以支持更多字符 - static constexpr int ATLAS_WIDTH = 1024; - static constexpr int ATLAS_HEIGHT = 1024; - static constexpr int PADDING = 2; // 字形之间的间距 - - bool useSDF_; - int fontSize_; - - Ptr texture_; - std::unordered_map glyphs_; - float lineHeight_; - float ascent_; - float descent_; - float lineGap_; - - // 字体数据 - std::vector fontData_; - stbtt_fontinfo fontInfo_; - float scale_; - - // stb_rect_pack 上下文 - 持久化以支持增量打包 - mutable stbrp_context packContext_; - mutable std::vector packNodes_; - - // 预分配缓冲区,避免每次动态分配 - mutable std::vector glyphBitmapCache_; - mutable std::vector glyphRgbaCache_; - - // 初始化字体 - bool initFont(const std::string& filepath); - // 创建空白图集纹理 - void createAtlas(); - // 缓存字形到图集 - void cacheGlyph(char32_t codepoint); - // 更新图集纹理区域 - void updateAtlas(int x, int y, int width, int height, - const std::vector& data); -}; - -} // namespace extra2d diff --git a/Extra2D/include/extra2d/graphics/core/render_target.h b/Extra2D/include/extra2d/graphics/core/render_target.h index c7dca31..b511e8f 100644 --- a/Extra2D/include/extra2d/graphics/core/render_target.h +++ b/Extra2D/include/extra2d/graphics/core/render_target.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -257,7 +257,7 @@ public: /** * @brief 获取单例实例 */ - static RenderTargetMgr& get(); + static RenderTargetMgr &get(); /** * @brief 初始化渲染目标管理器 diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_buffer.h b/Extra2D/include/extra2d/graphics/opengl/gl_buffer.h similarity index 100% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_buffer.h rename to Extra2D/include/extra2d/graphics/opengl/gl_buffer.h diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_context.h b/Extra2D/include/extra2d/graphics/opengl/gl_context.h similarity index 100% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_context.h rename to Extra2D/include/extra2d/graphics/opengl/gl_context.h diff --git a/Extra2D/include/extra2d/graphics/opengl/gl_font_atlas.h b/Extra2D/include/extra2d/graphics/opengl/gl_font_atlas.h new file mode 100644 index 0000000..cb9bd6e --- /dev/null +++ b/Extra2D/include/extra2d/graphics/opengl/gl_font_atlas.h @@ -0,0 +1,85 @@ +#pragma once + +#include +#include + +#include +#include + + +#include +#include + +namespace extra2d { + +// ============================================================================ +// OpenGL 字体图集实现 (使用 STB 库) +// 使用 stb_rect_pack 进行动态矩形打包,支持动态缓存字形 +// ============================================================================ +class GLFontAtlas : public FontAtlas { +public: + GLFontAtlas(const std::string &filepath, int fontSize, bool useSDF = false); + ~GLFontAtlas() override; + + // FontAtlas 接口实现 + const Glyph *getGlyph(char32_t codepoint) const override; + Texture *getTexture() const override { return texture_.get(); } + int getFontSize() const override { return fontSize_; } + float getAscent() const override { return ascent_; } + float getDescent() const override { return descent_; } + float getLineGap() const override { return lineGap_; } + float getLineHeight() const override { return lineHeight_; } + bool isSDF() const override { return useSDF_; } + Vec2 measureText(const std::string &text) override; + +private: + // 字形数据内部结构 + struct GlyphData { + float width; + float height; + float bearingX; + float bearingY; + float advance; + float u0, v0, u1, v1; + }; + + // 图集配置 - 增大尺寸以支持更多字符 + static constexpr int ATLAS_WIDTH = 1024; + static constexpr int ATLAS_HEIGHT = 1024; + static constexpr int PADDING = 2; // 字形之间的间距 + + bool useSDF_; + int fontSize_; + + Ptr texture_; + std::unordered_map glyphs_; + float lineHeight_; + float ascent_; + float descent_; + float lineGap_; + + // 字体数据 + std::vector fontData_; + stbtt_fontinfo fontInfo_; + float scale_; + + // stb_rect_pack 上下文 - 持久化以支持增量打包 + mutable stbrp_context packContext_; + mutable std::vector packNodes_; + + // 预分配缓冲区,避免每次动态分配 + mutable std::vector glyphBitmapCache_; + mutable std::vector glyphRgbaCache_; + + // 初始化字体 + bool initFont(const std::string &filepath); + // 创建空白图集纹理 + void createAtlas(); + // 缓存字形到图集 + void cacheGlyph(char32_t codepoint); + // 更新图集纹理区域 + void updateAtlas(int x, int y, int width, int height, + const std::vector &data); +}; + +} // namespace extra2d diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_framebuffer.h b/Extra2D/include/extra2d/graphics/opengl/gl_framebuffer.h similarity index 100% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_framebuffer.h rename to Extra2D/include/extra2d/graphics/opengl/gl_framebuffer.h diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_pipeline.h b/Extra2D/include/extra2d/graphics/opengl/gl_pipeline.h similarity index 100% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_pipeline.h rename to Extra2D/include/extra2d/graphics/opengl/gl_pipeline.h diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_renderer.h b/Extra2D/include/extra2d/graphics/opengl/gl_renderer.h similarity index 78% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_renderer.h rename to Extra2D/include/extra2d/graphics/opengl/gl_renderer.h index 67869f1..609a6ca 100644 --- a/Extra2D/include/extra2d/graphics/backends/opengl/gl_renderer.h +++ b/Extra2D/include/extra2d/graphics/opengl/gl_renderer.h @@ -1,12 +1,13 @@ #pragma once -#include -#include -#include -#include #include +#include +#include +#include +#include #include + #include #include #include @@ -27,7 +28,7 @@ public: ~GLRenderer() override; // RenderBackend 接口实现 - bool init(IWindow* window) override; + bool init(IWindow *window) override; void shutdown() override; void beginFrame(const Color &clearColor) override; @@ -90,13 +91,13 @@ public: * @param desc 帧缓冲描述 * @return 创建的帧缓冲智能指针 */ - Ptr createFramebuffer(const FramebufferDesc& desc); + Ptr createFramebuffer(const FramebufferDesc &desc); /** * @brief 绑定帧缓冲(作为渲染目标) * @param framebuffer 帧缓冲对象指针,传入 nullptr 则绑定默认帧缓冲 */ - void bindFramebuffer(GLFramebuffer* framebuffer); + void bindFramebuffer(GLFramebuffer *framebuffer); /** * @brief 解绑帧缓冲(恢复到默认帧缓冲) @@ -116,7 +117,7 @@ public: * @param clearDepth 是否清除深度缓冲 * @param clearStencil 是否清除模板缓冲 */ - void clearFramebuffer(const Color& color, bool clearColor = true, + void clearFramebuffer(const Color &color, bool clearColor = true, bool clearDepth = true, bool clearStencil = false); private: @@ -131,15 +132,15 @@ private: float r, g, b, a; }; - IWindow* window_; + IWindow *window_; GLSpriteBatch spriteBatch_; Ptr shapeShader_; - Ptr sdfFontShader_; // SDF字体专用着色器 + Ptr sdfFontShader_; // SDF字体专用着色器 - GLuint shapeVao_; // 形状 VAO(手动管理,用于顶点属性配置) - GLBuffer shapeBuffer_; // 形状 VBO(使用 GLBuffer 管理) - GLuint lineVao_; // 线条 VAO(手动管理,用于顶点属性配置) - GLBuffer lineBuffer_; // 线条 VBO(使用 GLBuffer 管理) + GLuint shapeVao_; // 形状 VAO(手动管理,用于顶点属性配置) + GLBuffer shapeBuffer_; // 形状 VBO(使用 GLBuffer 管理) + GLuint lineVao_; // 线条 VAO(手动管理,用于顶点属性配置) + GLBuffer lineBuffer_; // 线条 VBO(使用 GLBuffer 管理) glm::mat4 viewProjection_; std::vector transformStack_; @@ -160,19 +161,19 @@ private: GLPipeline pipeline_; // 自动批处理状态 - bool batchActive_ = false; // 批处理是否激活 - bool autoBatchEnabled_ = true; // 是否启用自动批处理 - const Texture* currentBatchTexture_ = nullptr; // 当前批处理的纹理 - std::vector pendingSprites_; // 待提交的精灵 + 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/backends/opengl/gl_shader.h b/Extra2D/include/extra2d/graphics/opengl/gl_shader.h similarity index 100% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_shader.h rename to Extra2D/include/extra2d/graphics/opengl/gl_shader.h diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_sprite_batch.h b/Extra2D/include/extra2d/graphics/opengl/gl_sprite_batch.h similarity index 91% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_sprite_batch.h rename to Extra2D/include/extra2d/graphics/opengl/gl_sprite_batch.h index e2b5650..b05acff 100644 --- a/Extra2D/include/extra2d/graphics/backends/opengl/gl_sprite_batch.h +++ b/Extra2D/include/extra2d/graphics/opengl/gl_sprite_batch.h @@ -1,11 +1,12 @@ #pragma once -#include -#include #include +#include +#include #include #include + #include #include @@ -48,7 +49,9 @@ public: Ptr getShader() const { return shader_; } // 设置额外的uniform值(用于SDF字体等特殊渲染) - void setExtraUniforms(const UniformValueMap &uniforms) { extraUniforms_ = uniforms; } + void setExtraUniforms(const UniformValueMap &uniforms) { + extraUniforms_ = uniforms; + } void clearExtraUniforms() { extraUniforms_.clear(); } private: diff --git a/Extra2D/include/extra2d/graphics/backends/opengl/gl_texture.h b/Extra2D/include/extra2d/graphics/opengl/gl_texture.h similarity index 100% rename from Extra2D/include/extra2d/graphics/backends/opengl/gl_texture.h rename to Extra2D/include/extra2d/graphics/opengl/gl_texture.h diff --git a/Extra2D/include/extra2d/graphics/texture/texture_atlas.h b/Extra2D/include/extra2d/graphics/texture/texture_atlas.h index 8fca067..fdef05a 100644 --- a/Extra2D/include/extra2d/graphics/texture/texture_atlas.h +++ b/Extra2D/include/extra2d/graphics/texture/texture_atlas.h @@ -3,12 +3,13 @@ #include #include #include +#include #include -#include -#include -#include -#include #include +#include +#include +#include + namespace extra2d { @@ -20,11 +21,11 @@ namespace extra2d { * @brief 图集中的单个纹理条目 */ struct AtlasEntry { - std::string name; // 原始纹理名称/路径 - Rect uvRect; // 在图集中的 UV 坐标范围 - Vec2 originalSize; // 原始纹理尺寸 - uint32_t padding; // 边距(用于避免纹理 bleeding) - + std::string name; // 原始纹理名称/路径 + Rect uvRect; // 在图集中的 UV 坐标范围 + Vec2 originalSize; // 原始纹理尺寸 + uint32_t padding; // 边距(用于避免纹理 bleeding) + AtlasEntry() : uvRect(), originalSize(), padding(2) {} }; @@ -36,56 +37,56 @@ class TextureAtlasPage { public: static constexpr int DEFAULT_SIZE = 2048; static constexpr int MAX_SIZE = 4096; - static constexpr int MIN_TEXTURE_SIZE = 32; // 小于此大小的纹理才考虑合并 - static constexpr int PADDING = 2; // 纹理间边距 - + static constexpr int MIN_TEXTURE_SIZE = 32; // 小于此大小的纹理才考虑合并 + static constexpr int PADDING = 2; // 纹理间边距 + TextureAtlasPage(int width = DEFAULT_SIZE, int height = DEFAULT_SIZE); ~TextureAtlasPage(); - + // 尝试添加纹理到图集 // 返回是否成功,如果成功则输出 uvRect - bool tryAddTexture(const std::string& name, int texWidth, int texHeight, - const uint8_t* pixels, Rect& outUvRect); - + bool tryAddTexture(const std::string &name, int texWidth, int texHeight, + const uint8_t *pixels, Rect &outUvRect); + // 获取图集纹理 Ptr getTexture() const { return texture_; } - + // 获取条目 - const AtlasEntry* getEntry(const std::string& name) const; - + const AtlasEntry *getEntry(const std::string &name) const; + // 获取使用率 float getUsageRatio() const; - + // 获取尺寸 int getWidth() const { return width_; } int getHeight() const { return height_; } - + // 是否已满 bool isFull() const { return isFull_; } - + private: int width_, height_; Ptr texture_; std::unordered_map entries_; - + // 矩形打包数据 struct PackNode { int x, y, width, height; bool used; std::unique_ptr left; std::unique_ptr right; - - PackNode(int x_, int y_, int w, int h) - : x(x_), y(y_), width(w), height(h), used(false) {} + + PackNode(int x_, int y_, int w, int h) + : x(x_), y(y_), width(w), height(h), used(false) {} }; - + std::unique_ptr root_; bool isFull_; int usedArea_; - + // 递归插入 - PackNode* insert(PackNode* node, int width, int height); - void writePixels(int x, int y, int w, int h, const uint8_t* pixels); + PackNode *insert(PackNode *node, int width, int height); + void writePixels(int x, int y, int w, int h, const uint8_t *pixels); }; /** @@ -96,47 +97,49 @@ class TextureAtlas { public: TextureAtlas(); ~TextureAtlas(); - + // 初始化 void init(int pageSize = TextureAtlasPage::DEFAULT_SIZE); - + // 添加纹理到图集 // 如果纹理太大,返回 false,应该作为独立纹理加载 - bool addTexture(const std::string& name, int width, int height, - const uint8_t* pixels); - + bool addTexture(const std::string &name, int width, int height, + const uint8_t *pixels); + // 查询纹理是否在图集中 - bool contains(const std::string& name) const; - + bool contains(const std::string &name) const; + // 获取纹理在图集中的信息 // 返回图集纹理和 UV 坐标 - const Texture* getAtlasTexture(const std::string& name) const; - Rect getUVRect(const std::string& name) const; - + const Texture *getAtlasTexture(const std::string &name) const; + Rect getUVRect(const std::string &name) const; + // 获取原始纹理尺寸 - Vec2 getOriginalSize(const std::string& name) const; - + Vec2 getOriginalSize(const std::string &name) const; + // 获取所有图集页面 - const std::vector>& getPages() const { return pages_; } - + const std::vector> &getPages() const { + return pages_; + } + // 获取总使用率 float getTotalUsageRatio() const; - + // 清空所有图集 void clear(); - + // 设置是否启用自动图集 void setEnabled(bool enabled) { enabled_ = enabled; } bool isEnabled() const { return enabled_; } - + // 设置纹理大小阈值(小于此大小的纹理才进入图集) void setSizeThreshold(int threshold) { sizeThreshold_ = threshold; } int getSizeThreshold() const { return sizeThreshold_; } private: std::vector> pages_; - std::unordered_map entryToPage_; - + std::unordered_map entryToPage_; + int pageSize_; int sizeThreshold_; bool enabled_; @@ -148,36 +151,34 @@ private: */ class TextureAtlasMgr { public: - static TextureAtlasMgr& get(); - + static TextureAtlasMgr &get(); + // 获取主图集 - TextureAtlas& getAtlas() { return atlas_; } - + TextureAtlas &getAtlas() { return atlas_; } + // 快捷方法 - bool addTexture(const std::string& name, int width, int height, - const uint8_t* pixels) { + bool addTexture(const std::string &name, int width, int height, + const uint8_t *pixels) { return atlas_.addTexture(name, width, height, pixels); } - - bool contains(const std::string& name) const { - return atlas_.contains(name); - } - - const Texture* getAtlasTexture(const std::string& name) const { + + bool contains(const std::string &name) const { return atlas_.contains(name); } + + const Texture *getAtlasTexture(const std::string &name) const { return atlas_.getAtlasTexture(name); } - - Rect getUVRect(const std::string& name) const { + + Rect getUVRect(const std::string &name) const { return atlas_.getUVRect(name); } private: TextureAtlasMgr() = default; ~TextureAtlasMgr() = default; - - TextureAtlasMgr(const TextureAtlasMgr&) = delete; - TextureAtlasMgr& operator=(const TextureAtlasMgr&) = delete; - + + TextureAtlasMgr(const TextureAtlasMgr &) = delete; + TextureAtlasMgr &operator=(const TextureAtlasMgr &) = delete; + TextureAtlas atlas_; }; diff --git a/Extra2D/src/platform/glfw/glfw_input.h b/Extra2D/include/extra2d/platform/glfw/glfw_input.h similarity index 100% rename from Extra2D/src/platform/glfw/glfw_input.h rename to Extra2D/include/extra2d/platform/glfw/glfw_input.h diff --git a/Extra2D/src/platform/glfw/glfw_window.h b/Extra2D/include/extra2d/platform/glfw/glfw_window.h similarity index 100% rename from Extra2D/src/platform/glfw/glfw_window.h rename to Extra2D/include/extra2d/platform/glfw/glfw_window.h diff --git a/Extra2D/src/graphics/core/render_module.cpp b/Extra2D/src/graphics/core/render_module.cpp index cb9b297..9a628fa 100644 --- a/Extra2D/src/graphics/core/render_module.cpp +++ b/Extra2D/src/graphics/core/render_module.cpp @@ -1,9 +1,9 @@ #include #include #include -#include -#include #include +#include +#include #include #include #include diff --git a/Extra2D/src/graphics/core/render_target.cpp b/Extra2D/src/graphics/core/render_target.cpp index d7abeaf..8e1d391 100644 --- a/Extra2D/src/graphics/core/render_target.cpp +++ b/Extra2D/src/graphics/core/render_target.cpp @@ -1,10 +1,9 @@ #include -#include #include +#include #include #include - #define STB_IMAGE_WRITE_IMPLEMENTATION #include diff --git a/Extra2D/src/graphics/opengl/gl_buffer.cpp b/Extra2D/src/graphics/opengl/gl_buffer.cpp index 71d3eaa..be72dc7 100644 --- a/Extra2D/src/graphics/opengl/gl_buffer.cpp +++ b/Extra2D/src/graphics/opengl/gl_buffer.cpp @@ -1,10 +1,8 @@ #include -#include -#include #include +#include #include - namespace extra2d { // ============================================================================ @@ -42,9 +40,8 @@ bool GLBuffer::init(const BufferDesc &desc) { // 追踪显存使用 VRAMMgr::get().allocBuffer(size_); - E2D_LOG_DEBUG("GLBuffer 已创建: ID={}, 大小={}, 类型={}, 用途={}", - bufferID_, size_, static_cast(type_), - static_cast(usage_)); + E2D_LOG_DEBUG("GLBuffer 已创建: ID={}, 大小={}, 类型={}, 用途={}", bufferID_, + size_, static_cast(type_), static_cast(usage_)); return true; } diff --git a/Extra2D/src/graphics/opengl/gl_context.cpp b/Extra2D/src/graphics/opengl/gl_context.cpp index fc7d2b3..1bf12b0 100644 --- a/Extra2D/src/graphics/opengl/gl_context.cpp +++ b/Extra2D/src/graphics/opengl/gl_context.cpp @@ -1,6 +1,5 @@ -#include -#include #include +#include #include namespace extra2d { diff --git a/Extra2D/src/graphics/opengl/gl_font_atlas.cpp b/Extra2D/src/graphics/opengl/gl_font_atlas.cpp index 3a17c24..2ad5d2d 100644 --- a/Extra2D/src/graphics/opengl/gl_font_atlas.cpp +++ b/Extra2D/src/graphics/opengl/gl_font_atlas.cpp @@ -1,5 +1,4 @@ -#include -#include +#include #include #include diff --git a/Extra2D/src/graphics/opengl/gl_framebuffer.cpp b/Extra2D/src/graphics/opengl/gl_framebuffer.cpp index a8cbb56..292936a 100644 --- a/Extra2D/src/graphics/opengl/gl_framebuffer.cpp +++ b/Extra2D/src/graphics/opengl/gl_framebuffer.cpp @@ -1,9 +1,7 @@ -#include -#include -#include +#include +#include #include - namespace extra2d { // ============================================================================ @@ -37,8 +35,8 @@ bool GLFramebuffer::init(const FramebufferDesc &desc) { return false; } - E2D_LOG_DEBUG("GLFramebuffer 已创建: ID={}, 大小={}x{}, 颜色附件={}", - fboID_, width_, height_, numColorAttachments_); + E2D_LOG_DEBUG("GLFramebuffer 已创建: ID={}, 大小={}x{}, 颜色附件={}", fboID_, + width_, height_, numColorAttachments_); return true; } @@ -237,8 +235,7 @@ bool GLFramebuffer::checkStatus() { E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_UNDEFINED"); break; case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: - E2D_LOG_ERROR( - "帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"); + E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"); break; case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: E2D_LOG_ERROR( @@ -246,12 +243,10 @@ bool GLFramebuffer::checkStatus() { break; #ifndef GL_ES_VERSION_2_0 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: - E2D_LOG_ERROR( - "帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"); + E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"); break; case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: - E2D_LOG_ERROR( - "帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"); + E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"); break; #endif case GL_FRAMEBUFFER_UNSUPPORTED: diff --git a/Extra2D/src/graphics/opengl/gl_pipeline.cpp b/Extra2D/src/graphics/opengl/gl_pipeline.cpp index cc7f168..f22c8b5 100644 --- a/Extra2D/src/graphics/opengl/gl_pipeline.cpp +++ b/Extra2D/src/graphics/opengl/gl_pipeline.cpp @@ -1,8 +1,6 @@ -#include -#include +#include #include - namespace extra2d { // ============================================================================ diff --git a/Extra2D/src/graphics/opengl/gl_renderer.cpp b/Extra2D/src/graphics/opengl/gl_renderer.cpp index f4ded47..6a2ce5b 100644 --- a/Extra2D/src/graphics/opengl/gl_renderer.cpp +++ b/Extra2D/src/graphics/opengl/gl_renderer.cpp @@ -1,19 +1,19 @@ #include #include -#include -#include -#include -#include -#include -#include #include #include #include +#include +#include +#include +#include +#include #include #include #include #include + namespace extra2d { // VBO 初始大小(用于 VRAM 跟踪) diff --git a/Extra2D/src/graphics/opengl/gl_shader.cpp b/Extra2D/src/graphics/opengl/gl_shader.cpp index 6ea4bfc..19525a2 100644 --- a/Extra2D/src/graphics/opengl/gl_shader.cpp +++ b/Extra2D/src/graphics/opengl/gl_shader.cpp @@ -1,8 +1,6 @@ -#include -#include +#include #include - namespace extra2d { /** diff --git a/Extra2D/src/graphics/opengl/gl_sprite_batch.cpp b/Extra2D/src/graphics/opengl/gl_sprite_batch.cpp index fb48994..6ec1dbc 100644 --- a/Extra2D/src/graphics/opengl/gl_sprite_batch.cpp +++ b/Extra2D/src/graphics/opengl/gl_sprite_batch.cpp @@ -1,6 +1,5 @@ -#include -#include -#include +#include +#include #include #include @@ -93,7 +92,8 @@ void GLSpriteBatch::begin(const glm::mat4 &viewProjection) { ebo_.bind(); } -void GLSpriteBatch::begin(const glm::mat4 &viewProjection, Ptr shader) { +void GLSpriteBatch::begin(const glm::mat4 &viewProjection, + Ptr shader) { // 设置自定义着色器 if (shader) { shader_ = shader; @@ -183,7 +183,7 @@ void GLSpriteBatch::submitBatch() { uniformValues["u_viewProjection"] = viewProjection_; // 合并额外的uniform值(如SDF字体的u_textureSize) - for (const auto& [name, value] : extraUniforms_) { + for (const auto &[name, value] : extraUniforms_) { uniformValues[name] = value; } diff --git a/Extra2D/src/graphics/opengl/gl_texture.cpp b/Extra2D/src/graphics/opengl/gl_texture.cpp index 722b793..111db7a 100644 --- a/Extra2D/src/graphics/opengl/gl_texture.cpp +++ b/Extra2D/src/graphics/opengl/gl_texture.cpp @@ -1,6 +1,7 @@ -#include #include #include +#include + #define STB_IMAGE_IMPLEMENTATION #include #include @@ -8,7 +9,6 @@ #include #include - namespace extra2d { // ============================================================================ @@ -351,8 +351,8 @@ bool GLTexture::loadKTX(const std::string &filepath) { dataSize_ = imageSize; VRAMMgr::get().allocTexture(dataSize_); - E2D_LOG_INFO("已加载 KTX 压缩纹理: {} ({}x{}, 格式={:#06x})", - filepath, width_, height_, glInternalFormat); + E2D_LOG_INFO("已加载 KTX 压缩纹理: {} ({}x{}, 格式={:#06x})", filepath, + width_, height_, glInternalFormat); return true; } @@ -412,8 +412,7 @@ bool GLTexture::loadDDS(const std::string &filepath) { return false; } } else { - E2D_LOG_ERROR("DDS 文件未使用 DX10 扩展,不支持: {}", - filepath); + E2D_LOG_ERROR("DDS 文件未使用 DX10 扩展,不支持: {}", filepath); return false; } @@ -455,8 +454,7 @@ bool GLTexture::loadDDS(const std::string &filepath) { dataSize_ = imageSize; VRAMMgr::get().allocTexture(dataSize_); - E2D_LOG_INFO("已加载 DDS 压缩纹理: {} ({}x{})", filepath, width_, - height_); + E2D_LOG_INFO("已加载 DDS 压缩纹理: {} ({}x{})", filepath, width_, height_); return true; } diff --git a/Extra2D/src/platform/glfw/glfw_input.cpp b/Extra2D/src/platform/glfw/glfw_input.cpp index ca291fb..e787d48 100644 --- a/Extra2D/src/platform/glfw/glfw_input.cpp +++ b/Extra2D/src/platform/glfw/glfw_input.cpp @@ -1,7 +1,6 @@ -#include "glfw_input.h" #include -#include #include +#include namespace extra2d { diff --git a/Extra2D/src/platform/glfw/glfw_window.cpp b/Extra2D/src/platform/glfw/glfw_window.cpp index 9b4f846..82f6ecf 100644 --- a/Extra2D/src/platform/glfw/glfw_window.cpp +++ b/Extra2D/src/platform/glfw/glfw_window.cpp @@ -1,6 +1,6 @@ -#include "glfw_window.h" -#include "glfw_input.h" -#include +#include +#include + #include #include #include @@ -488,79 +488,225 @@ void GLFWWindow::keyCallback(GLFWwindow *window, int key, int scancode, // 将 GLFW key code 转换为引擎 Key 枚举值 Key eKey = Key::None; switch (key) { - case GLFW_KEY_A: eKey = Key::A; break; - case GLFW_KEY_B: eKey = Key::B; break; - case GLFW_KEY_C: eKey = Key::C; break; - case GLFW_KEY_D: eKey = Key::D; break; - case GLFW_KEY_E: eKey = Key::E; break; - case GLFW_KEY_F: eKey = Key::F; break; - case GLFW_KEY_G: eKey = Key::G; break; - case GLFW_KEY_H: eKey = Key::H; break; - case GLFW_KEY_I: eKey = Key::I; break; - case GLFW_KEY_J: eKey = Key::J; break; - case GLFW_KEY_K: eKey = Key::K; break; - case GLFW_KEY_L: eKey = Key::L; break; - case GLFW_KEY_M: eKey = Key::M; break; - case GLFW_KEY_N: eKey = Key::N; break; - case GLFW_KEY_O: eKey = Key::O; break; - case GLFW_KEY_P: eKey = Key::P; break; - case GLFW_KEY_Q: eKey = Key::Q; break; - case GLFW_KEY_R: eKey = Key::R; break; - case GLFW_KEY_S: eKey = Key::S; break; - case GLFW_KEY_T: eKey = Key::T; break; - case GLFW_KEY_U: eKey = Key::U; break; - case GLFW_KEY_V: eKey = Key::V; break; - case GLFW_KEY_W: eKey = Key::W; break; - case GLFW_KEY_X: eKey = Key::X; break; - case GLFW_KEY_Y: eKey = Key::Y; break; - case GLFW_KEY_Z: eKey = Key::Z; break; - case GLFW_KEY_0: eKey = Key::Num0; break; - case GLFW_KEY_1: eKey = Key::Num1; break; - case GLFW_KEY_2: eKey = Key::Num2; break; - case GLFW_KEY_3: eKey = Key::Num3; break; - case GLFW_KEY_4: eKey = Key::Num4; break; - case GLFW_KEY_5: eKey = Key::Num5; break; - case GLFW_KEY_6: eKey = Key::Num6; break; - case GLFW_KEY_7: eKey = Key::Num7; break; - case GLFW_KEY_8: eKey = Key::Num8; break; - case GLFW_KEY_9: eKey = Key::Num9; break; - case GLFW_KEY_F1: eKey = Key::F1; break; - case GLFW_KEY_F2: eKey = Key::F2; break; - case GLFW_KEY_F3: eKey = Key::F3; break; - case GLFW_KEY_F4: eKey = Key::F4; break; - case GLFW_KEY_F5: eKey = Key::F5; break; - case GLFW_KEY_F6: eKey = Key::F6; break; - case GLFW_KEY_F7: eKey = Key::F7; break; - case GLFW_KEY_F8: eKey = Key::F8; break; - case GLFW_KEY_F9: eKey = Key::F9; break; - case GLFW_KEY_F10: eKey = Key::F10; break; - case GLFW_KEY_F11: eKey = Key::F11; break; - case GLFW_KEY_F12: eKey = Key::F12; break; - case GLFW_KEY_SPACE: eKey = Key::Space; break; - case GLFW_KEY_ENTER: eKey = Key::Enter; break; - case GLFW_KEY_ESCAPE: eKey = Key::Escape; break; - case GLFW_KEY_TAB: eKey = Key::Tab; break; - case GLFW_KEY_BACKSPACE: eKey = Key::Backspace; break; - case GLFW_KEY_INSERT: eKey = Key::Insert; break; - case GLFW_KEY_DELETE: eKey = Key::Delete; break; - case GLFW_KEY_HOME: eKey = Key::Home; break; - case GLFW_KEY_END: eKey = Key::End; break; - case GLFW_KEY_PAGE_UP: eKey = Key::PageUp; break; - case GLFW_KEY_PAGE_DOWN: eKey = Key::PageDown; break; - case GLFW_KEY_UP: eKey = Key::Up; break; - case GLFW_KEY_DOWN: eKey = Key::Down; break; - case GLFW_KEY_LEFT: eKey = Key::Left; break; - case GLFW_KEY_RIGHT: eKey = Key::Right; break; - case GLFW_KEY_LEFT_SHIFT: eKey = Key::LShift; break; - case GLFW_KEY_RIGHT_SHIFT: eKey = Key::RShift; break; - case GLFW_KEY_LEFT_CONTROL: eKey = Key::LCtrl; break; - case GLFW_KEY_RIGHT_CONTROL: eKey = Key::RCtrl; break; - case GLFW_KEY_LEFT_ALT: eKey = Key::LAlt; break; - case GLFW_KEY_RIGHT_ALT: eKey = Key::RAlt; break; - case GLFW_KEY_CAPS_LOCK: eKey = Key::CapsLock; break; - case GLFW_KEY_NUM_LOCK: eKey = Key::NumLock; break; - case GLFW_KEY_SCROLL_LOCK: eKey = Key::ScrollLock; break; - default: eKey = Key::None; break; + case GLFW_KEY_A: + eKey = Key::A; + break; + case GLFW_KEY_B: + eKey = Key::B; + break; + case GLFW_KEY_C: + eKey = Key::C; + break; + case GLFW_KEY_D: + eKey = Key::D; + break; + case GLFW_KEY_E: + eKey = Key::E; + break; + case GLFW_KEY_F: + eKey = Key::F; + break; + case GLFW_KEY_G: + eKey = Key::G; + break; + case GLFW_KEY_H: + eKey = Key::H; + break; + case GLFW_KEY_I: + eKey = Key::I; + break; + case GLFW_KEY_J: + eKey = Key::J; + break; + case GLFW_KEY_K: + eKey = Key::K; + break; + case GLFW_KEY_L: + eKey = Key::L; + break; + case GLFW_KEY_M: + eKey = Key::M; + break; + case GLFW_KEY_N: + eKey = Key::N; + break; + case GLFW_KEY_O: + eKey = Key::O; + break; + case GLFW_KEY_P: + eKey = Key::P; + break; + case GLFW_KEY_Q: + eKey = Key::Q; + break; + case GLFW_KEY_R: + eKey = Key::R; + break; + case GLFW_KEY_S: + eKey = Key::S; + break; + case GLFW_KEY_T: + eKey = Key::T; + break; + case GLFW_KEY_U: + eKey = Key::U; + break; + case GLFW_KEY_V: + eKey = Key::V; + break; + case GLFW_KEY_W: + eKey = Key::W; + break; + case GLFW_KEY_X: + eKey = Key::X; + break; + case GLFW_KEY_Y: + eKey = Key::Y; + break; + case GLFW_KEY_Z: + eKey = Key::Z; + break; + case GLFW_KEY_0: + eKey = Key::Num0; + break; + case GLFW_KEY_1: + eKey = Key::Num1; + break; + case GLFW_KEY_2: + eKey = Key::Num2; + break; + case GLFW_KEY_3: + eKey = Key::Num3; + break; + case GLFW_KEY_4: + eKey = Key::Num4; + break; + case GLFW_KEY_5: + eKey = Key::Num5; + break; + case GLFW_KEY_6: + eKey = Key::Num6; + break; + case GLFW_KEY_7: + eKey = Key::Num7; + break; + case GLFW_KEY_8: + eKey = Key::Num8; + break; + case GLFW_KEY_9: + eKey = Key::Num9; + break; + case GLFW_KEY_F1: + eKey = Key::F1; + break; + case GLFW_KEY_F2: + eKey = Key::F2; + break; + case GLFW_KEY_F3: + eKey = Key::F3; + break; + case GLFW_KEY_F4: + eKey = Key::F4; + break; + case GLFW_KEY_F5: + eKey = Key::F5; + break; + case GLFW_KEY_F6: + eKey = Key::F6; + break; + case GLFW_KEY_F7: + eKey = Key::F7; + break; + case GLFW_KEY_F8: + eKey = Key::F8; + break; + case GLFW_KEY_F9: + eKey = Key::F9; + break; + case GLFW_KEY_F10: + eKey = Key::F10; + break; + case GLFW_KEY_F11: + eKey = Key::F11; + break; + case GLFW_KEY_F12: + eKey = Key::F12; + break; + case GLFW_KEY_SPACE: + eKey = Key::Space; + break; + case GLFW_KEY_ENTER: + eKey = Key::Enter; + break; + case GLFW_KEY_ESCAPE: + eKey = Key::Escape; + break; + case GLFW_KEY_TAB: + eKey = Key::Tab; + break; + case GLFW_KEY_BACKSPACE: + eKey = Key::Backspace; + break; + case GLFW_KEY_INSERT: + eKey = Key::Insert; + break; + case GLFW_KEY_DELETE: + eKey = Key::Delete; + break; + case GLFW_KEY_HOME: + eKey = Key::Home; + break; + case GLFW_KEY_END: + eKey = Key::End; + break; + case GLFW_KEY_PAGE_UP: + eKey = Key::PageUp; + break; + case GLFW_KEY_PAGE_DOWN: + eKey = Key::PageDown; + break; + case GLFW_KEY_UP: + eKey = Key::Up; + break; + case GLFW_KEY_DOWN: + eKey = Key::Down; + break; + case GLFW_KEY_LEFT: + eKey = Key::Left; + break; + case GLFW_KEY_RIGHT: + eKey = Key::Right; + break; + case GLFW_KEY_LEFT_SHIFT: + eKey = Key::LShift; + break; + case GLFW_KEY_RIGHT_SHIFT: + eKey = Key::RShift; + break; + case GLFW_KEY_LEFT_CONTROL: + eKey = Key::LCtrl; + break; + case GLFW_KEY_RIGHT_CONTROL: + eKey = Key::RCtrl; + break; + case GLFW_KEY_LEFT_ALT: + eKey = Key::LAlt; + break; + case GLFW_KEY_RIGHT_ALT: + eKey = Key::RAlt; + break; + case GLFW_KEY_CAPS_LOCK: + eKey = Key::CapsLock; + break; + case GLFW_KEY_NUM_LOCK: + eKey = Key::NumLock; + break; + case GLFW_KEY_SCROLL_LOCK: + eKey = Key::ScrollLock; + break; + default: + eKey = Key::None; + break; } if (eKey != Key::None) { diff --git a/Extra2D/src/platform/input_module.cpp b/Extra2D/src/platform/input_module.cpp index 4a3bceb..67e8914 100644 --- a/Extra2D/src/platform/input_module.cpp +++ b/Extra2D/src/platform/input_module.cpp @@ -1,51 +1,53 @@ -#include -#include #include -#include +#include #include +#include + namespace extra2d { -InputModule::InputModule(std::function configFn) { - configFn(cfg_); +InputModule::InputModule(std::function configFn) { + configFn(cfg_); } InputModule::~InputModule() { - if (initialized_) { - shutdown(); - } + if (initialized_) { + shutdown(); + } } bool InputModule::init() { - if (initialized_) return true; - - // 获取WindowModule依赖 - auto* winMod = Registry::instance().get(); - if (!winMod || !winMod->win()) { - return false; - } - - // 获取输入接口 - input_ = winMod->win()->input(); - if (!input_) { - return false; - } - - initialized_ = true; + if (initialized_) return true; + + // 获取WindowModule依赖 + auto *winMod = Registry::instance().get(); + if (!winMod || !winMod->win()) { + return false; + } + + // 获取输入接口 + input_ = winMod->win()->input(); + if (!input_) { + return false; + } + + initialized_ = true; + return true; } void InputModule::shutdown() { - if (!initialized_) return; - - input_ = nullptr; - initialized_ = false; + if (!initialized_) + return; + + input_ = nullptr; + initialized_ = false; } void InputModule::update() { - if (initialized_ && input_) { - input_->update(); - } + if (initialized_ && input_) { + input_->update(); + } } } // namespace extra2d diff --git a/Extra2D/src/platform/window_module.cpp b/Extra2D/src/platform/window_module.cpp index 196b762..d362cf8 100644 --- a/Extra2D/src/platform/window_module.cpp +++ b/Extra2D/src/platform/window_module.cpp @@ -2,7 +2,7 @@ #include #include -#include "backends/glfw/glfw_window.h" +#include #ifdef __SWITCH__ #include diff --git a/Extra2D/src/scene/node.cpp b/Extra2D/src/scene/node.cpp index c216a15..5af1ca2 100644 --- a/Extra2D/src/scene/node.cpp +++ b/Extra2D/src/scene/node.cpp @@ -1,12 +1,10 @@ #include #include -#include #include #include #include #include - namespace extra2d { /** diff --git a/Extra2D/src/scene/scene.cpp b/Extra2D/src/scene/scene.cpp index db7572a..151acf1 100644 --- a/Extra2D/src/scene/scene.cpp +++ b/Extra2D/src/scene/scene.cpp @@ -1,10 +1,8 @@ -#include #include #include #include #include - namespace extra2d { /** diff --git a/Extra2D/src/scene/scene_manager.cpp b/Extra2D/src/scene/scene_manager.cpp index 497d11c..a91274d 100644 --- a/Extra2D/src/scene/scene_manager.cpp +++ b/Extra2D/src/scene/scene_manager.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -13,7 +12,6 @@ #include #include - namespace extra2d { namespace { diff --git a/xmake/engine.lua b/xmake/engine.lua index 1c59e3d..319d2c5 100644 --- a/xmake/engine.lua +++ b/xmake/engine.lua @@ -17,14 +17,14 @@ function define_extra2d_engine() set_kind("static") -- 引擎核心源文件(后端无关) - add_files("Extra2D/src/**.cpp|platform/backends/**.cpp|graphics/backends/**.cpp") + add_files("Extra2D/src/**.cpp|platform/backends/**.cpp|graphics/opengl/**.cpp") -- OpenGL 渲染后端源文件 - add_files("Extra2D/src/graphics/backends/opengl/*.cpp") + add_files("Extra2D/src/graphics/opengl/*.cpp") add_files("Extra2D/src/glad/glad.c") -- GLFW 窗口后端源文件 - add_files("Extra2D/src/platform/backends/glfw/*.cpp") + add_files("Extra2D/src/platform/glfw/*.cpp") -- 头文件路径 add_includedirs("Extra2D/include", {public = true})