222 lines
7.0 KiB
C++
222 lines
7.0 KiB
C++
#include <extra2d.h>
|
|
#include <cstdio>
|
|
|
|
using namespace extra2d;
|
|
|
|
/**
|
|
* @brief 资源加载示例
|
|
*
|
|
* 演示如何使用资源管理器加载各种资源:
|
|
* - 纹理 (Texture)
|
|
* - 字体 (Font)
|
|
* - 着色器 (Shader)
|
|
* - 音频 (Audio)
|
|
*/
|
|
|
|
int main(int argc, char **argv) {
|
|
// 创建应用(自动创建窗口)
|
|
auto app = Application::create();
|
|
|
|
AppConfig config;
|
|
config.title = "Resource Loading Example";
|
|
config.width = 1280;
|
|
config.height = 720;
|
|
|
|
if (!app->init(config)) {
|
|
printf("Failed to initialize!\n");
|
|
return -1;
|
|
}
|
|
|
|
// 获取资源管理器
|
|
auto& resources = app->getContext()->resources();
|
|
|
|
printf("=== Resource Loading Example ===\n\n");
|
|
|
|
// ============================================
|
|
// 1. 加载纹理
|
|
// ============================================
|
|
printf("1. Loading Textures...\n");
|
|
|
|
// 从文件加载纹理
|
|
auto texture = resources.getTexture("assets/textures/test.png");
|
|
if (texture) {
|
|
printf(" ✓ Loaded texture: %dx%d\n", texture->getWidth(), texture->getHeight());
|
|
} else {
|
|
printf(" ✗ Failed to load texture (file not found)\n");
|
|
}
|
|
|
|
// 使用默认纹理
|
|
auto defaultTex = resources.getDefaultTexture();
|
|
if (defaultTex) {
|
|
printf(" ✓ Default texture available: %dx%d\n",
|
|
defaultTex->getWidth(), defaultTex->getHeight());
|
|
}
|
|
|
|
// ============================================
|
|
// 2. 加载字体
|
|
// ============================================
|
|
printf("\n2. Loading Fonts...\n");
|
|
|
|
FontConfig fontConfig;
|
|
fontConfig.fontSize = 24;
|
|
fontConfig.preloadCommon = true; // 预加载常用字符
|
|
|
|
auto font = resources.getFont("assets/fonts/font.ttf", fontConfig);
|
|
if (font) {
|
|
printf(" ✓ Loaded font with size %d\n", fontConfig.fontSize);
|
|
|
|
// 测试字体测量
|
|
Vec2 textSize = font->measureText("Hello 世界!", 24.0f);
|
|
printf(" ✓ Text 'Hello 世界!' size: %.1f x %.1f\n", textSize.x, textSize.y);
|
|
|
|
// 获取字符信息(会自动渲染并缓存)
|
|
const GlyphInfo* glyph = font->getGlyph('A');
|
|
if (glyph && glyph->valid) {
|
|
printf(" ✓ Glyph 'A' cached in atlas page %d\n", glyph->pageIndex);
|
|
}
|
|
} else {
|
|
printf(" ✗ Failed to load font (file not found)\n");
|
|
}
|
|
|
|
// ============================================
|
|
// 3. 创建文本对象
|
|
// ============================================
|
|
printf("\n3. Creating Text Objects...\n");
|
|
|
|
if (font) {
|
|
auto text = resources.createText(font, "Resource Loading Example\n你好,世界!");
|
|
text->setFontSize(32);
|
|
text->setColor(Color::White);
|
|
text->setAlign(TextAlign::Center);
|
|
text->rebuild();
|
|
|
|
Vec2 size = text->getSize();
|
|
printf(" ✓ Created text object: %.1f x %.1f\n", size.x, size.y);
|
|
printf(" ✓ Text has %zu lines\n", text->getLines().size());
|
|
}
|
|
|
|
// ============================================
|
|
// 4. 加载着色器
|
|
// ============================================
|
|
printf("\n4. Loading Shaders...\n");
|
|
|
|
// 从文件加载着色器
|
|
auto shader = resources.getShader("assets/shaders/sprite.vs",
|
|
"assets/shaders/sprite.fs");
|
|
if (shader) {
|
|
printf(" ✓ Loaded shader from files\n");
|
|
} else {
|
|
printf(" ✗ Failed to load shader (files not found)\n");
|
|
}
|
|
|
|
// 使用默认着色器
|
|
auto defaultShader = resources.getDefaultShader();
|
|
if (defaultShader) {
|
|
printf(" ✓ Default shader available\n");
|
|
}
|
|
|
|
// 从源码创建着色器
|
|
const char* vsSource = R"(
|
|
#version 320 es
|
|
layout(location = 0) in vec2 aPosition;
|
|
layout(location = 1) in vec2 aTexCoord;
|
|
out vec2 vTexCoord;
|
|
void main() {
|
|
gl_Position = vec4(aPosition, 0.0, 1.0);
|
|
vTexCoord = aTexCoord;
|
|
}
|
|
)";
|
|
|
|
const char* fsSource = R"(
|
|
#version 320 es
|
|
precision mediump float;
|
|
in vec2 vTexCoord;
|
|
out vec4 fragColor;
|
|
void main() {
|
|
fragColor = vec4(vTexCoord, 0.5, 1.0);
|
|
}
|
|
)";
|
|
|
|
auto runtimeShader = resources.getShaderFromSource("runtime_shader", vsSource, fsSource);
|
|
if (runtimeShader) {
|
|
printf(" ✓ Created shader from source\n");
|
|
}
|
|
|
|
// ============================================
|
|
// 5. 创建材质
|
|
// ============================================
|
|
printf("\n5. Creating Materials...\n");
|
|
|
|
auto material = resources.createMaterial(defaultShader);
|
|
if (material) {
|
|
material->setTexture("uTexture", defaultTex);
|
|
material->setColor("uColor", Color(1.0f, 0.5f, 0.2f, 1.0f));
|
|
material->setFloat("uTime", 0.0f);
|
|
printf(" ✓ Created material with texture and uniforms\n");
|
|
}
|
|
|
|
// 注册命名材质以便后续获取
|
|
resources.registerMaterial("sprite_mat", material);
|
|
auto retrievedMat = resources.getMaterial("sprite_mat");
|
|
if (retrievedMat) {
|
|
printf(" ✓ Retrieved material by name\n");
|
|
}
|
|
|
|
// ============================================
|
|
// 6. 加载音频
|
|
// ============================================
|
|
printf("\n6. Loading Audio...\n");
|
|
|
|
// 加载音效
|
|
auto sound = resources.getAudio("assets/audio/jump.wav", AudioType::Sound);
|
|
if (sound) {
|
|
printf(" ✓ Loaded sound effect\n");
|
|
// sound->play(); // 播放一次
|
|
} else {
|
|
printf(" ✗ Failed to load sound (file not found)\n");
|
|
}
|
|
|
|
// 加载音乐
|
|
auto music = resources.getAudio("assets/audio/bgm.mp3", AudioType::Music);
|
|
if (music) {
|
|
printf(" ✓ Loaded music\n");
|
|
// music->play(-1); // 循环播放
|
|
} else {
|
|
printf(" ✗ Failed to load music (file not found)\n");
|
|
}
|
|
|
|
// ============================================
|
|
// 7. 资源统计
|
|
// ============================================
|
|
printf("\n7. Resource Statistics...\n");
|
|
|
|
auto stats = resources.getStats();
|
|
printf(" Textures: %zu\n", stats.textureCount);
|
|
printf(" Shaders: %zu\n", stats.shaderCount);
|
|
printf(" Materials: %zu\n", stats.materialCount);
|
|
printf(" Fonts: %zu\n", stats.fontCount);
|
|
printf(" Texts: %zu\n", stats.textCount);
|
|
printf(" Audio: %zu\n", stats.audioCount);
|
|
|
|
// ============================================
|
|
// 8. 默认资源
|
|
// ============================================
|
|
printf("\n8. Default Resources...\n");
|
|
|
|
auto defTex = resources.getDefaultTexture();
|
|
auto defShader = resources.getDefaultShader();
|
|
auto defMaterial = resources.getDefaultMaterial();
|
|
|
|
printf(" ✓ Default Texture: %s\n", defTex ? "available" : "null");
|
|
printf(" ✓ Default Shader: %s\n", defShader ? "available" : "null");
|
|
printf(" ✓ Default Material: %s\n", defMaterial ? "available" : "null");
|
|
|
|
printf("\n=== Resource Loading Complete ===\n");
|
|
|
|
// 运行应用(主循环)
|
|
printf("\nRunning main loop...\n");
|
|
app->run();
|
|
|
|
return 0;
|
|
}
|