Extra2D/examples/text_rendering/main.cpp

173 lines
4.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file main.cpp
* @brief Extra2D 文字渲染示例
*
* 演示如何使用 Renderer 抽象接口渲染文字
* 此示例不依赖任何特定渲染后端(如 OpenGL
*/
#include <extra2d/extra2d.h>
#include <iostream>
using namespace extra2d;
class TextRenderingScene : public Scene {
public:
void onEnter() override {
// 加载字体
// 注意:请确保有字体文件在 assets/fonts/ 目录下
// 如果没有,可以使用系统字体路径
if (!renderer_) {
std::cerr << "Renderer not available!" << std::endl;
return;
}
try {
font_ = renderer_->createFontAtlas("assets/fonts/fonts.ttf", 26, true);
} catch (...) {
std::cerr << "Failed to load font from assets/fonts/fonts.ttf"
<< std::endl;
}
if (font_) {
std::cout << "Font loaded successfully!" << std::endl;
std::cout << " Font size: " << font_->getFontSize() << std::endl;
std::cout << " Line height: " << font_->getLineHeight() << std::endl;
std::cout << " Ascent: " << font_->getAscent() << std::endl;
std::cout << " Descent: " << font_->getDescent() << std::endl;
}
}
void onExit() override { font_.reset(); }
void onRender(Renderer &renderer) override {
Scene::onRender(renderer);
if (!font_) {
return;
}
float y = 100.0f;
float x = 100.0f;
renderText(renderer, "Extra2D Text Rendering Demo", x, y,
Color(1.0f, 0.8f, 0.2f, 1.0f));
y += font_->getLineHeight() * 2;
// 渲染不同颜色的文字
renderText(renderer, "Red Text", x, y, Color(1.0f, 0.2f, 0.2f, 1.0f));
y += font_->getLineHeight();
renderText(renderer, "Green Text", x, y, Color(0.2f, 1.0f, 0.2f, 1.0f));
y += font_->getLineHeight();
renderText(renderer, "Blue Text", x, y, Color(0.2f, 0.2f, 1.0f, 1.0f));
y += font_->getLineHeight() * 2;
// 渲染多行文字
renderText(renderer, "This is a multi-line text example.", x, y,
Color(1.0f, 1.0f, 1.0f, 1.0f));
y += font_->getLineHeight();
renderText(renderer, "You can render text with different colors", x, y,
Color(0.8f, 0.8f, 0.8f, 1.0f));
y += font_->getLineHeight();
renderText(renderer, "and styles using FontAtlas.", x, y,
Color(0.6f, 0.6f, 0.6f, 1.0f));
y += font_->getLineHeight() * 2;
// 渲染半透明文字
renderText(renderer, "Semi-transparent text (50% opacity)", x, y,
Color(1.0f, 1.0f, 1.0f, 0.5f));
y += font_->getLineHeight() * 2;
// 渲染操作提示
renderText(renderer, "Press ESC to exit", x, y,
Color(0.5f, 0.5f, 0.5f, 1.0f));
}
void setRenderer(Renderer *renderer) { renderer_ = renderer; }
private:
void renderText(Renderer &renderer, const std::string &text, float x, float y,
const Color &color) {
if (!font_) {
return;
}
// 使用 Renderer 的 drawText 方法
renderer.drawText(*font_, text, Vec2(x, y), color);
}
Ptr<FontAtlas> font_;
Renderer *renderer_ = nullptr;
};
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;
});
app.use<RenderModule>([](auto &cfg) { cfg.priority = 10; });
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();
}
});
}
// 获取渲染器
Renderer *renderer = app.renderer();
// 创建并配置场景
auto scene = makeShared<TextRenderingScene>();
scene->setRenderer(renderer);
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.logicWidth = static_cast<float>(win->width());
cfg.logicHeight = static_cast<float>(win->height());
cfg.mode = ViewportMode::AspectRatio;
});
cameraService->updateViewport(win->width(), win->height());
cameraService->applyViewportAdapter();
}
app.enterScene(scene);
app.run();
app.shutdown();
return 0;
}