Extra2D/examples/hello_module/main.cpp

62 lines
1.4 KiB
C++

#include "hello_module.h"
#include <extra2d/extra2d.h>
using namespace extra2d;
/**
* @brief 自定义场景类
*
* 展示如何在场景中使用自定义模块
*/
class HelloScene : public Scene {
public:
static Ptr<HelloScene> create() { return makeShared<HelloScene>(); }
void onEnter() override {
Scene::onEnter();
addListener(EventType::KeyPress, [](Event &e) {
auto &keyEvent = std::get<KeyEvent>(e.data);
auto app = Application::get().getModule<HelloModule>();
E2D_LOG_INFO("Module {}", app->getName());
if (keyEvent.scancode == static_cast<int>(Key::Escape)) {
e.handled = true;
E2D_LOG_INFO("ESC pressed, exiting...");
Application::get().quit();
}
});
E2D_LOG_INFO("HelloScene entered");
setBackgroundColor(Color(0.1f, 0.1f, 0.2f, 1.0f));
}
private:
};
/**
* @brief 应用程序入口
*
* 静态链接时模块直接编译到可执行文件,自动注册,无需任何额外代码!
*/
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Application &app = Application::get();
AppConfig appConfig;
appConfig.appName = "HelloModule Example";
appConfig.appVersion = "1.0.0";
if (!app.init(appConfig)) {
E2D_LOG_ERROR("Failed to initialize application");
return 1;
}
auto scene = HelloScene::create();
app.enterScene(scene);
app.run();
return 0;
}