/** * @file main.cpp * @brief MSDF 文字渲染示例 * * 演示 MSDF 字体的高质量文字渲染功能,使用 RenderService 服务。 */ #include #include using namespace extra2d; namespace { Ref 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(); app.useService(); app.useService(); app.useService(); app.useService(); app.useModule([](WindowCfg &cfg) { cfg.title = "Extra2D - MSDF Text Demo"; cfg.width = 1280; cfg.height = 720; }); app.useModule([](RenderCfg &cfg) { cfg.glMajor = 4; cfg.glMinor = 5; cfg.vsync = true; }); if (!app.init()) { E2D_ERROR(CAT_APP, "应用初始化失败!"); return -1; } auto renderService = app.service(); if (!renderService) { E2D_ERROR(CAT_APP, "无法获取 RenderService"); app.shutdown(); return -1; } auto assetService = app.service(); if (!assetService) { E2D_ERROR(CAT_APP, "无法获取 AssetService"); app.shutdown(); return -1; } auto fontHandle = assetService->load("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(); if (eventService) { eventService->on(EventType::KeyPressed, [&app](Event &e) { auto &keyEvent = std::get(e.data); if (keyEvent.key == static_cast(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; }