From c94e43089bbf97ee9ebcc8c9fe587635b25a338c Mon Sep 17 00:00:00 2001 From: Lenheart <947330670@qq.com> Date: Sun, 22 Feb 2026 14:28:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E5=90=8D=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Engine_Architecture.md | 567 ------------------ .../include/KHR/khrplatform.h | 0 .../include/frostbite2D}/core/application.h | 6 +- .../include/frostbite2D}/core/window.h | 6 +- .../include/frostbite2D}/module/module.h | 0 .../include/frostbite2D}/platform/switch.h | 0 .../include/frostbite2D}/types/type_alias.h | 0 .../include/frostbite2D}/types/type_color.h | 2 +- .../include/frostbite2D}/types/type_math.h | 2 +- .../include/frostbite2D}/utils/asset.h | 2 +- .../include/glad/glad.h | 0 .../include/stb/stb_image.h | 0 .../include/stb/stb_image_resize2.h | 0 .../include/stb/stb_image_write.h | 0 .../include/stb/stb_perlin.h | 0 .../include/stb/stb_rect_pack.h | 0 .../include/stb/stb_truetype.h | 0 .../src/frostbite2D}/core/application.cpp | 4 +- .../src/frostbite2D}/core/window.cpp | 2 +- .../src/frostbite2D}/platform/switch.cpp | 2 +- .../src/frostbite2D}/utils/asset.cpp | 2 +- {Fostbite2D => Frostbite2D}/src/glad/glad.c | 0 {Fostbite2D => Frostbite2D}/src/main.cpp | 6 +- README.md | 279 --------- platform/linux.lua | 6 +- platform/mingw.lua | 6 +- platform/switch.lua | 6 +- platform/windows.lua | 6 +- 28 files changed, 29 insertions(+), 875 deletions(-) delete mode 100644 Engine_Architecture.md rename {Fostbite2D => Frostbite2D}/include/KHR/khrplatform.h (100%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/core/application.h (97%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/core/window.h (97%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/module/module.h (100%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/platform/switch.h (100%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/types/type_alias.h (100%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/types/type_color.h (99%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/types/type_math.h (99%) rename {Fostbite2D/include/fostbite2D => Frostbite2D/include/frostbite2D}/utils/asset.h (99%) rename {Fostbite2D => Frostbite2D}/include/glad/glad.h (100%) rename {Fostbite2D => Frostbite2D}/include/stb/stb_image.h (100%) rename {Fostbite2D => Frostbite2D}/include/stb/stb_image_resize2.h (100%) rename {Fostbite2D => Frostbite2D}/include/stb/stb_image_write.h (100%) rename {Fostbite2D => Frostbite2D}/include/stb/stb_perlin.h (100%) rename {Fostbite2D => Frostbite2D}/include/stb/stb_rect_pack.h (100%) rename {Fostbite2D => Frostbite2D}/include/stb/stb_truetype.h (100%) rename {Fostbite2D/src/fostbite2D => Frostbite2D/src/frostbite2D}/core/application.cpp (93%) rename {Fostbite2D/src/fostbite2D => Frostbite2D/src/frostbite2D}/core/window.cpp (99%) rename {Fostbite2D/src/fostbite2D => Frostbite2D/src/frostbite2D}/platform/switch.cpp (86%) rename {Fostbite2D/src/fostbite2D => Frostbite2D/src/frostbite2D}/utils/asset.cpp (99%) rename {Fostbite2D => Frostbite2D}/src/glad/glad.c (100%) rename {Fostbite2D => Frostbite2D}/src/main.cpp (89%) delete mode 100644 README.md diff --git a/Engine_Architecture.md b/Engine_Architecture.md deleted file mode 100644 index 5d65ed4..0000000 --- a/Engine_Architecture.md +++ /dev/null @@ -1,567 +0,0 @@ -# Frostbite2D 引擎架构说明 - -## 1. 整体架构图 - -``` -┌─────────────────────────────────────────────────────────────────────────────┐ -│ 用户应用层 (main.cpp) │ -│ 创建配置 → 初始化应用 → 游戏循环 → 清理 │ -└─────────────────────────────────────────────────────────────────────────────┘ - │ - ▼ -┌─────────────────────────────────────────────────────────────────────────────┐ -│ 应用程序层 (Application) │ -│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ -│ │ 单例管理 │ │ 模块系统 │ │ 时间管理 │ │ 窗口管理 │ │ -│ │ get() │ │ use/init │ │ deltaTime() │ │ Window │ │ -│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │ -│ │ │ -│ ┌───────────┴───────────┐ │ -│ ▼ ▼ │ -│ mainLoop() ────────► update()/render() │ -└─────────────────────────────────────────────────────────────────────────────┘ - │ - ┌────────────────────────────┼────────────────────────────┐ - │ │ │ - ▼ ▼ ▼ -┌─────────────────┐ ┌─────────────────────┐ ┌─────────────────────┐ -│ 渲染系统 │ │ 着色器系统 │ │ 输入系统 │ -│ (GLRenderer) │◄────►│ (ShaderManager) │ │ (SDL Events) │ -└─────────────────┘ └─────────────────────┘ └─────────────────────┘ - -┌─────────────────────────────────────────────────────────────────────────────┐ -│ 渲染核心层 │ -│ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ ┌───────────┐ │ -│ │ 2D图形绘制 │ │ 精灵批处理 │ │ 字体渲染 │ │ 相机 │ │ -│ │ drawRect() │ │ GLSpriteBatch │ │ GLFontAtlas │ │ Camera │ │ -│ │ fillCircle() │ │ 10000 sprites │ │ stb_truetype │ │ view/proj │ │ -│ │ drawLine() │ │ drawSprite() │ │ cache glyphs │ │ move/zoom │ │ -│ │ drawText() │ │ endBatch() │ │ measureText() │ │ │ │ -│ └────────────────┘ └────────────────┘ └────────────────┘ └───────────┘ │ -│ │ -│ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ │ -│ │ 纹理管理 │ │ 着色器实现 │ │ OpenGL资源 │ │ -│ │ GLTexture │ │ GLShader │ │ VAO/VBO │ │ -│ │ load from file│ │ vertex/frag │ │ shape/line │ │ -│ └────────────────┘ └────────────────┘ └────────────────┘ │ -└─────────────────────────────────────────────────────────────────────────────┘ - │ - ▼ -┌─────────────────────────────────────────────────────────────────────────────┐ -│ 平台抽象层 │ -│ ┌─────────────────────────┐ ┌─────────────────────────────────────┐ │ -│ │ Window 窗口 │ │ SDL2 跨平台库 │ │ -│ │ - 创建/销毁窗口 │ │ 窗口管理 | 事件处理 | OpenGL上下文 │ │ -│ │ - OpenGL 上下文 │ │ │ │ -│ │ - 事件轮询 │ │ 支持: Windows | Linux | macOS │ │ -│ │ - 交换缓冲区 │ │ Android | iOS | Switch │ │ -│ └─────────────────────────┘ └─────────────────────────────────────┘ │ -└─────────────────────────────────────────────────────────────────────────────┘ - │ - ▼ -┌─────────────────────────────────────────────────────────────────────────────┐ -│ 第三方库 │ -│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │ -│ │ OpenGL │ │ SDL2 │ │ GLAD │ │ GLM │ │ stb_truetype │ │ -│ │ ES 3.2 │ │ 2.32.2 │ │ Loader │ │ 1.0.3 │ │ rect_pack │ │ -│ │ 图形API │ │ 窗口/输入 │ │ 函数加载 │ │ 数学库 │ │ 字体渲染 │ │ -│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ └──────────────┘ │ -└─────────────────────────────────────────────────────────────────────────────┘ -``` - -## 2. 模块详细说明 - -### 2.1 应用程序层 (Application) - -``` -┌─────────────────────────────────────────┐ -│ Application 单例 │ -├─────────────────────────────────────────┤ -│ + get() : Application& │ -│ + init(config) : bool │ -│ + run() : void │ -│ + quit() : void │ -│ + deltaTime() : float │ -│ + fps() : int │ -├─────────────────────────────────────────┤ -│ - window_ : Window* │ -│ - modules_ : vector │ -│ - running_ : bool │ -│ - deltaTime_ : float │ -│ - currentFps_ : int │ -└─────────────────────────────────────────┘ -``` - -**生命周期流程:** -``` -main() - │ - ├─► AppConfig config = AppConfig::createDefault() - │ - ├─► Application::get().init(config) - │ ├─► 创建 Window - │ ├─► 初始化 SDL - │ └─► 初始化 GLAD (OpenGL 函数加载) - │ - ├─► ShaderManager::getInstance().init(factory) - │ ├─► 加载 sprite shader - │ └─► 加载 shape shader - │ - ├─► GLRenderer.init(window) - │ ├─► 创建 VAO/VBO - │ ├─► 初始化 SpriteBatch - │ └─► 设置 OpenGL 状态 - │ - ├─► 游戏主循环 - │ ├─► 处理输入事件 (SDL_PollEvent) - │ ├─► renderer.beginFrame(color) // 清除缓冲区 - │ ├─► 绘制图形 - │ │ ├─► fillRect() 填充矩形 - │ │ ├─► fillCircle() 填充圆形 - │ │ ├─► drawLine() 绘制线条 - │ │ ├─► drawText() 绘制文字 - │ │ └─► ... - │ ├─► renderer.endFrame() // 提交批次 - │ └─► SDL_GL_SwapWindow() // 显示到屏幕 - │ - ├─► 清理资源 - │ ├─► renderer.shutdown() - │ ├─► ShaderManager::shutdown() - │ └─► Application::shutdown() - │ - └─► return 0 -``` - -### 2.2 渲染系统 (GLRenderer) - -**核心功能:** - -| 功能类别 | 方法 | 说明 | -|---------|------|------| -| **帧管理** | `beginFrame()` / `endFrame()` | 开始/结束渲染帧 | -| **2D形状** | `fillRect()` / `drawRect()` | 填充/描边矩形 | -| **圆形** | `fillCircle()` / `drawCircle()` | 填充/描边圆形 | -| **线条** | `drawLine()` | 绘制线段 | -| **三角形** | `fillTriangle()` / `drawTriangle()` | 填充/描边三角形 | -| **多边形** | `fillPolygon()` / `drawPolygon()` | 填充/描边多边形 | -| **精灵** | `drawSprite()` | 绘制纹理精灵 | -| **文字** | `drawText()` | 渲染文字 | -| **变换** | `pushTransform()` / `popTransform()` | 矩阵变换栈 | -| **视口** | `setViewport()` | 设置渲染区域 | -| **混合** | `setBlendMode()` | 设置混合模式 | - -**渲染管线:** - -``` -┌──────────────┐ ┌────────────────────────────────────────────────┐ -│ drawRect() │────►│ │ -└──────────────┘ │ 形状批处理缓冲区 │ -┌──────────────┐ │ shapeVertexCache_[MAX_SHAPE_VERTICES] │ -│ fillCircle() │────►│ │ -└──────────────┘ │ 顶点格式: {x, y, r, g, b, a} │ -┌──────────────┐ │ │ -│fillTriangle()│────►│ 当缓冲区满或模式改变时: flushShapeBatch() │ -└──────────────┘ │ │ - └────────────────────┬───────────────────────────┘ - │ - ▼ - ┌────────────────────────────────────────────────┐ - │ 线条批处理缓冲区 │ - │ lineVertexCache_[MAX_LINE_VERTICES] │ - │ │ - │ 绘制模式: GL_LINES (每线条2顶点) │ - │ 支持线宽设置 │ - └────────────────────┬───────────────────────────┘ - │ - ▼ - ┌────────────────────────────────────────────────┐ - │ 精灵批处理系统 │ - │ GLSpriteBatch (10000精灵) │ - │ │ - │ 图集纹理 → 顶点数据 → VBO → GPU 批量绘制 │ - │ │ - │ beginSpriteBatch() → drawSprite() → endBatch()│ - └────────────────────┬───────────────────────────┘ - │ - ▼ - ┌────────────────────────────────────────────────┐ - │ OpenGL 渲染 │ - │ │ - │ glDrawArrays(GL_TRIANGLES, ...) │ - │ glDrawArrays(GL_LINES, ...) │ - └────────────────────────────────────────────────┘ -``` - -### 2.3 着色器系统 (ShaderManager) - -``` -┌─────────────────────────────────────────────────────────────────┐ -│ ShaderManager 单例 │ -├─────────────────────────────────────────────────────────────────┤ -│ │ -│ ┌──────────────┐ ┌────────────────────────────────────┐ │ -│ │ IShader │◄───┤ GLShader (OpenGL实现) │ │ -│ │ 接口 │ │ - 编译 vertex/fragment shader │ │ -│ │ │ │ - 创建 program │ │ -│ │ bind() │ │ - 设置 uniform │ │ -│ │ setUniform() │ │ - 绑定/解绑 │ │ -│ └──────────────┘ └────────────────────────────────────┘ │ -│ ▲ │ -│ │ │ -│ ┌──────────────┐ ┌────────────────────────────────────┐ │ -│ │ IShaderFactory│◄──┤ GLShaderFactory │ │ -│ │ 工厂接口 │ │ - createShader() │ │ -│ │ │ │ - 创建 OpenGL Shader 实例 │ │ -│ └──────────────┘ └────────────────────────────────────┘ │ -│ │ -│ 管理: unordered_map shaders_ │ -│ │ -└─────────────────────────────────────────────────────────────────┘ -``` - -**内置着色器:** - -| 着色器 | 文件 | 用途 | -|-------|------|------| -| **sprite** | sprite.vert / sprite.frag | 纹理精灵渲染 (支持颜色混合) | -| **shape** | shape.vert / shape.frag | 2D形状渲染 (顶点颜色) | - -### 2.4 字体渲染 (GLFontAtlas) - -``` -┌─────────────────────────────────────────────────────────────────┐ -│ 字体图集系统 │ -├─────────────────────────────────────────────────────────────────┤ -│ │ -│ 初始化流程: │ -│ ┌───────────┐ ┌───────────┐ ┌──────────────────────┐ │ -│ │ 加载字体 │───►│ 初始化stb │───►│ 创建 1024x1024 图集 │ │ -│ │ 文件(.ttf)│ │ _truetype │ │ 纹理 (RGBA) │ │ -│ └───────────┘ └───────────┘ └──────────────────────┘ │ -│ │ │ -│ 渲染字符时: │ │ -│ ┌───────────┐ ┌───────────┐ ┌────────▼──────────┐ │ -│ │ 字符查缓存 │───►│ 缓存未命中 │───►│ stbtt_MakeCodepoint│ │ -│ │ │ │ │ │ _Bitmap() 栅格化 │ │ -│ └─────┬─────┘ └───────────┘ └────────┬──────────┘ │ -│ │ │ │ -│ │ ┌───────────┐ ┌─────────▼──────────┐ │ -│ └────────►│ 返回字形 │◄───│ stb_rect_pack 打包 │ │ -│ │ 信息 │ │ 到图集纹理 │ │ -│ └───────────┘ └────────────────────┘ │ -│ │ -│ Glyph 信息: │ -│ - 尺寸 (width, height) │ -│ - 偏移 (bearingX, bearingY) │ -│ - 步进 (advance) │ -│ - 纹理坐标 (u0, v0, u1, v1) │ -│ │ -└─────────────────────────────────────────────────────────────────┘ -``` - -### 2.5 相机系统 (Camera) - -``` -┌─────────────────────────────────────────┐ -│ Camera 相机 │ -├─────────────────────────────────────────┤ -│ │ -│ 视口配置: │ -│ ┌─────────────────────────────────┐ │ -│ │ left = 0 │ │ -│ │ right = 800 ─────────────── │ │ -│ │ top = 0 │ │ │ │ -│ │ bottom = 600 │ 世界坐标 │ │ │ -│ │ │ │ │ │ -│ │ 默认位置(0,0) │ │ │ │ -│ │ 看向左上角 └─────────────┘ │ │ -│ │ 视口 (0,0) │ │ -│ └─────────────────────────────────┘ │ -│ │ -│ 变换矩阵: │ -│ ┌───────────────┐ │ -│ │ viewMatrix │ 视图矩阵 (相机位置) │ -│ ├───────────────┤ │ -│ │ projMatrix │ 正交投影矩阵 │ -│ ├───────────────┤ │ -│ │ viewProj │ view * projection │ -│ └───────────────┘ │ -│ │ -│ 控制: │ -│ - move(x, y) 移动相机 │ -│ - setZoom(z) 设置缩放 │ -│ - setPosition() 设置位置 │ -│ │ -└─────────────────────────────────────────┘ -``` - -### 2.6 模块系统 (Module) - -``` -┌─────────────────────────────────────────┐ -│ Module 模块基类 │ -├─────────────────────────────────────────┤ -│ + setupModule() 初始化 │ -│ + destroyModule() 销毁 │ -│ + onUpdate() 每帧更新 │ -│ + beforeRender() 渲染前 │ -│ + onRender() 渲染时 │ -│ + afterRender() 渲染后 │ -│ + handleEvent() 事件处理 │ -│ + getName() 模块名称 │ -│ + getPriority() 优先级 (越小越优先) │ -└─────────────────────────────────────────┘ - ▲ - │ 继承 - ┌──────────────┼──────────────┐ - │ │ │ -┌───┴───┐ ┌─────┴─────┐ ┌────┴────┐ -│渲染模块│ │ 物理模块 │ │音频模块 │ -│ │ │ │ │ │ -└───────┘ └───────────┘ └─────────┘ -``` - -## 3. 数据流图 - -### 3.1 一帧的渲染流程 - -``` -┌──────────────────────────────────────────────────────────────────────┐ -│ 游戏主循环 │ -└─────────────────────────────────┬────────────────────────────────────┘ - │ - ▼ -┌──────────────────────────────────────────────────────────────────────┐ -│ ① 处理输入事件 │ -│ SDL_PollEvent() → 键盘/鼠标事件 → 更新相机/游戏逻辑 │ -└─────────────────────────────────┬────────────────────────────────────┘ - │ - ▼ -┌──────────────────────────────────────────────────────────────────────┐ -│ ② 开始新帧 (beginFrame) │ -│ glClear() 清除颜色缓冲区 + 重置渲染统计 │ -└─────────────────────────────────┬────────────────────────────────────┘ - │ - ▼ -┌──────────────────────────────────────────────────────────────────────┐ -│ ③ 设置渲染状态 │ -│ setViewport() + setViewProjection(camera.getViewProjectionMatrix)│ -└─────────────────────────────────┬────────────────────────────────────┘ - │ - ▼ -┌──────────────────────────────────────────────────────────────────────┐ -│ ④ 绘制各种图形 │ -│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ -│ │ fillRect() │ │fillCircle() │ │ drawLine() │ │drawSprite() │ │ -│ │ │ │ │ │ │ │ │ │ -│ │ 顶点数据 ──►│ │ 顶点数据 ──►│ │ 顶点数据 ──►│ │ 精灵数据 ──►│ │ -│ │ 形状缓冲区 │ │ 形状缓冲区 │ │ 线条缓冲区 │ │ 精灵批次 │ │ -│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │ -└─────────────────────────────────┬────────────────────────────────────┘ - │ - ▼ -┌──────────────────────────────────────────────────────────────────────┐ -│ ⑤ 结束帧 (endFrame) │ -│ flushShapeBatch() + flushLineBatch() + endSpriteBatch() │ -│ 提交所有待渲染数据到 GPU │ -└─────────────────────────────────┬────────────────────────────────────┘ - │ - ▼ -┌──────────────────────────────────────────────────────────────────────┐ -│ ⑥ 交换缓冲区 │ -│ SDL_GL_SwapWindow() 将渲染结果显示到屏幕 │ -└──────────────────────────────────────────────────────────────────────┘ -``` - -### 3.2 资源加载流程 - -``` -纹理加载: -┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ -│ 文件路径 │───►│ stb_image│───►│ 像素数据 │───►│ GLTexture │ -│ .png/.jpg│ │ 解码 │ │ RGBA │ │ glTexImage2D -└──────────┘ └──────────┘ └──────────┘ └──────────┘ - -字体加载: -┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ -│ .ttf文件 │───►│读取到内存│───►│stbtt_Init│───►│GLFontAtlas│ -│ │ │ │ │Font() │ │ 创建图集 │ -└──────────┘ └──────────┘ └──────────┘ └──────────┘ - -着色器加载: -┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ -│.vert文件 │───►│读取源码 │───►│glCompile │───►│glLink │ -│.frag文件 │───►│ │ │Shader() │ │Program │ -└──────────┘ └──────────┘ └──────────┘ └──────────┘ -``` - -## 4. 项目目录结构 - -``` -Frostbite2D/ -├── xmake.lua # 主构建配置 -├── platform/ -│ └── windows.lua # Windows 平台配置 -├── shaders/ -│ ├── shape.vert # 形状着色器 - 顶点 -│ ├── shape.frag # 形状着色器 - 片段 -│ ├── sprite.vert # 精灵着色器 - 顶点 -│ └── sprite.frag # 精灵着色器 - 片段 -└── Fostbite2D/ - ├── include/ - │ ├── fostbite2D/ - │ │ ├── app/ - │ │ │ └── application.h # 应用主类 - │ │ ├── config/ - │ │ │ ├── app_config.h # 应用配置 - │ │ │ └── platform_config.h # 平台配置 - │ │ ├── core/ - │ │ │ ├── color.h # 颜色类 - │ │ │ ├── math_types.h # 数学类型 - │ │ │ └── types.h # 基础类型定义 - │ │ ├── module/ - │ │ │ └── module.h # 模块基类 - │ │ ├── platform/ - │ │ │ └── window.h # 窗口类 - │ │ └── render/ - │ │ ├── camera.h # 相机 - │ │ ├── font.h # 字体接口 - │ │ ├── texture.h # 纹理接口 - │ │ ├── opengl/ - │ │ │ ├── gl_font_atlas.h # OpenGL字体图集 - │ │ │ ├── gl_renderer.h # OpenGL渲染器 - │ │ │ ├── gl_shader.h # OpenGL着色器 - │ │ │ ├── gl_sprite_batch.h # 精灵批处理 - │ │ │ └── gl_texture.h # OpenGL纹理 - │ │ └── shader/ - │ │ ├── shader_interface.h # 着色器接口 - │ │ └── shader_manager.h # 着色器管理器 - │ ├── glad/ - │ │ └── glad.h # OpenGL 加载器 - │ ├── KHR/ - │ │ └── khrplatform.h # Khronos 平台定义 - │ └── stb/ - │ ├── stb_image.h # 图片加载 - │ ├── stb_rect_pack.h # 矩形打包 - │ └── stb_truetype.h # 字体渲染 - └── src/ - ├── main.cpp # 程序入口 - ├── glad/ - │ └── glad.c # GLAD 实现 - └── fostbite2D/ - ├── app/ - │ └── application.cpp # Application 实现 - ├── config/ - │ └── app_config.cpp # 配置实现 - ├── platform/ - │ └── window.cpp # Window 实现 - └── render/ - ├── camera.cpp # Camera 实现 - ├── texture.cpp # Texture 实现 - ├── opengl/ # OpenGL 实现 - └── shader/ # Shader 实现 -``` - -## 5. 核心类关系图 - -``` - ┌─────────────────┐ - │ Application │ - │ 单例 │ - └────────┬────────┘ - │ - ┌───────────────────────────┼───────────────────────────┐ - │ │ │ - ▼ ▼ ▼ -┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ -│ Window │◄───────►│ ShaderManager │◄───────►│ GLRenderer │ -│ (SDL2) │ │ 单例 │ │ │ -└─────────────────┘ └────────┬────────┘ └───────┬─────────┘ - │ │ - ▼ ▼ - ┌─────────────────┐ ┌─────────────────┐ - │ IShader │ │ GLSpriteBatch │ - │ 接口 │ │ 精灵批处理 │ - └────────┬────────┘ └───────┬─────────┘ - │ │ - ▼ ▼ - ┌─────────────────┐ ┌─────────────────┐ - │ GLShader │ │ GLTexture │ - │ OpenGL实现 │ │ 纹理管理 │ - └─────────────────┘ └─────────────────┘ - -┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ -│ Camera │◄───────►│ GLFontAtlas │◄───────►│ FontAtlas │ -│ 2D相机 │ │ 字体图集 │ │ 接口 │ -│ view/proj矩阵 │ │ stb_truetype │ │ │ -└─────────────────┘ └─────────────────┘ └─────────────────┘ -``` - -## 6. 关键技术点 - -### 6.1 批处理渲染 (Batch Rendering) - -- **形状批处理**: 8192 个顶点缓冲区,减少 draw call -- **线条批处理**: 16384 个顶点,支持线宽变化时 flush -- **精灵批处理**: 10000 个精灵,使用单一 draw call - -### 6.2 字体图集 (Font Atlas) - -- 使用 `stb_rect_pack` 进行矩形打包 -- 1024x1024 RGBA 纹理存储字形 -- 动态缓存:首次使用字符时渲染到图集 - -### 6.3 坐标系 - -- **世界坐标**: Y轴向下 (0,0) 在左上角 -- **纹理坐标**: OpenGL 标准,(0,0) 在左下角 -- **相机**: 正交投影,可移动/缩放 - -### 6.4 渲染状态管理 - -- 自动处理 OpenGL 状态缓存 -- 混合模式切换 (None/Alpha/Additive/Multiply) -- 变换矩阵栈支持嵌套变换 - -## 7. 使用示例 - -```cpp -// 1. 初始化 -AppConfig config = AppConfig::createDefault(); -config.windowConfig.title = "My Game"; -Application::get().init(config); - -// 2. 初始化渲染 -GLRenderer renderer; -renderer.init(sdlWindow); - -// 3. 加载字体 -GLFontAtlas font("C:/Windows/Fonts/arial.ttf", 24); - -// 4. 游戏循环 -while (running) { - // 处理事件... - - // 开始渲染 - renderer.beginFrame(Color(0.1f, 0.1f, 0.15f, 1.0f)); - renderer.setViewProjection(camera.getViewProjectionMatrix()); - - // 绘制图形 - renderer.fillRect(Rect(100, 100, 200, 150), Colors::Red); - renderer.fillCircle(Vec2(400, 300), 50.0f, Colors::Blue, 32); - renderer.drawLine(Vec2(0, 0), Vec2(800, 600), Colors::White, 2.0f); - - // 绘制文字 - renderer.beginSpriteBatch(); - renderer.drawText(font, "Hello World!", 100, 100, Colors::White); - renderer.endSpriteBatch(); - - // 结束渲染 - renderer.endFrame(); - SDL_GL_SwapWindow(sdlWindow); -} - -// 5. 清理 -renderer.shutdown(); -Application::get().shutdown(); -``` diff --git a/Fostbite2D/include/KHR/khrplatform.h b/Frostbite2D/include/KHR/khrplatform.h similarity index 100% rename from Fostbite2D/include/KHR/khrplatform.h rename to Frostbite2D/include/KHR/khrplatform.h diff --git a/Fostbite2D/include/fostbite2D/core/application.h b/Frostbite2D/include/frostbite2D/core/application.h similarity index 97% rename from Fostbite2D/include/fostbite2D/core/application.h rename to Frostbite2D/include/frostbite2D/core/application.h index 65cd2cd..9688cc7 100644 --- a/Fostbite2D/include/fostbite2D/core/application.h +++ b/Frostbite2D/include/frostbite2D/core/application.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include namespace frostbite2D { diff --git a/Fostbite2D/include/fostbite2D/core/window.h b/Frostbite2D/include/frostbite2D/core/window.h similarity index 97% rename from Fostbite2D/include/fostbite2D/core/window.h rename to Frostbite2D/include/frostbite2D/core/window.h index 3c63be2..2d119f4 100644 --- a/Fostbite2D/include/fostbite2D/core/window.h +++ b/Frostbite2D/include/frostbite2D/core/window.h @@ -1,8 +1,8 @@ #pragma once #include -#include -#include +#include +#include #include #include @@ -64,7 +64,7 @@ struct Icon { struct WindowConfig { uint32_t width = 640; ///< 窗口宽度 uint32_t height = 480; ///< 窗口高度 - std::string title = "fostbite2D Game"; ///< 窗口标题 + std::string title = "frostbite2D Game"; ///< 窗口标题 Icon icon; ///< 窗口图标 bool resizable = false; ///< 窗口大小可调整 bool fullscreen = false; ///< 窗口全屏 diff --git a/Fostbite2D/include/fostbite2D/module/module.h b/Frostbite2D/include/frostbite2D/module/module.h similarity index 100% rename from Fostbite2D/include/fostbite2D/module/module.h rename to Frostbite2D/include/frostbite2D/module/module.h diff --git a/Fostbite2D/include/fostbite2D/platform/switch.h b/Frostbite2D/include/frostbite2D/platform/switch.h similarity index 100% rename from Fostbite2D/include/fostbite2D/platform/switch.h rename to Frostbite2D/include/frostbite2D/platform/switch.h diff --git a/Fostbite2D/include/fostbite2D/types/type_alias.h b/Frostbite2D/include/frostbite2D/types/type_alias.h similarity index 100% rename from Fostbite2D/include/fostbite2D/types/type_alias.h rename to Frostbite2D/include/frostbite2D/types/type_alias.h diff --git a/Fostbite2D/include/fostbite2D/types/type_color.h b/Frostbite2D/include/frostbite2D/types/type_color.h similarity index 99% rename from Fostbite2D/include/fostbite2D/types/type_color.h rename to Frostbite2D/include/frostbite2D/types/type_color.h index 584caea..db5c917 100644 --- a/Fostbite2D/include/fostbite2D/types/type_color.h +++ b/Frostbite2D/include/frostbite2D/types/type_color.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/Fostbite2D/include/fostbite2D/types/type_math.h b/Frostbite2D/include/frostbite2D/types/type_math.h similarity index 99% rename from Fostbite2D/include/fostbite2D/types/type_math.h rename to Frostbite2D/include/frostbite2D/types/type_math.h index 66d70ec..f74b263 100644 --- a/Fostbite2D/include/fostbite2D/types/type_math.h +++ b/Frostbite2D/include/frostbite2D/types/type_math.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/Fostbite2D/include/fostbite2D/utils/asset.h b/Frostbite2D/include/frostbite2D/utils/asset.h similarity index 99% rename from Fostbite2D/include/fostbite2D/utils/asset.h rename to Frostbite2D/include/frostbite2D/utils/asset.h index 644d884..24f3b81 100644 --- a/Fostbite2D/include/fostbite2D/utils/asset.h +++ b/Frostbite2D/include/frostbite2D/utils/asset.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/Fostbite2D/include/glad/glad.h b/Frostbite2D/include/glad/glad.h similarity index 100% rename from Fostbite2D/include/glad/glad.h rename to Frostbite2D/include/glad/glad.h diff --git a/Fostbite2D/include/stb/stb_image.h b/Frostbite2D/include/stb/stb_image.h similarity index 100% rename from Fostbite2D/include/stb/stb_image.h rename to Frostbite2D/include/stb/stb_image.h diff --git a/Fostbite2D/include/stb/stb_image_resize2.h b/Frostbite2D/include/stb/stb_image_resize2.h similarity index 100% rename from Fostbite2D/include/stb/stb_image_resize2.h rename to Frostbite2D/include/stb/stb_image_resize2.h diff --git a/Fostbite2D/include/stb/stb_image_write.h b/Frostbite2D/include/stb/stb_image_write.h similarity index 100% rename from Fostbite2D/include/stb/stb_image_write.h rename to Frostbite2D/include/stb/stb_image_write.h diff --git a/Fostbite2D/include/stb/stb_perlin.h b/Frostbite2D/include/stb/stb_perlin.h similarity index 100% rename from Fostbite2D/include/stb/stb_perlin.h rename to Frostbite2D/include/stb/stb_perlin.h diff --git a/Fostbite2D/include/stb/stb_rect_pack.h b/Frostbite2D/include/stb/stb_rect_pack.h similarity index 100% rename from Fostbite2D/include/stb/stb_rect_pack.h rename to Frostbite2D/include/stb/stb_rect_pack.h diff --git a/Fostbite2D/include/stb/stb_truetype.h b/Frostbite2D/include/stb/stb_truetype.h similarity index 100% rename from Fostbite2D/include/stb/stb_truetype.h rename to Frostbite2D/include/stb/stb_truetype.h diff --git a/Fostbite2D/src/fostbite2D/core/application.cpp b/Frostbite2D/src/frostbite2D/core/application.cpp similarity index 93% rename from Fostbite2D/src/fostbite2D/core/application.cpp rename to Frostbite2D/src/frostbite2D/core/application.cpp index f6752b7..e6d17da 100644 --- a/Fostbite2D/src/fostbite2D/core/application.cpp +++ b/Frostbite2D/src/frostbite2D/core/application.cpp @@ -1,6 +1,6 @@ #include -#include -#include +#include +#include namespace frostbite2D { Application &Application::get() { diff --git a/Fostbite2D/src/fostbite2D/core/window.cpp b/Frostbite2D/src/frostbite2D/core/window.cpp similarity index 99% rename from Fostbite2D/src/fostbite2D/core/window.cpp rename to Frostbite2D/src/frostbite2D/core/window.cpp index dbd1f23..5258b45 100644 --- a/Fostbite2D/src/fostbite2D/core/window.cpp +++ b/Frostbite2D/src/frostbite2D/core/window.cpp @@ -1,6 +1,6 @@ #include "SDL_log.h" #include -#include +#include #include diff --git a/Fostbite2D/src/fostbite2D/platform/switch.cpp b/Frostbite2D/src/frostbite2D/platform/switch.cpp similarity index 86% rename from Fostbite2D/src/fostbite2D/platform/switch.cpp rename to Frostbite2D/src/frostbite2D/platform/switch.cpp index 7d98c4c..9ba89b1 100644 --- a/Fostbite2D/src/fostbite2D/platform/switch.cpp +++ b/Frostbite2D/src/frostbite2D/platform/switch.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef __SWITCH__ namespace frostbite2D { diff --git a/Fostbite2D/src/fostbite2D/utils/asset.cpp b/Frostbite2D/src/frostbite2D/utils/asset.cpp similarity index 99% rename from Fostbite2D/src/fostbite2D/utils/asset.cpp rename to Frostbite2D/src/frostbite2D/utils/asset.cpp index ac6754d..9add6da 100644 --- a/Fostbite2D/src/fostbite2D/utils/asset.cpp +++ b/Frostbite2D/src/frostbite2D/utils/asset.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/Fostbite2D/src/glad/glad.c b/Frostbite2D/src/glad/glad.c similarity index 100% rename from Fostbite2D/src/glad/glad.c rename to Frostbite2D/src/glad/glad.c diff --git a/Fostbite2D/src/main.cpp b/Frostbite2D/src/main.cpp similarity index 89% rename from Fostbite2D/src/main.cpp rename to Frostbite2D/src/main.cpp index 529d7de..c727d9e 100644 --- a/Fostbite2D/src/main.cpp +++ b/Frostbite2D/src/main.cpp @@ -1,8 +1,8 @@ #include "SDL_log.h" #include -#include -#include -#include +#include +#include +#include #include diff --git a/README.md b/README.md deleted file mode 100644 index 19eed32..0000000 --- a/README.md +++ /dev/null @@ -1,279 +0,0 @@ -# Frostbite2D - -一个轻量级、跨平台的 2D 游戏引擎,使用现代 C++ 编写。 - -[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -[![C++17](https://img.shields.io/badge/C%2B%2B-17-blue.svg)](https://en.cppreference.com/w/cpp/17) -[![Platform](https://img.shields.io/badge/Platform-Windows%20%7C%20Linux%20%7C%20macOS%20%7C%20Switch-green.svg)](#平台支持) - -## 特性 - -- **跨平台**: 支持 Windows、Linux、macOS 和 Nintendo Switch -- **现代 C++17**: 类型安全的代码,使用智能指针 -- **OpenGL ES 3.2**: 硬件加速的 2D 渲染 -- **模块化架构**: 灵活的模块系统,易于扩展 -- **UTF-8 支持**: 完整支持中文及其他 Unicode 路径 -- **批量渲染**: 高效的精灵批处理,支持数千个精灵 -- **字体渲染**: 通过 stb_truetype 支持 TrueType 字体 - -## 平台支持 - -| 平台 | 状态 | -|------|------| -| Windows | ✅ 已支持 | -| Linux | ✅ 已支持 | -| macOS | ✅ 已支持 | -| Nintendo Switch | ✅ 已支持 (homebrew) | - -## 依赖库 - -- **SDL2** - 窗口管理和输入处理 -- **OpenGL ES 3.2** - 图形 API -- **GLAD** - OpenGL 函数加载器 -- **GLM** - 数学库 -- **stb 库** - 图像加载、字体渲染等 - -## 构建 - -### 环境要求 - -- [xmake](https://xmake.io/zh-cn/) 构建系统 -- 支持 C++17 的编译器 -- SDL2 开发库 - -### 构建命令 - -```bash -# 克隆仓库 -git clone https://github.com/yourusername/Frostbite2D.git -cd Frostbite2D - -# 构建项目 -xmake build - -# 运行 -xmake run -``` - -### 指定平台构建 - -```bash -# Windows -xmake config --plat=windows -xmake build - -# Linux -xmake config --plat=linux -xmake build - -# Nintendo Switch (需要 devkitPro) -xmake config --plat=switch -xmake build -``` - -## 快速开始 - -```cpp -#include -#include - -using namespace frostbite2D; - -class GameModule : public Module { -public: - const char* getName() const override { return "GameModule"; } - - void setupModule() override { - auto& asset = Asset::get(); - asset.setWorkingDirectory("assets"); - } - - void onRender() override { - // 在这里编写渲染代码 - } -}; - -int main() { - auto& app = Application::get(); - - AppConfig config = AppConfig::createDefault(); - config.appName = "我的游戏"; - config.windowConfig.width = 1280; - config.windowConfig.height = 720; - - if (!app.init(config)) { - return -1; - } - - app.use(new GameModule()); - app.run(); - - return 0; -} -``` - -## 架构 - -``` -┌─────────────────────────────────────────────────────────────┐ -│ 用户应用层 │ -└─────────────────────────────────────────────────────────────┘ - │ - ▼ -┌─────────────────────────────────────────────────────────────┐ -│ 应用程序层 │ -│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ -│ │ 单例管理 │ │ 模块系统 │ │ 时间管理 │ │ 窗口管理 │ │ -│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ -└─────────────────────────────────────────────────────────────┘ - │ - ▼ -┌─────────────────────────────────────────────────────────────┐ -│ 核心系统层 │ -│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ -│ │ 渲染器 │ │ 着色器 │ │ 资源管理 │ │ 输入系统 │ │ -│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ -└─────────────────────────────────────────────────────────────┘ - │ - ▼ -┌─────────────────────────────────────────────────────────────┐ -│ 平台抽象层 │ -│ SDL2 + OpenGL │ -└─────────────────────────────────────────────────────────────┘ -``` - -## 核心模块 - -### Application(应用程序) - -主入口点和生命周期管理器。 - -```cpp -Application::get().init(config); // 初始化 -Application::get().use(module); // 添加模块 -Application::get().run(); // 运行主循环 -``` - -### Window(窗口) - -跨平台窗口管理,支持 OpenGL 上下文。 - -```cpp -WindowConfig config; -config.width = 1280; -config.height = 720; -config.title = "我的窗口"; -config.vsync = true; -config.fullscreen = false; -``` - -### Asset(资源管理) - -文件系统工具,支持 UTF-8 中文路径。 - -```cpp -auto& asset = Asset::get(); - -// 设置工作目录 -asset.setWorkingDirectory("D:/游戏/资源"); - -// 读取文件 -std::string content; -asset.readTextFile("config.json", content); - -// 遍历文件 -auto files = asset.listFiles("textures", true); -auto pngs = asset.listFilesWithExtension("sprites", ".png"); -``` - -### Math Types(数学类型) - -基于 GLM 的 2D 数学工具。 - -```cpp -Vec2 position(100.0f, 200.0f); -Size size(50.0f, 50.0f); -Rect bounds(position, size); - -// 变换 -auto transform = Transform2D::translation(100, 100) - * Transform2D::rotation(45.0f) - * Transform2D::scaling(2.0f); -``` - -### Module System(模块系统) - -创建自定义模块来扩展功能。 - -```cpp -class MyModule : public Module { -public: - const char* getName() const override { return "MyModule"; } - - void setupModule() override { /* 初始化 */ } - void onUpdate() override { /* 更新逻辑 */ } - void onRender() override { /* 渲染 */ } - void destroyModule() override { /* 清理 */ } -}; -``` - -## 项目结构 - -``` -Frostbite2D/ -├── Fostbite2D/ -│ ├── include/ -│ │ └── fostbite2D/ -│ │ ├── core/ # 应用程序、窗口 -│ │ ├── module/ # 模块系统 -│ │ ├── platform/ # 平台相关代码 -│ │ ├── types/ # 数学类型、颜色 -│ │ └── utils/ # 资源管理 -│ └── src/ -│ └── fostbite2D/ -│ ├── core/ -│ ├── platform/ -│ └── utils/ -├── platform/ # 平台构建配置 -│ ├── windows.lua -│ ├── linux.lua -│ └── switch.lua -├── xmake.lua # 构建配置 -└── README.md -``` - -## API 参考 - -### Application - -| 方法 | 说明 | -|------|------| -| `get()` | 获取单例实例 | -| `init(config)` | 使用配置初始化 | -| `run()` | 启动主循环 | -| `quit()` | 请求退出 | -| `use(module)` | 添加模块 | -| `deltaTime()` | 获取帧间隔时间 | -| `fps()` | 获取当前帧率 | - -### Asset - -| 方法 | 说明 | -|------|------| -| `readTextFile(path, out)` | 读取文本文件 | -| `writeTextFile(path, content)` | 写入文本文件 | -| `readBinaryFile(path, out)` | 读取二进制文件 | -| `listFiles(dir, recursive)` | 列出目录中的文件 | -| `exists(path)` | 检查路径是否存在 | -| `setWorkingDirectory(path)` | 设置工作目录 | - -## 许可证 - -本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件。 - -## 致谢 - -- [SDL2](https://www.libsdl.org/) - 跨平台多媒体库 -- [GLM](https://github.com/g-truc/glm) - OpenGL 数学库 -- [stb libraries](https://github.com/nothings/stb) - 单文件库集合 -- [xmake](https://xmake.io/zh-cn/) - 跨平台构建系统 diff --git a/platform/linux.lua b/platform/linux.lua index f187952..9a189cf 100644 --- a/platform/linux.lua +++ b/platform/linux.lua @@ -4,9 +4,9 @@ add_requires("glm") target("Frostbite2D") set_kind("binary") - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.cpp")) - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.c")) - add_includedirs(path.join(os.projectdir(), "Fostbite2D/include")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) + add_includedirs(path.join(os.projectdir(), "Frostbite2D/include")) add_packages("libsdl2") add_packages("glm") diff --git a/platform/mingw.lua b/platform/mingw.lua index 8b84511..27e3353 100644 --- a/platform/mingw.lua +++ b/platform/mingw.lua @@ -6,9 +6,9 @@ add_requires("glm") target("Frostbite2D") set_kind("binary") - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.cpp")) - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.c")) - add_includedirs(path.join(os.projectdir(), "Fostbite2D/include")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) + add_includedirs(path.join(os.projectdir(), "Frostbite2D/include")) add_packages("libsdl2") add_packages("glm") diff --git a/platform/switch.lua b/platform/switch.lua index dd9b166..10d9cd1 100644 --- a/platform/switch.lua +++ b/platform/switch.lua @@ -1,9 +1,9 @@ target("Frostbite2D") set_kind("binary") - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.cpp")) - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.c")) - add_includedirs(path.join(os.projectdir(), "Fostbite2D/include")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) + add_includedirs(path.join(os.projectdir(), "Frostbite2D/include")) -- 检查 DEVKITPRO 环境变量(Windows 上使用 C:/devkitPro) local devkitPro = os.getenv("DEVKITPRO") or "L:/Switch/devkitPro" diff --git a/platform/windows.lua b/platform/windows.lua index 3565af9..058bd37 100644 --- a/platform/windows.lua +++ b/platform/windows.lua @@ -6,9 +6,9 @@ add_requires("glm") target("Frostbite2D") set_kind("binary") - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.cpp")) - add_files(path.join(os.projectdir(), "Fostbite2D/src/**.c")) - add_includedirs(path.join(os.projectdir(), "Fostbite2D/include")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.cpp")) + add_files(path.join(os.projectdir(), "Frostbite2D/src/**.c")) + add_includedirs(path.join(os.projectdir(), "Frostbite2D/include")) add_packages("libsdl2") add_packages("glm")