Extra2D/examples/resource_loading
ChestnutYueyue bdf78f5eca feat(resource): 实现资源管理系统及示例程序
新增资源管理系统,支持纹理、字体、着色器、音频等资源的加载和管理:
- 添加 ResourceManager 核心模块
- 实现 Texture、Font、Shader、Audio 等资源类
- 添加 FontAtlas 动态字符图集管理
- 实现 Material 材质系统
- 添加 Text 文本渲染支持
- 新增资源加载示例程序
- 更新构建系统以支持资源加载示例
- 完善文档和 README
2026-03-01 15:39:07 +08:00
..
README.md feat(resource): 实现资源管理系统及示例程序 2026-03-01 15:39:07 +08:00
main.cpp feat(resource): 实现资源管理系统及示例程序 2026-03-01 15:39:07 +08:00
xmake.lua feat(resource): 实现资源管理系统及示例程序 2026-03-01 15:39:07 +08:00

README.md

资源加载示例

演示如何使用 Extra2D 引擎的资源管理器加载各种资源。

功能演示

  1. 纹理加载 - 从文件加载 PNG/JPG 纹理
  2. 字体加载 - 使用 FreeType 加载 TTF 字体,支持中英文
  3. 文本创建 - 创建可渲染的文本对象
  4. 着色器加载 - 从文件或源码加载 GLSL 着色器
  5. 材质创建 - 组合纹理和着色器创建材质
  6. 音频加载 - 加载音效和音乐
  7. 资源统计 - 查看已加载资源的统计信息

目录结构

resource_loading/
├── main.cpp              # 示例主文件
├── xmake.lua            # 构建脚本
├── README.md            # 本文件
└── romfs/
    └── assets/          # 资源目录
        ├── textures/    # 纹理文件
        ├── fonts/       # 字体文件
        ├── audio/       # 音频文件
        └── shaders/     # 着色器文件

构建

MinGW (Windows)

xmake f -p mingw -a x86_64
xmake resource_loading

Nintendo Switch

xmake f -p switch -a aarch64
xmake resource_loading

运行

Windows

./build/examples/resource_loading/resource_loading.exe

Switch

将生成的 resource_loading.nro 复制到 Switch SD 卡运行。

资源管理 API 示例

// 获取资源管理器
auto& resources = app->context().resources();

// 加载纹理
auto texture = resources.getTexture("assets/textures/image.png");

// 加载字体
FontConfig config;
config.fontSize = 24;
config.preloadCommon = true;
auto font = resources.getFont("assets/fonts/font.ttf", config);

// 创建文本
auto text = resources.createText(font, "Hello 世界!");
text->setFontSize(32);
text->rebuild();

// 加载着色器
auto shader = resources.getShader("vs.glsl", "fs.glsl");

// 创建材质
auto material = resources.createMaterial(shader);
material->setTexture("uTexture", texture);
material->setColor("uColor", Color::Red);

// 加载音频
auto sound = resources.getAudio("jump.wav", AudioType::Sound);
auto music = resources.getAudio("bgm.mp3", AudioType::Music);

// 获取资源统计
auto stats = resources.getStats();
printf("Textures: %zu\n", stats.textureCount);

注意事项

  • 字体文件需要是 TTF 格式
  • 纹理支持 PNG、JPG、BMP 等格式(通过 stb_image
  • 音频支持 WAV、MP3、OGG 等格式(通过 SDL2_mixer
  • 着色器使用 OpenGL ES 3.2 语法