Extra2D/examples/hello_world/main.cpp

59 lines
1.6 KiB
C++
Raw Normal View History

2026-02-11 19:40:26 +08:00
#include <extra2d/extra2d.h>
using namespace extra2d;
int main(int argc, char **argv) {
E2D_INFO(CAT_APP, "========================");
E2D_INFO(CAT_APP, "Extra2D Hello World Demo");
E2D_INFO(CAT_APP, "========================");
auto &app = Application::instance();
app.name = "Hello World";
// 注册服务和模块顺序无关Lifecycle 会自动按依赖拓扑排序)
app.useService<ILogger, ConsoleLogger>();
app.useService<IEventService, EventService>();
app.useService<ITimerService, TimerService>();
app.useModule<WindowModule>([](WindowCfg &cfg) {
cfg.title = "Extra2D - Hello World";
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 eventService = Lifecycle::instance().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();
}
});
eventService->on(EventType::GamepadButtonPressed, [&app](Event &e) {
auto &btnEvent = std::get<GamepadButtonEvent>(e.data);
if (btnEvent.button == static_cast<i32>(Gamepad::Start)) {
E2D_INFO(CAT_INPUT, "START 按钮按下,退出应用");
app.quit();
}
});
}
E2D_INFO(CAT_APP, "开始主循环...");
app.run();
E2D_INFO(CAT_APP, "应用结束");
app.shutdown();
return 0;
}