# 资源加载示例 演示如何使用 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) ```bash xmake f -p mingw -a x86_64 xmake resource_loading ``` ### Nintendo Switch ```bash xmake f -p switch -a aarch64 xmake resource_loading ``` ## 运行 ### Windows ```bash ./build/examples/resource_loading/resource_loading.exe ``` ### Switch 将生成的 `resource_loading.nro` 复制到 Switch SD 卡运行。 ## 资源管理 API 示例 ```cpp // 获取资源管理器 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 语法