Extra2D/examples/text_rendering/main.cpp

187 lines
5.3 KiB
C++

/**
* @file main.cpp
* @brief Extra2D 文字渲染示例
*
* 演示如何使用 RenderModule 渲染文字
*/
#include <extra2d/extra2d.h>
#include <iostream>
using namespace extra2d;
class TextRenderingScene : public Scene {
public:
void onEnter() override {
auto &fontManager = FontManager::getInstance();
auto *renderMod = Application::get().get<RenderModule>();
if (!renderMod) {
std::cerr << "RenderModule not available!" << std::endl;
return;
}
auto *renderer = renderMod->getRenderer();
if (!renderer) {
std::cerr << "Renderer not available!" << std::endl;
return;
}
auto *context = renderer->getContext();
if (!context) {
std::cerr << "RenderContext not available!" << std::endl;
return;
}
if (!fontManager.isInitialized()) {
fontManager.init(context->getDevice());
}
fontHandle_ = fontManager.load("assets/fonts/fonts.ttf",
{.fontSize = 26, .useSDF = true});
if (fontHandle_) {
fontAtlas_ = fontManager.getAtlas(fontHandle_, 26);
if (fontAtlas_) {
std::cout << "Font loaded successfully!" << std::endl;
std::cout << " Font size: " << fontAtlas_->getFontSize() << std::endl;
std::cout << " Line height: " << fontAtlas_->getLineHeight()
<< std::endl;
std::cout << " Ascent: " << fontAtlas_->getAscent() << std::endl;
std::cout << " Descent: " << fontAtlas_->getDescent() << std::endl;
}
} else {
std::cerr << "Failed to load font from assets/fonts/fonts.ttf"
<< std::endl;
}
}
void onExit() override {
fontAtlas_.reset();
fontHandle_.reset();
}
void onRender(RenderBackend &renderer) override {
Scene::onRender(renderer);
if (!fontAtlas_) {
return;
}
float y = 100.0f;
float x = 100.0f;
float fontSize = 26.0f;
renderText(renderer, "Extra2D Text Rendering Demo", x, y, fontSize,
Color(1.0f, 0.8f, 0.2f, 1.0f));
y += fontAtlas_->getLineHeight() * 2;
renderText(renderer, "Red Text", x, y, fontSize,
Color(1.0f, 0.2f, 0.2f, 1.0f));
y += fontAtlas_->getLineHeight();
renderText(renderer, "Green Text", x, y, fontSize,
Color(0.2f, 1.0f, 0.2f, 1.0f));
y += fontAtlas_->getLineHeight();
renderText(renderer, "Blue Text", x, y, fontSize,
Color(0.2f, 0.2f, 1.0f, 1.0f));
y += fontAtlas_->getLineHeight() * 2;
renderText(renderer, "This is a multi-line text example.", x, y, fontSize,
Color(1.0f, 1.0f, 1.0f, 1.0f));
y += fontAtlas_->getLineHeight();
renderText(renderer, "You can render text with different colors", x, y,
fontSize, Color(0.8f, 0.8f, 0.8f, 1.0f));
y += fontAtlas_->getLineHeight();
renderText(renderer, "and styles using FontAtlas.", x, y, fontSize,
Color(0.6f, 0.6f, 0.6f, 1.0f));
y += fontAtlas_->getLineHeight() * 2;
renderText(renderer, "Semi-transparent text (50% opacity)", x, y, fontSize,
Color(1.0f, 1.0f, 1.0f, 0.5f));
y += fontAtlas_->getLineHeight() * 2;
renderText(renderer, "Press ESC to exit", x, y, fontSize,
Color(0.5f, 0.5f, 0.5f, 1.0f));
}
private:
/**
* @brief 渲染文本
*/
void renderText(RenderBackend &renderer, const std::string &text, float x,
float y, float fontSize, const Color &color) {
if (!fontAtlas_) {
return;
}
renderer.drawText(text, Vec2(x, y), fontSize, color);
}
FontHandle fontHandle_;
Ptr<FontAtlas> fontAtlas_;
};
int main(int argc, char *argv[]) {
Application &app = Application::get();
app.use<WindowModule>([](auto &cfg) {
cfg.w = 1280;
cfg.h = 720;
cfg.title = "Extra2D 文字显示示例";
cfg.priority = 0;
cfg.backend = "sdl2";
});
app.use<RenderModule>([](auto &cfg) { cfg.api = rhi::GraphicsAPI::OpenGL; });
app.use<InputModule>([](auto &cfg) { cfg.priority = 20; });
if (!app.init()) {
E2D_LOG_INFO("Failed to initialize application!");
return -1;
}
auto win = app.window();
if (win) {
E2D_LOG_INFO("Window :{} x {}", win->width(), win->height());
}
auto eventService = ServiceLocator::instance().getService<IEventService>();
if (eventService) {
eventService->addListener(EventType::KeyPressed, [](Event &e) {
auto &keyEvent = std::get<KeyEvent>(e.data);
if (keyEvent.keyCode == static_cast<int>(Key::Escape)) {
e.handled = true;
Application::get().quit();
}
});
}
auto scene = makeShared<TextRenderingScene>();
scene->setBackgroundColor(Color(0.1f, 0.1f, 0.15f, 1.0f));
if (win) {
scene->setViewportSize(static_cast<float>(win->width()),
static_cast<float>(win->height()));
}
auto cameraService = ServiceLocator::instance().getService<ICameraService>();
if (cameraService && win) {
cameraService->setViewportConfig([&](auto &cfg) {
cfg.designWidth = static_cast<float>(win->width());
cfg.designHeight = static_cast<float>(win->height());
cfg.scaleMode = ViewportScaleMode::Letterbox;
});
cameraService->updateViewport(win->width(), win->height());
cameraService->applyViewportAdapter();
}
app.enterScene(scene);
app.run();
app.shutdown();
return 0;
}