Extra2D/examples/hello_world/main.cpp

125 lines
3.6 KiB
C++
Raw Normal View History

#include <extra2d.h>
2026-02-11 19:40:26 +08:00
using namespace extra2d;
// ============================================================================
// Hello World 场景
// ============================================================================
/**
* @brief Hello World
* "Hello World"
*/
class HelloWorldScene : public Scene {
public:
/**
* @brief
*/
void onEnter() override {
E2D_LOG_INFO("HelloWorldScene::onEnter - 进入场景");
// 设置背景颜色为深蓝色
setBackgroundColor(Color(0.1f, 0.1f, 0.3f, 1.0f));
// 加载字体(支持多种字体后备)
auto &resources = Application::instance().resources();
font_ = resources.loadFont("assets/font.ttf", 48, true);
if (!font_) {
E2D_LOG_ERROR("字体加载失败,文字渲染将不可用!");
return;
}
// 创建 "你好世界" 文本组件 - 使用节点位置
2026-02-11 19:40:26 +08:00
auto text1 = Text::create("你好世界", font_);
text1->setPosition(640.0f, 360.0f); // 屏幕中心
text1->setAnchor(0.5f, 0.5f); // 中心锚点,让文字中心对准位置
text1->setTextColor(Color(1.0f, 1.0f, 1.0f, 1.0f));
2026-02-11 19:40:26 +08:00
addChild(text1);
// 创建提示文本组件 - 固定在屏幕底部
2026-02-11 19:40:26 +08:00
auto text2 = Text::create("退出按键START 按钮)", font_);
text2->setPosition(640.0f, 650.0f); // 屏幕底部
text2->setAnchor(0.5f, 0.5f);
text2->setTextColor(Color(1.0f, 1.0f, 0.0f, 1.0f));
2026-02-11 19:40:26 +08:00
addChild(text2);
// 创建带偏移的文本 - 使用相对位置
auto text3 = Text::create("偏移文本", font_);
text3->setPosition(50.0f, 50.0f); // 左上角偏移
text3->setAnchor(0.0f, 0.0f); // 左上角锚点,文字从指定位置开始显示
text3->setTextColor(Color(0.0f, 1.0f, 1.0f, 1.0f));
2026-02-11 19:40:26 +08:00
addChild(text3);
// 创建世界空间文本 - 随相机移动(默认行为)
auto text4 = Text::create("世界空间文本", font_);
text4->setPosition(100.0f, 100.0f); // 世界坐标
text4->setAnchor(0.0f, 0.0f); // 左上角锚点,文字从指定位置开始显示
text4->setTextColor(Color(1.0f, 0.5f, 0.5f, 1.0f));
2026-02-11 19:40:26 +08:00
addChild(text4);
}
/**
* @brief
* @param dt
*/
void onUpdate(float dt) override {
Scene::onUpdate(dt);
// 检查退出按键
auto &input = Application::instance().input();
// 使用手柄 START 按钮退出 (GamepadButton::Start)
if (input.isButtonPressed(GamepadButton::Start)) {
E2D_LOG_INFO("退出应用 (START 按钮)");
Application::instance().quit();
}
}
private:
Ptr<FontAtlas> font_; // 字体图集
};
// ============================================================================
// 程序入口
// ============================================================================
int main(int argc, char **argv) {
2026-02-11 19:40:26 +08:00
// 初始化日志系统
Logger::init();
Logger::setLevel(LogLevel::Debug);
E2D_LOG_INFO("========================");
E2D_LOG_INFO("Easy2D Hello World Demo");
E2D_LOG_INFO("========================");
// 获取应用实例
auto &app = Application::instance();
// 配置应用
AppConfig config;
config.title = "Easy2D - Hello World";
config.width = 1280;
config.height = 720;
config.vsync = true;
config.fpsLimit = 60;
// 初始化应用
if (!app.init(config)) {
E2D_LOG_ERROR("应用初始化失败!");
return -1;
}
// 进入 Hello World 场景
app.enterScene(makePtr<HelloWorldScene>());
E2D_LOG_INFO("开始主循环...");
// 运行应用
app.run();
E2D_LOG_INFO("应用结束");
return 0;
}