Extra2D/examples/msdf_text_demo/main.cpp

156 lines
4.2 KiB
C++
Raw Normal View History

/**
* @file main.cpp
* @brief MSDF
*
* MSDF 使 RenderService
*/
#include <extra2d/extra2d.h>
#include <extra2d/services/render_service.h>
using namespace extra2d;
namespace {
Ref<FontAsset> g_font;
} // namespace
int main(int argc, char **argv) {
E2D_INFO(CAT_APP, "============================");
E2D_INFO(CAT_APP, "MSDF Text Rendering Demo");
E2D_INFO(CAT_APP, "============================");
auto &app = Application::instance();
app.name = "MSDF Text Demo";
app.useService<ILogger, ConsoleLogger>();
app.useService<IEventService, EventService>();
app.useService<ITimerService, TimerService>();
app.useService<IAssetService, AssetService>();
app.useService<IRenderService, RenderService>();
app.useModule<WindowModule>([](WindowCfg &cfg) {
cfg.title = "Extra2D - MSDF Text Demo";
cfg.width = 1280;
cfg.height = 720;
});
app.useModule<RenderModule>([](RenderCfg &cfg) {
cfg.glMajor = 4;
cfg.glMinor = 5;
cfg.vsync = true;
});
if (!app.init()) {
E2D_ERROR(CAT_APP, "应用初始化失败!");
return -1;
}
auto renderService = app.service<IRenderService>();
if (!renderService) {
E2D_ERROR(CAT_APP, "无法获取 RenderService");
app.shutdown();
return -1;
}
auto assetService = app.service<IAssetService>();
if (!assetService) {
E2D_ERROR(CAT_APP, "无法获取 AssetService");
app.shutdown();
return -1;
}
auto fontHandle = assetService->load<FontAsset>("assets/simhei.msdf.png");
g_font = fontHandle.get();
if (!g_font || !g_font->loaded()) {
E2D_ERROR(CAT_APP, "无法加载字体");
app.shutdown();
return -1;
}
E2D_INFO(CAT_APP, "字体加载成功,字体大小: {}", g_font->fontSize());
auto eventService = app.service<IEventService>();
if (eventService) {
eventService->on(EventType::KeyPressed, [&app](Event &e) {
auto &keyEvent = std::get<KeyEvent>(e.data);
if (keyEvent.key == static_cast<i32>(Key::Escape)) {
E2D_INFO(CAT_INPUT, "ESC 键按下,退出应用");
app.quit();
}
});
}
renderService->onRender([](IRenderService *render, f32 dt) {
if (!g_font) {
return;
}
float y = 60.0f;
::extra2d::TextConfig config;
config.fontSize = 48.0f;
config.color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
render->drawText(g_font.get(), U"MSDF Text Rendering Demo", 20.0f, y,
config);
y += 80.0f;
config.fontSize = 32.0f;
render->drawText(g_font.get(), U"Extra2D Engine - 2D Game Engine", 20.0f, y,
config);
y += 60.0f;
config.fontSize = 24.0f;
config.color = glm::vec4(0.7f, 0.9f, 1.0f, 1.0f);
render->drawText(g_font.get(), U"High quality text rendering with MSDF",
20.0f, y, config);
y += 45.0f;
config.fontSize = 20.0f;
config.color = glm::vec4(1.0f, 0.8f, 0.4f, 1.0f);
render->drawText(g_font.get(),
U"Multi-channel Signed Distance Field technology", 20.0f,
y, config);
y += 35.0f;
config.color = glm::vec4(0.6f, 1.0f, 0.6f, 1.0f);
render->drawText(g_font.get(),
U"Provides high quality, scalable vector font rendering",
20.0f, y, config);
y += 50.0f;
config.fontSize = 28.0f;
config.color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
render->drawText(g_font.get(), U"Character Test:", 20.0f, y, config);
y += 45.0f;
config.fontSize = 22.0f;
config.color = glm::vec4(0.9f, 0.9f, 0.9f, 1.0f);
render->drawText(g_font.get(), U"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20.0f, y,
config);
y += 35.0f;
render->drawText(g_font.get(), U"abcdefghijklmnopqrstuvwxyz", 20.0f, y,
config);
y += 35.0f;
render->drawText(g_font.get(), U"0123456789!@#$%^&*()[]{}<>", 20.0f, y,
config);
static int frameCounter = 0;
if (++frameCounter % 60 == 0) {
auto stats = render->stats();
E2D_DEBUG(CAT_RENDER, "FPS: {:.1f}, Draw Calls: {}", stats.fps,
stats.drawCalls);
}
});
E2D_INFO(CAT_APP, "开始主循环...");
app.run();
E2D_INFO(CAT_APP, "应用结束");
app.shutdown();
return 0;
}