refactor: 重构文件结构,将backend相关文件移动到opengl目录
- 将graphics/backends/opengl下的文件移动到graphics/opengl目录 - 更新相关头文件引用路径 - 调整xmake构建文件以匹配新的文件结构 - 清理不再使用的service_locator.h包含 - 格式化代码并统一代码风格
This commit is contained in:
parent
b892736fb2
commit
a490d9e296
|
|
@ -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
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include <extra2d/core/color.h>
|
#include <extra2d/core/color.h>
|
||||||
#include <extra2d/core/types.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 <extra2d/graphics/texture/texture.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
|
@ -257,7 +257,7 @@ public:
|
||||||
/**
|
/**
|
||||||
* @brief 获取单例实例
|
* @brief 获取单例实例
|
||||||
*/
|
*/
|
||||||
static RenderTargetMgr& get();
|
static RenderTargetMgr &get();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 初始化渲染目标管理器
|
* @brief 初始化渲染目标管理器
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
#pragma once
|
#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/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 <extra2d/graphics/shader/shader_interface.h>
|
||||||
|
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -27,7 +28,7 @@ public:
|
||||||
~GLRenderer() override;
|
~GLRenderer() override;
|
||||||
|
|
||||||
// RenderBackend 接口实现
|
// RenderBackend 接口实现
|
||||||
bool init(IWindow* window) override;
|
bool init(IWindow *window) override;
|
||||||
void shutdown() override;
|
void shutdown() override;
|
||||||
|
|
||||||
void beginFrame(const Color &clearColor) override;
|
void beginFrame(const Color &clearColor) override;
|
||||||
|
|
@ -90,13 +91,13 @@ public:
|
||||||
* @param desc 帧缓冲描述
|
* @param desc 帧缓冲描述
|
||||||
* @return 创建的帧缓冲智能指针
|
* @return 创建的帧缓冲智能指针
|
||||||
*/
|
*/
|
||||||
Ptr<GLFramebuffer> createFramebuffer(const FramebufferDesc& desc);
|
Ptr<GLFramebuffer> createFramebuffer(const FramebufferDesc &desc);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 绑定帧缓冲(作为渲染目标)
|
* @brief 绑定帧缓冲(作为渲染目标)
|
||||||
* @param framebuffer 帧缓冲对象指针,传入 nullptr 则绑定默认帧缓冲
|
* @param framebuffer 帧缓冲对象指针,传入 nullptr 则绑定默认帧缓冲
|
||||||
*/
|
*/
|
||||||
void bindFramebuffer(GLFramebuffer* framebuffer);
|
void bindFramebuffer(GLFramebuffer *framebuffer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 解绑帧缓冲(恢复到默认帧缓冲)
|
* @brief 解绑帧缓冲(恢复到默认帧缓冲)
|
||||||
|
|
@ -116,7 +117,7 @@ public:
|
||||||
* @param clearDepth 是否清除深度缓冲
|
* @param clearDepth 是否清除深度缓冲
|
||||||
* @param clearStencil 是否清除模板缓冲
|
* @param clearStencil 是否清除模板缓冲
|
||||||
*/
|
*/
|
||||||
void clearFramebuffer(const Color& color, bool clearColor = true,
|
void clearFramebuffer(const Color &color, bool clearColor = true,
|
||||||
bool clearDepth = true, bool clearStencil = false);
|
bool clearDepth = true, bool clearStencil = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -131,15 +132,15 @@ private:
|
||||||
float r, g, b, a;
|
float r, g, b, a;
|
||||||
};
|
};
|
||||||
|
|
||||||
IWindow* window_;
|
IWindow *window_;
|
||||||
GLSpriteBatch spriteBatch_;
|
GLSpriteBatch spriteBatch_;
|
||||||
Ptr<IShader> shapeShader_;
|
Ptr<IShader> shapeShader_;
|
||||||
Ptr<IShader> sdfFontShader_; // SDF字体专用着色器
|
Ptr<IShader> sdfFontShader_; // SDF字体专用着色器
|
||||||
|
|
||||||
GLuint shapeVao_; // 形状 VAO(手动管理,用于顶点属性配置)
|
GLuint shapeVao_; // 形状 VAO(手动管理,用于顶点属性配置)
|
||||||
GLBuffer shapeBuffer_; // 形状 VBO(使用 GLBuffer 管理)
|
GLBuffer shapeBuffer_; // 形状 VBO(使用 GLBuffer 管理)
|
||||||
GLuint lineVao_; // 线条 VAO(手动管理,用于顶点属性配置)
|
GLuint lineVao_; // 线条 VAO(手动管理,用于顶点属性配置)
|
||||||
GLBuffer lineBuffer_; // 线条 VBO(使用 GLBuffer 管理)
|
GLBuffer lineBuffer_; // 线条 VBO(使用 GLBuffer 管理)
|
||||||
|
|
||||||
glm::mat4 viewProjection_;
|
glm::mat4 viewProjection_;
|
||||||
std::vector<glm::mat4> transformStack_;
|
std::vector<glm::mat4> transformStack_;
|
||||||
|
|
@ -160,19 +161,19 @@ private:
|
||||||
GLPipeline pipeline_;
|
GLPipeline pipeline_;
|
||||||
|
|
||||||
// 自动批处理状态
|
// 自动批处理状态
|
||||||
bool batchActive_ = false; // 批处理是否激活
|
bool batchActive_ = false; // 批处理是否激活
|
||||||
bool autoBatchEnabled_ = true; // 是否启用自动批处理
|
bool autoBatchEnabled_ = true; // 是否启用自动批处理
|
||||||
const Texture* currentBatchTexture_ = nullptr; // 当前批处理的纹理
|
const Texture *currentBatchTexture_ = nullptr; // 当前批处理的纹理
|
||||||
std::vector<SpriteData> pendingSprites_; // 待提交的精灵
|
std::vector<SpriteData> pendingSprites_; // 待提交的精灵
|
||||||
static constexpr size_t MAX_BATCH_SPRITES = 1000; // 最大批处理精灵数
|
static constexpr size_t MAX_BATCH_SPRITES = 1000; // 最大批处理精灵数
|
||||||
|
|
||||||
// 帧缓冲管理
|
// 帧缓冲管理
|
||||||
mutable Ptr<GLFramebuffer> defaultFramebuffer_; // 默认帧缓冲(延迟创建)
|
mutable Ptr<GLFramebuffer> defaultFramebuffer_; // 默认帧缓冲(延迟创建)
|
||||||
GLFramebuffer* currentFramebuffer_ = nullptr; // 当前绑定的帧缓冲
|
GLFramebuffer *currentFramebuffer_ = nullptr; // 当前绑定的帧缓冲
|
||||||
|
|
||||||
void initShapeRendering();
|
void initShapeRendering();
|
||||||
void ensureBatchActive(); // 确保批处理已激活
|
void ensureBatchActive(); // 确保批处理已激活
|
||||||
void submitPendingSprites(); // 提交待处理的精灵
|
void submitPendingSprites(); // 提交待处理的精灵
|
||||||
void flushShapeBatch();
|
void flushShapeBatch();
|
||||||
void flushLineBatch();
|
void flushLineBatch();
|
||||||
void addShapeVertex(float x, float y, const Color &color);
|
void addShapeVertex(float x, float y, const Color &color);
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
#pragma once
|
#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/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_interface.h>
|
||||||
#include <extra2d/graphics/shader/shader_manager.h>
|
#include <extra2d/graphics/shader/shader_manager.h>
|
||||||
|
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
@ -48,7 +49,9 @@ public:
|
||||||
Ptr<IShader> getShader() const { return shader_; }
|
Ptr<IShader> getShader() const { return shader_; }
|
||||||
|
|
||||||
// 设置额外的uniform值(用于SDF字体等特殊渲染)
|
// 设置额外的uniform值(用于SDF字体等特殊渲染)
|
||||||
void setExtraUniforms(const UniformValueMap &uniforms) { extraUniforms_ = uniforms; }
|
void setExtraUniforms(const UniformValueMap &uniforms) {
|
||||||
|
extraUniforms_ = uniforms;
|
||||||
|
}
|
||||||
void clearExtraUniforms() { extraUniforms_.clear(); }
|
void clearExtraUniforms() { extraUniforms_.clear(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -3,12 +3,13 @@
|
||||||
#include <extra2d/core/color.h>
|
#include <extra2d/core/color.h>
|
||||||
#include <extra2d/core/math_types.h>
|
#include <extra2d/core/math_types.h>
|
||||||
#include <extra2d/core/types.h>
|
#include <extra2d/core/types.h>
|
||||||
|
#include <extra2d/graphics/opengl/gl_texture.h>
|
||||||
#include <extra2d/graphics/texture/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 <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
|
|
@ -20,11 +21,11 @@ namespace extra2d {
|
||||||
* @brief 图集中的单个纹理条目
|
* @brief 图集中的单个纹理条目
|
||||||
*/
|
*/
|
||||||
struct AtlasEntry {
|
struct AtlasEntry {
|
||||||
std::string name; // 原始纹理名称/路径
|
std::string name; // 原始纹理名称/路径
|
||||||
Rect uvRect; // 在图集中的 UV 坐标范围
|
Rect uvRect; // 在图集中的 UV 坐标范围
|
||||||
Vec2 originalSize; // 原始纹理尺寸
|
Vec2 originalSize; // 原始纹理尺寸
|
||||||
uint32_t padding; // 边距(用于避免纹理 bleeding)
|
uint32_t padding; // 边距(用于避免纹理 bleeding)
|
||||||
|
|
||||||
AtlasEntry() : uvRect(), originalSize(), padding(2) {}
|
AtlasEntry() : uvRect(), originalSize(), padding(2) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -36,56 +37,56 @@ class TextureAtlasPage {
|
||||||
public:
|
public:
|
||||||
static constexpr int DEFAULT_SIZE = 2048;
|
static constexpr int DEFAULT_SIZE = 2048;
|
||||||
static constexpr int MAX_SIZE = 4096;
|
static constexpr int MAX_SIZE = 4096;
|
||||||
static constexpr int MIN_TEXTURE_SIZE = 32; // 小于此大小的纹理才考虑合并
|
static constexpr int MIN_TEXTURE_SIZE = 32; // 小于此大小的纹理才考虑合并
|
||||||
static constexpr int PADDING = 2; // 纹理间边距
|
static constexpr int PADDING = 2; // 纹理间边距
|
||||||
|
|
||||||
TextureAtlasPage(int width = DEFAULT_SIZE, int height = DEFAULT_SIZE);
|
TextureAtlasPage(int width = DEFAULT_SIZE, int height = DEFAULT_SIZE);
|
||||||
~TextureAtlasPage();
|
~TextureAtlasPage();
|
||||||
|
|
||||||
// 尝试添加纹理到图集
|
// 尝试添加纹理到图集
|
||||||
// 返回是否成功,如果成功则输出 uvRect
|
// 返回是否成功,如果成功则输出 uvRect
|
||||||
bool tryAddTexture(const std::string& name, int texWidth, int texHeight,
|
bool tryAddTexture(const std::string &name, int texWidth, int texHeight,
|
||||||
const uint8_t* pixels, Rect& outUvRect);
|
const uint8_t *pixels, Rect &outUvRect);
|
||||||
|
|
||||||
// 获取图集纹理
|
// 获取图集纹理
|
||||||
Ptr<Texture> getTexture() const { return texture_; }
|
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;
|
float getUsageRatio() const;
|
||||||
|
|
||||||
// 获取尺寸
|
// 获取尺寸
|
||||||
int getWidth() const { return width_; }
|
int getWidth() const { return width_; }
|
||||||
int getHeight() const { return height_; }
|
int getHeight() const { return height_; }
|
||||||
|
|
||||||
// 是否已满
|
// 是否已满
|
||||||
bool isFull() const { return isFull_; }
|
bool isFull() const { return isFull_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int width_, height_;
|
int width_, height_;
|
||||||
Ptr<Texture> texture_;
|
Ptr<Texture> texture_;
|
||||||
std::unordered_map<std::string, AtlasEntry> entries_;
|
std::unordered_map<std::string, AtlasEntry> entries_;
|
||||||
|
|
||||||
// 矩形打包数据
|
// 矩形打包数据
|
||||||
struct PackNode {
|
struct PackNode {
|
||||||
int x, y, width, height;
|
int x, y, width, height;
|
||||||
bool used;
|
bool used;
|
||||||
std::unique_ptr<PackNode> left;
|
std::unique_ptr<PackNode> left;
|
||||||
std::unique_ptr<PackNode> right;
|
std::unique_ptr<PackNode> right;
|
||||||
|
|
||||||
PackNode(int x_, int y_, int w, int h)
|
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_;
|
std::unique_ptr<PackNode> root_;
|
||||||
bool isFull_;
|
bool isFull_;
|
||||||
int usedArea_;
|
int usedArea_;
|
||||||
|
|
||||||
// 递归插入
|
// 递归插入
|
||||||
PackNode* insert(PackNode* node, int width, int height);
|
PackNode *insert(PackNode *node, int width, int height);
|
||||||
void writePixels(int x, int y, int w, int h, const uint8_t* pixels);
|
void writePixels(int x, int y, int w, int h, const uint8_t *pixels);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -96,47 +97,49 @@ class TextureAtlas {
|
||||||
public:
|
public:
|
||||||
TextureAtlas();
|
TextureAtlas();
|
||||||
~TextureAtlas();
|
~TextureAtlas();
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
void init(int pageSize = TextureAtlasPage::DEFAULT_SIZE);
|
void init(int pageSize = TextureAtlasPage::DEFAULT_SIZE);
|
||||||
|
|
||||||
// 添加纹理到图集
|
// 添加纹理到图集
|
||||||
// 如果纹理太大,返回 false,应该作为独立纹理加载
|
// 如果纹理太大,返回 false,应该作为独立纹理加载
|
||||||
bool addTexture(const std::string& name, int width, int height,
|
bool addTexture(const std::string &name, int width, int height,
|
||||||
const uint8_t* pixels);
|
const uint8_t *pixels);
|
||||||
|
|
||||||
// 查询纹理是否在图集中
|
// 查询纹理是否在图集中
|
||||||
bool contains(const std::string& name) const;
|
bool contains(const std::string &name) const;
|
||||||
|
|
||||||
// 获取纹理在图集中的信息
|
// 获取纹理在图集中的信息
|
||||||
// 返回图集纹理和 UV 坐标
|
// 返回图集纹理和 UV 坐标
|
||||||
const Texture* getAtlasTexture(const std::string& name) const;
|
const Texture *getAtlasTexture(const std::string &name) const;
|
||||||
Rect getUVRect(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;
|
float getTotalUsageRatio() const;
|
||||||
|
|
||||||
// 清空所有图集
|
// 清空所有图集
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
// 设置是否启用自动图集
|
// 设置是否启用自动图集
|
||||||
void setEnabled(bool enabled) { enabled_ = enabled; }
|
void setEnabled(bool enabled) { enabled_ = enabled; }
|
||||||
bool isEnabled() const { return enabled_; }
|
bool isEnabled() const { return enabled_; }
|
||||||
|
|
||||||
// 设置纹理大小阈值(小于此大小的纹理才进入图集)
|
// 设置纹理大小阈值(小于此大小的纹理才进入图集)
|
||||||
void setSizeThreshold(int threshold) { sizeThreshold_ = threshold; }
|
void setSizeThreshold(int threshold) { sizeThreshold_ = threshold; }
|
||||||
int getSizeThreshold() const { return sizeThreshold_; }
|
int getSizeThreshold() const { return sizeThreshold_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<TextureAtlasPage>> pages_;
|
std::vector<std::unique_ptr<TextureAtlasPage>> pages_;
|
||||||
std::unordered_map<std::string, TextureAtlasPage*> entryToPage_;
|
std::unordered_map<std::string, TextureAtlasPage *> entryToPage_;
|
||||||
|
|
||||||
int pageSize_;
|
int pageSize_;
|
||||||
int sizeThreshold_;
|
int sizeThreshold_;
|
||||||
bool enabled_;
|
bool enabled_;
|
||||||
|
|
@ -148,36 +151,34 @@ private:
|
||||||
*/
|
*/
|
||||||
class TextureAtlasMgr {
|
class TextureAtlasMgr {
|
||||||
public:
|
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,
|
bool addTexture(const std::string &name, int width, int height,
|
||||||
const uint8_t* pixels) {
|
const uint8_t *pixels) {
|
||||||
return atlas_.addTexture(name, width, height, pixels);
|
return atlas_.addTexture(name, width, height, pixels);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool contains(const std::string& name) const {
|
bool contains(const std::string &name) const { return atlas_.contains(name); }
|
||||||
return atlas_.contains(name);
|
|
||||||
}
|
const Texture *getAtlasTexture(const std::string &name) const {
|
||||||
|
|
||||||
const Texture* getAtlasTexture(const std::string& name) const {
|
|
||||||
return atlas_.getAtlasTexture(name);
|
return atlas_.getAtlasTexture(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect getUVRect(const std::string& name) const {
|
Rect getUVRect(const std::string &name) const {
|
||||||
return atlas_.getUVRect(name);
|
return atlas_.getUVRect(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TextureAtlasMgr() = default;
|
TextureAtlasMgr() = default;
|
||||||
~TextureAtlasMgr() = default;
|
~TextureAtlasMgr() = default;
|
||||||
|
|
||||||
TextureAtlasMgr(const TextureAtlasMgr&) = delete;
|
TextureAtlasMgr(const TextureAtlasMgr &) = delete;
|
||||||
TextureAtlasMgr& operator=(const TextureAtlasMgr&) = delete;
|
TextureAtlasMgr &operator=(const TextureAtlasMgr &) = delete;
|
||||||
|
|
||||||
TextureAtlas atlas_;
|
TextureAtlas atlas_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
#include <extra2d/app/application.h>
|
#include <extra2d/app/application.h>
|
||||||
#include <extra2d/core/registry.h>
|
#include <extra2d/core/registry.h>
|
||||||
#include <extra2d/core/service_locator.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/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/graphics/shader/shader_manager.h>
|
||||||
#include <extra2d/platform/window_module.h>
|
#include <extra2d/platform/window_module.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
#include <extra2d/core/service_locator.h>
|
#include <extra2d/core/service_locator.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_texture.h>
|
|
||||||
#include <extra2d/graphics/core/render_target.h>
|
#include <extra2d/graphics/core/render_target.h>
|
||||||
|
#include <extra2d/graphics/opengl/gl_texture.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
|
||||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
#include <stb/stb_image_write.h>
|
#include <stb/stb_image_write.h>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
#include <cstring>
|
#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/memory/vram_manager.h>
|
||||||
|
#include <extra2d/graphics/opengl/gl_buffer.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
@ -42,9 +40,8 @@ bool GLBuffer::init(const BufferDesc &desc) {
|
||||||
// 追踪显存使用
|
// 追踪显存使用
|
||||||
VRAMMgr::get().allocBuffer(size_);
|
VRAMMgr::get().allocBuffer(size_);
|
||||||
|
|
||||||
E2D_LOG_DEBUG("GLBuffer 已创建: ID={}, 大小={}, 类型={}, 用途={}",
|
E2D_LOG_DEBUG("GLBuffer 已创建: ID={}, 大小={}, 类型={}, 用途={}", bufferID_,
|
||||||
bufferID_, size_, static_cast<int>(type_),
|
size_, static_cast<int>(type_), static_cast<int>(usage_));
|
||||||
static_cast<int>(usage_));
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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/memory/gpu_context.h>
|
||||||
|
#include <extra2d/graphics/opengl/gl_context.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
#include <extra2d/core/service_locator.h>
|
#include <extra2d/graphics/opengl/gl_font_atlas.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_font_atlas.h>
|
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
#include <extra2d/core/service_locator.h>
|
#include <extra2d/graphics/opengl/gl_framebuffer.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_framebuffer.h>
|
#include <extra2d/graphics/opengl/gl_texture.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_texture.h>
|
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
@ -37,8 +35,8 @@ bool GLFramebuffer::init(const FramebufferDesc &desc) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
E2D_LOG_DEBUG("GLFramebuffer 已创建: ID={}, 大小={}x{}, 颜色附件={}",
|
E2D_LOG_DEBUG("GLFramebuffer 已创建: ID={}, 大小={}x{}, 颜色附件={}", fboID_,
|
||||||
fboID_, width_, height_, numColorAttachments_);
|
width_, height_, numColorAttachments_);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -237,8 +235,7 @@ bool GLFramebuffer::checkStatus() {
|
||||||
E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_UNDEFINED");
|
E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_UNDEFINED");
|
||||||
break;
|
break;
|
||||||
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
|
||||||
E2D_LOG_ERROR(
|
E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
|
||||||
"帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
|
|
||||||
break;
|
break;
|
||||||
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
|
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
|
||||||
E2D_LOG_ERROR(
|
E2D_LOG_ERROR(
|
||||||
|
|
@ -246,12 +243,10 @@ bool GLFramebuffer::checkStatus() {
|
||||||
break;
|
break;
|
||||||
#ifndef GL_ES_VERSION_2_0
|
#ifndef GL_ES_VERSION_2_0
|
||||||
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
|
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
|
||||||
E2D_LOG_ERROR(
|
E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER");
|
||||||
"帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER");
|
|
||||||
break;
|
break;
|
||||||
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
|
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
|
||||||
E2D_LOG_ERROR(
|
E2D_LOG_ERROR("帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER");
|
||||||
"帧缓冲区不完整: GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER");
|
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
case GL_FRAMEBUFFER_UNSUPPORTED:
|
case GL_FRAMEBUFFER_UNSUPPORTED:
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
#include <extra2d/core/service_locator.h>
|
#include <extra2d/graphics/opengl/gl_pipeline.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_pipeline.h>
|
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,19 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#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/batch/sprite_batch.h>
|
||||||
#include <extra2d/graphics/memory/gpu_context.h>
|
#include <extra2d/graphics/memory/gpu_context.h>
|
||||||
#include <extra2d/graphics/memory/vram_manager.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/graphics/shader/shader_manager.h>
|
||||||
#include <extra2d/platform/iwindow.h>
|
#include <extra2d/platform/iwindow.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
// VBO 初始大小(用于 VRAM 跟踪)
|
// VBO 初始大小(用于 VRAM 跟踪)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
#include <extra2d/core/service_locator.h>
|
#include <extra2d/graphics/opengl/gl_shader.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_shader.h>
|
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#include <extra2d/core/service_locator.h>
|
#include <extra2d/graphics/opengl/gl_sprite_batch.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_sprite_batch.h>
|
#include <extra2d/graphics/opengl/gl_texture.h>
|
||||||
#include <extra2d/graphics/backends/opengl/gl_texture.h>
|
|
||||||
#include <extra2d/graphics/shader/shader_manager.h>
|
#include <extra2d/graphics/shader/shader_manager.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
@ -93,7 +92,8 @@ void GLSpriteBatch::begin(const glm::mat4 &viewProjection) {
|
||||||
ebo_.bind();
|
ebo_.bind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GLSpriteBatch::begin(const glm::mat4 &viewProjection, Ptr<IShader> shader) {
|
void GLSpriteBatch::begin(const glm::mat4 &viewProjection,
|
||||||
|
Ptr<IShader> shader) {
|
||||||
// 设置自定义着色器
|
// 设置自定义着色器
|
||||||
if (shader) {
|
if (shader) {
|
||||||
shader_ = shader;
|
shader_ = shader;
|
||||||
|
|
@ -183,7 +183,7 @@ void GLSpriteBatch::submitBatch() {
|
||||||
uniformValues["u_viewProjection"] = viewProjection_;
|
uniformValues["u_viewProjection"] = viewProjection_;
|
||||||
|
|
||||||
// 合并额外的uniform值(如SDF字体的u_textureSize)
|
// 合并额外的uniform值(如SDF字体的u_textureSize)
|
||||||
for (const auto& [name, value] : extraUniforms_) {
|
for (const auto &[name, value] : extraUniforms_) {
|
||||||
uniformValues[name] = value;
|
uniformValues[name] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <extra2d/graphics/backends/opengl/gl_texture.h>
|
|
||||||
#include <extra2d/graphics/memory/gpu_context.h>
|
#include <extra2d/graphics/memory/gpu_context.h>
|
||||||
#include <extra2d/graphics/memory/vram_manager.h>
|
#include <extra2d/graphics/memory/vram_manager.h>
|
||||||
|
#include <extra2d/graphics/opengl/gl_texture.h>
|
||||||
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <extra2d/core/service_locator.h>
|
#include <extra2d/core/service_locator.h>
|
||||||
|
|
@ -8,7 +9,6 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <stb/stb_image.h>
|
#include <stb/stb_image.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
@ -351,8 +351,8 @@ bool GLTexture::loadKTX(const std::string &filepath) {
|
||||||
dataSize_ = imageSize;
|
dataSize_ = imageSize;
|
||||||
VRAMMgr::get().allocTexture(dataSize_);
|
VRAMMgr::get().allocTexture(dataSize_);
|
||||||
|
|
||||||
E2D_LOG_INFO("已加载 KTX 压缩纹理: {} ({}x{}, 格式={:#06x})",
|
E2D_LOG_INFO("已加载 KTX 压缩纹理: {} ({}x{}, 格式={:#06x})", filepath,
|
||||||
filepath, width_, height_, glInternalFormat);
|
width_, height_, glInternalFormat);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -412,8 +412,7 @@ bool GLTexture::loadDDS(const std::string &filepath) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
E2D_LOG_ERROR("DDS 文件未使用 DX10 扩展,不支持: {}",
|
E2D_LOG_ERROR("DDS 文件未使用 DX10 扩展,不支持: {}", filepath);
|
||||||
filepath);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -455,8 +454,7 @@ bool GLTexture::loadDDS(const std::string &filepath) {
|
||||||
dataSize_ = imageSize;
|
dataSize_ = imageSize;
|
||||||
VRAMMgr::get().allocTexture(dataSize_);
|
VRAMMgr::get().allocTexture(dataSize_);
|
||||||
|
|
||||||
E2D_LOG_INFO("已加载 DDS 压缩纹理: {} ({}x{})", filepath, width_,
|
E2D_LOG_INFO("已加载 DDS 压缩纹理: {} ({}x{})", filepath, width_, height_);
|
||||||
height_);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include "glfw_input.h"
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <extra2d/core/service_locator.h>
|
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
#include <glfw/glfw_input.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#include "glfw_window.h"
|
#include <glfw/glfw_input.h>
|
||||||
#include "glfw_input.h"
|
#include <glfw/glfw_window.h>
|
||||||
#include <extra2d/core/service_locator.h>
|
|
||||||
#include <extra2d/event/event.h>
|
#include <extra2d/event/event.h>
|
||||||
#include <extra2d/platform/keys.h>
|
#include <extra2d/platform/keys.h>
|
||||||
#include <extra2d/services/event_service.h>
|
#include <extra2d/services/event_service.h>
|
||||||
|
|
@ -488,79 +488,225 @@ void GLFWWindow::keyCallback(GLFWwindow *window, int key, int scancode,
|
||||||
// 将 GLFW key code 转换为引擎 Key 枚举值
|
// 将 GLFW key code 转换为引擎 Key 枚举值
|
||||||
Key eKey = Key::None;
|
Key eKey = Key::None;
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case GLFW_KEY_A: eKey = Key::A; break;
|
case GLFW_KEY_A:
|
||||||
case GLFW_KEY_B: eKey = Key::B; break;
|
eKey = Key::A;
|
||||||
case GLFW_KEY_C: eKey = Key::C; break;
|
break;
|
||||||
case GLFW_KEY_D: eKey = Key::D; break;
|
case GLFW_KEY_B:
|
||||||
case GLFW_KEY_E: eKey = Key::E; break;
|
eKey = Key::B;
|
||||||
case GLFW_KEY_F: eKey = Key::F; break;
|
break;
|
||||||
case GLFW_KEY_G: eKey = Key::G; break;
|
case GLFW_KEY_C:
|
||||||
case GLFW_KEY_H: eKey = Key::H; break;
|
eKey = Key::C;
|
||||||
case GLFW_KEY_I: eKey = Key::I; break;
|
break;
|
||||||
case GLFW_KEY_J: eKey = Key::J; break;
|
case GLFW_KEY_D:
|
||||||
case GLFW_KEY_K: eKey = Key::K; break;
|
eKey = Key::D;
|
||||||
case GLFW_KEY_L: eKey = Key::L; break;
|
break;
|
||||||
case GLFW_KEY_M: eKey = Key::M; break;
|
case GLFW_KEY_E:
|
||||||
case GLFW_KEY_N: eKey = Key::N; break;
|
eKey = Key::E;
|
||||||
case GLFW_KEY_O: eKey = Key::O; break;
|
break;
|
||||||
case GLFW_KEY_P: eKey = Key::P; break;
|
case GLFW_KEY_F:
|
||||||
case GLFW_KEY_Q: eKey = Key::Q; break;
|
eKey = Key::F;
|
||||||
case GLFW_KEY_R: eKey = Key::R; break;
|
break;
|
||||||
case GLFW_KEY_S: eKey = Key::S; break;
|
case GLFW_KEY_G:
|
||||||
case GLFW_KEY_T: eKey = Key::T; break;
|
eKey = Key::G;
|
||||||
case GLFW_KEY_U: eKey = Key::U; break;
|
break;
|
||||||
case GLFW_KEY_V: eKey = Key::V; break;
|
case GLFW_KEY_H:
|
||||||
case GLFW_KEY_W: eKey = Key::W; break;
|
eKey = Key::H;
|
||||||
case GLFW_KEY_X: eKey = Key::X; break;
|
break;
|
||||||
case GLFW_KEY_Y: eKey = Key::Y; break;
|
case GLFW_KEY_I:
|
||||||
case GLFW_KEY_Z: eKey = Key::Z; break;
|
eKey = Key::I;
|
||||||
case GLFW_KEY_0: eKey = Key::Num0; break;
|
break;
|
||||||
case GLFW_KEY_1: eKey = Key::Num1; break;
|
case GLFW_KEY_J:
|
||||||
case GLFW_KEY_2: eKey = Key::Num2; break;
|
eKey = Key::J;
|
||||||
case GLFW_KEY_3: eKey = Key::Num3; break;
|
break;
|
||||||
case GLFW_KEY_4: eKey = Key::Num4; break;
|
case GLFW_KEY_K:
|
||||||
case GLFW_KEY_5: eKey = Key::Num5; break;
|
eKey = Key::K;
|
||||||
case GLFW_KEY_6: eKey = Key::Num6; break;
|
break;
|
||||||
case GLFW_KEY_7: eKey = Key::Num7; break;
|
case GLFW_KEY_L:
|
||||||
case GLFW_KEY_8: eKey = Key::Num8; break;
|
eKey = Key::L;
|
||||||
case GLFW_KEY_9: eKey = Key::Num9; break;
|
break;
|
||||||
case GLFW_KEY_F1: eKey = Key::F1; break;
|
case GLFW_KEY_M:
|
||||||
case GLFW_KEY_F2: eKey = Key::F2; break;
|
eKey = Key::M;
|
||||||
case GLFW_KEY_F3: eKey = Key::F3; break;
|
break;
|
||||||
case GLFW_KEY_F4: eKey = Key::F4; break;
|
case GLFW_KEY_N:
|
||||||
case GLFW_KEY_F5: eKey = Key::F5; break;
|
eKey = Key::N;
|
||||||
case GLFW_KEY_F6: eKey = Key::F6; break;
|
break;
|
||||||
case GLFW_KEY_F7: eKey = Key::F7; break;
|
case GLFW_KEY_O:
|
||||||
case GLFW_KEY_F8: eKey = Key::F8; break;
|
eKey = Key::O;
|
||||||
case GLFW_KEY_F9: eKey = Key::F9; break;
|
break;
|
||||||
case GLFW_KEY_F10: eKey = Key::F10; break;
|
case GLFW_KEY_P:
|
||||||
case GLFW_KEY_F11: eKey = Key::F11; break;
|
eKey = Key::P;
|
||||||
case GLFW_KEY_F12: eKey = Key::F12; break;
|
break;
|
||||||
case GLFW_KEY_SPACE: eKey = Key::Space; break;
|
case GLFW_KEY_Q:
|
||||||
case GLFW_KEY_ENTER: eKey = Key::Enter; break;
|
eKey = Key::Q;
|
||||||
case GLFW_KEY_ESCAPE: eKey = Key::Escape; break;
|
break;
|
||||||
case GLFW_KEY_TAB: eKey = Key::Tab; break;
|
case GLFW_KEY_R:
|
||||||
case GLFW_KEY_BACKSPACE: eKey = Key::Backspace; break;
|
eKey = Key::R;
|
||||||
case GLFW_KEY_INSERT: eKey = Key::Insert; break;
|
break;
|
||||||
case GLFW_KEY_DELETE: eKey = Key::Delete; break;
|
case GLFW_KEY_S:
|
||||||
case GLFW_KEY_HOME: eKey = Key::Home; break;
|
eKey = Key::S;
|
||||||
case GLFW_KEY_END: eKey = Key::End; break;
|
break;
|
||||||
case GLFW_KEY_PAGE_UP: eKey = Key::PageUp; break;
|
case GLFW_KEY_T:
|
||||||
case GLFW_KEY_PAGE_DOWN: eKey = Key::PageDown; break;
|
eKey = Key::T;
|
||||||
case GLFW_KEY_UP: eKey = Key::Up; break;
|
break;
|
||||||
case GLFW_KEY_DOWN: eKey = Key::Down; break;
|
case GLFW_KEY_U:
|
||||||
case GLFW_KEY_LEFT: eKey = Key::Left; break;
|
eKey = Key::U;
|
||||||
case GLFW_KEY_RIGHT: eKey = Key::Right; break;
|
break;
|
||||||
case GLFW_KEY_LEFT_SHIFT: eKey = Key::LShift; break;
|
case GLFW_KEY_V:
|
||||||
case GLFW_KEY_RIGHT_SHIFT: eKey = Key::RShift; break;
|
eKey = Key::V;
|
||||||
case GLFW_KEY_LEFT_CONTROL: eKey = Key::LCtrl; break;
|
break;
|
||||||
case GLFW_KEY_RIGHT_CONTROL: eKey = Key::RCtrl; break;
|
case GLFW_KEY_W:
|
||||||
case GLFW_KEY_LEFT_ALT: eKey = Key::LAlt; break;
|
eKey = Key::W;
|
||||||
case GLFW_KEY_RIGHT_ALT: eKey = Key::RAlt; break;
|
break;
|
||||||
case GLFW_KEY_CAPS_LOCK: eKey = Key::CapsLock; break;
|
case GLFW_KEY_X:
|
||||||
case GLFW_KEY_NUM_LOCK: eKey = Key::NumLock; break;
|
eKey = Key::X;
|
||||||
case GLFW_KEY_SCROLL_LOCK: eKey = Key::ScrollLock; break;
|
break;
|
||||||
default: eKey = Key::None; 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) {
|
if (eKey != Key::None) {
|
||||||
|
|
|
||||||
|
|
@ -1,51 +1,53 @@
|
||||||
#include <extra2d/platform/input_module.h>
|
|
||||||
#include <extra2d/core/service_locator.h>
|
|
||||||
#include <extra2d/core/registry.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/platform/window_module.h>
|
||||||
|
#include <extra2d/services/event_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
InputModule::InputModule(std::function<void(InputCfg&)> configFn) {
|
InputModule::InputModule(std::function<void(InputCfg &)> configFn) {
|
||||||
configFn(cfg_);
|
configFn(cfg_);
|
||||||
}
|
}
|
||||||
|
|
||||||
InputModule::~InputModule() {
|
InputModule::~InputModule() {
|
||||||
if (initialized_) {
|
if (initialized_) {
|
||||||
shutdown();
|
shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InputModule::init() {
|
bool InputModule::init() {
|
||||||
if (initialized_) return true;
|
if (initialized_)
|
||||||
|
|
||||||
// 获取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;
|
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() {
|
void InputModule::shutdown() {
|
||||||
if (!initialized_) return;
|
if (!initialized_)
|
||||||
|
return;
|
||||||
input_ = nullptr;
|
|
||||||
initialized_ = false;
|
input_ = nullptr;
|
||||||
|
initialized_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputModule::update() {
|
void InputModule::update() {
|
||||||
if (initialized_ && input_) {
|
if (initialized_ && input_) {
|
||||||
input_->update();
|
input_->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace extra2d
|
} // namespace extra2d
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
#include <extra2d/platform/window_module.h>
|
#include <extra2d/platform/window_module.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
#include "backends/glfw/glfw_window.h"
|
#include <glfw/glfw_window.h>
|
||||||
|
|
||||||
#ifdef __SWITCH__
|
#ifdef __SWITCH__
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <extra2d/core/service_locator.h>
|
|
||||||
#include <extra2d/graphics/core/render_command.h>
|
#include <extra2d/graphics/core/render_command.h>
|
||||||
#include <extra2d/scene/node.h>
|
#include <extra2d/scene/node.h>
|
||||||
#include <extra2d/scene/scene.h>
|
#include <extra2d/scene/scene.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
#include <extra2d/core/service_locator.h>
|
|
||||||
#include <extra2d/graphics/core/render_backend.h>
|
#include <extra2d/graphics/core/render_backend.h>
|
||||||
#include <extra2d/graphics/core/render_command.h>
|
#include <extra2d/graphics/core/render_command.h>
|
||||||
#include <extra2d/scene/scene.h>
|
#include <extra2d/scene/scene.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <extra2d/app/application.h>
|
#include <extra2d/app/application.h>
|
||||||
#include <extra2d/core/service_locator.h>
|
|
||||||
#include <extra2d/graphics/core/render_backend.h>
|
#include <extra2d/graphics/core/render_backend.h>
|
||||||
#include <extra2d/graphics/core/render_command.h>
|
#include <extra2d/graphics/core/render_command.h>
|
||||||
#include <extra2d/platform/iinput.h>
|
#include <extra2d/platform/iinput.h>
|
||||||
|
|
@ -13,7 +12,6 @@
|
||||||
#include <extra2d/scene/transition_slide_scene.h>
|
#include <extra2d/scene/transition_slide_scene.h>
|
||||||
#include <extra2d/services/logger_service.h>
|
#include <extra2d/services/logger_service.h>
|
||||||
|
|
||||||
|
|
||||||
namespace extra2d {
|
namespace extra2d {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,14 @@ function define_extra2d_engine()
|
||||||
set_kind("static")
|
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 渲染后端源文件
|
-- OpenGL 渲染后端源文件
|
||||||
add_files("Extra2D/src/graphics/backends/opengl/*.cpp")
|
add_files("Extra2D/src/graphics/opengl/*.cpp")
|
||||||
add_files("Extra2D/src/glad/glad.c")
|
add_files("Extra2D/src/glad/glad.c")
|
||||||
|
|
||||||
-- GLFW 窗口后端源文件
|
-- GLFW 窗口后端源文件
|
||||||
add_files("Extra2D/src/platform/backends/glfw/*.cpp")
|
add_files("Extra2D/src/platform/glfw/*.cpp")
|
||||||
|
|
||||||
-- 头文件路径
|
-- 头文件路径
|
||||||
add_includedirs("Extra2D/include", {public = true})
|
add_includedirs("Extra2D/include", {public = true})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue