refactor: 重构文件结构,将backend相关文件移动到opengl目录

- 将graphics/backends/opengl下的文件移动到graphics/opengl目录
- 更新相关头文件引用路径
- 调整xmake构建文件以匹配新的文件结构
- 清理不再使用的service_locator.h包含
- 格式化代码并统一代码风格
This commit is contained in:
ChestnutYueyue 2026-02-20 13:07:43 +08:00
parent b892736fb2
commit a490d9e296
33 changed files with 477 additions and 347 deletions

View File

@ -1,84 +0,0 @@
#pragma once
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/texture/font.h>
#include <stb/stb_truetype.h>
#include <stb/stb_rect_pack.h>
#include <unordered_map>
#include <vector>
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<GLTexture> texture_;
std::unordered_map<char32_t, GlyphData> glyphs_;
float lineHeight_;
float ascent_;
float descent_;
float lineGap_;
// 字体数据
std::vector<unsigned char> fontData_;
stbtt_fontinfo fontInfo_;
float scale_;
// stb_rect_pack 上下文 - 持久化以支持增量打包
mutable stbrp_context packContext_;
mutable std::vector<stbrp_node> packNodes_;
// 预分配缓冲区,避免每次动态分配
mutable std::vector<uint8_t> glyphBitmapCache_;
mutable std::vector<uint8_t> 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<uint8_t>& data);
};
} // namespace extra2d

View File

@ -2,7 +2,7 @@
#include <extra2d/core/color.h>
#include <extra2d/core/types.h>
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/graphics/texture/texture.h>
#include <mutex>
@ -257,7 +257,7 @@ public:
/**
* @brief
*/
static RenderTargetMgr& get();
static RenderTargetMgr &get();
/**
* @brief

View File

@ -0,0 +1,85 @@
#pragma once
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/graphics/texture/font.h>
#include <stb/stb_rect_pack.h>
#include <stb/stb_truetype.h>
#include <unordered_map>
#include <vector>
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<GLTexture> texture_;
std::unordered_map<char32_t, GlyphData> glyphs_;
float lineHeight_;
float ascent_;
float descent_;
float lineGap_;
// 字体数据
std::vector<unsigned char> fontData_;
stbtt_fontinfo fontInfo_;
float scale_;
// stb_rect_pack 上下文 - 持久化以支持增量打包
mutable stbrp_context packContext_;
mutable std::vector<stbrp_node> packNodes_;
// 预分配缓冲区,避免每次动态分配
mutable std::vector<uint8_t> glyphBitmapCache_;
mutable std::vector<uint8_t> 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<uint8_t> &data);
};
} // namespace extra2d

View File

@ -1,12 +1,13 @@
#pragma once
#include <extra2d/graphics/backends/opengl/gl_buffer.h>
#include <extra2d/graphics/backends/opengl/gl_framebuffer.h>
#include <extra2d/graphics/backends/opengl/gl_pipeline.h>
#include <extra2d/graphics/backends/opengl/gl_sprite_batch.h>
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/graphics/opengl/gl_buffer.h>
#include <extra2d/graphics/opengl/gl_framebuffer.h>
#include <extra2d/graphics/opengl/gl_pipeline.h>
#include <extra2d/graphics/opengl/gl_sprite_batch.h>
#include <extra2d/graphics/shader/shader_interface.h>
#include <array>
#include <glad/glad.h>
#include <vector>
@ -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<GLFramebuffer> createFramebuffer(const FramebufferDesc& desc);
Ptr<GLFramebuffer> 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<IShader> shapeShader_;
Ptr<IShader> sdfFontShader_; // SDF字体专用着色器
Ptr<IShader> 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<glm::mat4> transformStack_;
@ -160,19 +161,19 @@ private:
GLPipeline pipeline_;
// 自动批处理状态
bool batchActive_ = false; // 批处理是否激活
bool autoBatchEnabled_ = true; // 是否启用自动批处理
const Texture* currentBatchTexture_ = nullptr; // 当前批处理的纹理
std::vector<SpriteData> pendingSprites_; // 待提交的精灵
bool batchActive_ = false; // 批处理是否激活
bool autoBatchEnabled_ = true; // 是否启用自动批处理
const Texture *currentBatchTexture_ = nullptr; // 当前批处理的纹理
std::vector<SpriteData> pendingSprites_; // 待提交的精灵
static constexpr size_t MAX_BATCH_SPRITES = 1000; // 最大批处理精灵数
// 帧缓冲管理
mutable Ptr<GLFramebuffer> defaultFramebuffer_; // 默认帧缓冲(延迟创建)
GLFramebuffer* currentFramebuffer_ = nullptr; // 当前绑定的帧缓冲
mutable Ptr<GLFramebuffer> 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);

View File

@ -1,11 +1,12 @@
#pragma once
#include <extra2d/graphics/backends/opengl/gl_buffer.h>
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/batch/sprite_batch.h>
#include <extra2d/graphics/opengl/gl_buffer.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/graphics/shader/shader_interface.h>
#include <extra2d/graphics/shader/shader_manager.h>
#include <glad/glad.h>
#include <vector>
@ -48,7 +49,9 @@ public:
Ptr<IShader> 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:

View File

@ -3,12 +3,13 @@
#include <extra2d/core/color.h>
#include <extra2d/core/math_types.h>
#include <extra2d/core/types.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/graphics/texture/texture.h>
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
namespace extra2d {
@ -20,10 +21,10 @@ 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,22 +37,22 @@ 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<Texture> getTexture() const { return texture_; }
// 获取条目
const AtlasEntry* getEntry(const std::string& name) const;
const AtlasEntry *getEntry(const std::string &name) const;
// 获取使用率
float getUsageRatio() const;
@ -76,7 +77,7 @@ private:
std::unique_ptr<PackNode> right;
PackNode(int x_, int y_, int w, int h)
: x(x_), y(y_), width(w), height(h), used(false) {}
: x(x_), y(y_), width(w), height(h), used(false) {}
};
std::unique_ptr<PackNode> root_;
@ -84,8 +85,8 @@ private:
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);
};
/**
@ -102,22 +103,24 @@ public:
// 添加纹理到图集
// 如果纹理太大,返回 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<std::unique_ptr<TextureAtlasPage>>& getPages() const { return pages_; }
const std::vector<std::unique_ptr<TextureAtlasPage>> &getPages() const {
return pages_;
}
// 获取总使用率
float getTotalUsageRatio() const;
@ -135,7 +138,7 @@ public:
private:
std::vector<std::unique_ptr<TextureAtlasPage>> pages_;
std::unordered_map<std::string, TextureAtlasPage*> entryToPage_;
std::unordered_map<std::string, TextureAtlasPage *> entryToPage_;
int pageSize_;
int sizeThreshold_;
@ -148,26 +151,24 @@ 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);
}
bool contains(const std::string &name) const { return atlas_.contains(name); }
const Texture* getAtlasTexture(const std::string& name) const {
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);
}
@ -175,8 +176,8 @@ 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_;
};

View File

@ -1,9 +1,9 @@
#include <extra2d/app/application.h>
#include <extra2d/core/registry.h>
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_renderer.h>
#include <extra2d/graphics/backends/opengl/gl_shader.h>
#include <extra2d/graphics/core/render_module.h>
#include <extra2d/graphics/opengl/gl_renderer.h>
#include <extra2d/graphics/opengl/gl_shader.h>
#include <extra2d/graphics/shader/shader_manager.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/services/logger_service.h>

View File

@ -1,10 +1,9 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/core/render_target.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/services/logger_service.h>
#include <glad/glad.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>

View File

@ -1,10 +1,8 @@
#include <cstring>
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_buffer.h>
#include <extra2d/graphics/memory/vram_manager.h>
#include <extra2d/graphics/opengl/gl_buffer.h>
#include <extra2d/services/logger_service.h>
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<int>(type_),
static_cast<int>(usage_));
E2D_LOG_DEBUG("GLBuffer 已创建: ID={}, 大小={}, 类型={}, 用途={}", bufferID_,
size_, static_cast<int>(type_), static_cast<int>(usage_));
return true;
}

View File

@ -1,6 +1,5 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_context.h>
#include <extra2d/graphics/memory/gpu_context.h>
#include <extra2d/graphics/opengl/gl_context.h>
#include <extra2d/services/logger_service.h>
namespace extra2d {

View File

@ -1,5 +1,4 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_font_atlas.h>
#include <extra2d/graphics/opengl/gl_font_atlas.h>
#include <extra2d/services/logger_service.h>
#include <algorithm>

View File

@ -1,9 +1,7 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_framebuffer.h>
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/opengl/gl_framebuffer.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/services/logger_service.h>
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:

View File

@ -1,8 +1,6 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_pipeline.h>
#include <extra2d/graphics/opengl/gl_pipeline.h>
#include <extra2d/services/logger_service.h>
namespace extra2d {
// ============================================================================

View File

@ -1,19 +1,19 @@
#include <cmath>
#include <cstring>
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_context.h>
#include <extra2d/graphics/backends/opengl/gl_font_atlas.h>
#include <extra2d/graphics/backends/opengl/gl_framebuffer.h>
#include <extra2d/graphics/backends/opengl/gl_renderer.h>
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/batch/sprite_batch.h>
#include <extra2d/graphics/memory/gpu_context.h>
#include <extra2d/graphics/memory/vram_manager.h>
#include <extra2d/graphics/opengl/gl_context.h>
#include <extra2d/graphics/opengl/gl_font_atlas.h>
#include <extra2d/graphics/opengl/gl_framebuffer.h>
#include <extra2d/graphics/opengl/gl_renderer.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/graphics/shader/shader_manager.h>
#include <extra2d/platform/iwindow.h>
#include <extra2d/services/logger_service.h>
#include <vector>
namespace extra2d {
// VBO 初始大小(用于 VRAM 跟踪)

View File

@ -1,8 +1,6 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_shader.h>
#include <extra2d/graphics/opengl/gl_shader.h>
#include <extra2d/services/logger_service.h>
namespace extra2d {
/**

View File

@ -1,6 +1,5 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/backends/opengl/gl_sprite_batch.h>
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/opengl/gl_sprite_batch.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#include <extra2d/graphics/shader/shader_manager.h>
#include <extra2d/services/logger_service.h>
@ -93,7 +92,8 @@ void GLSpriteBatch::begin(const glm::mat4 &viewProjection) {
ebo_.bind();
}
void GLSpriteBatch::begin(const glm::mat4 &viewProjection, Ptr<IShader> shader) {
void GLSpriteBatch::begin(const glm::mat4 &viewProjection,
Ptr<IShader> 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;
}

View File

@ -1,6 +1,7 @@
#include <extra2d/graphics/backends/opengl/gl_texture.h>
#include <extra2d/graphics/memory/gpu_context.h>
#include <extra2d/graphics/memory/vram_manager.h>
#include <extra2d/graphics/opengl/gl_texture.h>
#define STB_IMAGE_IMPLEMENTATION
#include <cstring>
#include <extra2d/core/service_locator.h>
@ -8,7 +9,6 @@
#include <fstream>
#include <stb/stb_image.h>
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;
}

View File

@ -1,7 +1,6 @@
#include "glfw_input.h"
#include <cmath>
#include <extra2d/core/service_locator.h>
#include <extra2d/services/logger_service.h>
#include <glfw/glfw_input.h>
namespace extra2d {

View File

@ -1,6 +1,6 @@
#include "glfw_window.h"
#include "glfw_input.h"
#include <extra2d/core/service_locator.h>
#include <glfw/glfw_input.h>
#include <glfw/glfw_window.h>
#include <extra2d/event/event.h>
#include <extra2d/platform/keys.h>
#include <extra2d/services/event_service.h>
@ -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) {

View File

@ -1,51 +1,53 @@
#include <extra2d/platform/input_module.h>
#include <extra2d/core/service_locator.h>
#include <extra2d/core/registry.h>
#include <extra2d/services/event_service.h>
#include <extra2d/platform/input_module.h>
#include <extra2d/platform/window_module.h>
#include <extra2d/services/event_service.h>
namespace extra2d {
InputModule::InputModule(std::function<void(InputCfg&)> configFn) {
configFn(cfg_);
InputModule::InputModule(std::function<void(InputCfg &)> configFn) {
configFn(cfg_);
}
InputModule::~InputModule() {
if (initialized_) {
shutdown();
}
if (initialized_) {
shutdown();
}
}
bool InputModule::init() {
if (initialized_) return true;
// 获取WindowModule依赖
auto* winMod = Registry::instance().get<WindowModule>();
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<WindowModule>();
if (!winMod || !winMod->win()) {
return false;
}
// 获取输入接口
input_ = winMod->win()->input();
if (!input_) {
return false;
}
initialized_ = true;
return true;
}
void InputModule::shutdown() {
if (!initialized_) return;
if (!initialized_)
return;
input_ = nullptr;
initialized_ = false;
input_ = nullptr;
initialized_ = false;
}
void InputModule::update() {
if (initialized_ && input_) {
input_->update();
}
if (initialized_ && input_) {
input_->update();
}
}
} // namespace extra2d

View File

@ -2,7 +2,7 @@
#include <extra2d/platform/window_module.h>
#include <extra2d/services/logger_service.h>
#include "backends/glfw/glfw_window.h"
#include <glfw/glfw_window.h>
#ifdef __SWITCH__
#include <switch.h>

View File

@ -1,12 +1,10 @@
#include <algorithm>
#include <cmath>
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/core/render_command.h>
#include <extra2d/scene/node.h>
#include <extra2d/scene/scene.h>
#include <extra2d/services/logger_service.h>
namespace extra2d {
/**

View File

@ -1,10 +1,8 @@
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/graphics/core/render_command.h>
#include <extra2d/scene/scene.h>
#include <extra2d/services/logger_service.h>
namespace extra2d {
/**

View File

@ -1,6 +1,5 @@
#include <algorithm>
#include <extra2d/app/application.h>
#include <extra2d/core/service_locator.h>
#include <extra2d/graphics/core/render_backend.h>
#include <extra2d/graphics/core/render_command.h>
#include <extra2d/platform/iinput.h>
@ -13,7 +12,6 @@
#include <extra2d/scene/transition_slide_scene.h>
#include <extra2d/services/logger_service.h>
namespace extra2d {
namespace {

View File

@ -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})