2026-02-15 17:00:39 +08:00
|
|
|
#include "hello_module.h"
|
|
|
|
|
#include <extra2d/app/application.h>
|
2026-02-16 23:14:12 +08:00
|
|
|
#include <extra2d/platform/window_module.h>
|
|
|
|
|
#include <extra2d/graphics/core/render_module.h>
|
2026-02-15 17:00:39 +08:00
|
|
|
#include <extra2d/scene/scene.h>
|
|
|
|
|
#include <extra2d/services/scene_service.h>
|
2026-02-16 23:14:12 +08:00
|
|
|
#include <iostream>
|
2026-02-15 17:00:39 +08:00
|
|
|
|
|
|
|
|
using namespace extra2d;
|
|
|
|
|
|
|
|
|
|
class HelloScene : public Scene {
|
|
|
|
|
public:
|
|
|
|
|
static Ptr<HelloScene> create() { return makeShared<HelloScene>(); }
|
|
|
|
|
|
|
|
|
|
void onEnter() override {
|
|
|
|
|
Scene::onEnter();
|
2026-02-16 23:14:12 +08:00
|
|
|
std::cout << "HelloScene entered" << std::endl;
|
2026-02-15 17:00:39 +08:00
|
|
|
setBackgroundColor(Color(0.1f, 0.1f, 0.2f, 1.0f));
|
|
|
|
|
|
2026-02-16 23:14:12 +08:00
|
|
|
auto* hello = Application::get().get<HelloModule>();
|
|
|
|
|
if (hello) {
|
|
|
|
|
std::cout << "Scene calling HelloModule from onEnter..." << std::endl;
|
|
|
|
|
hello->sayHello();
|
2026-02-15 17:00:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onUpdate(float dt) override {
|
|
|
|
|
Scene::onUpdate(dt);
|
|
|
|
|
time_ += dt;
|
|
|
|
|
|
|
|
|
|
if (time_ >= 5.0f) {
|
2026-02-16 23:14:12 +08:00
|
|
|
auto* hello = Application::get().get<HelloModule>();
|
|
|
|
|
if (hello) {
|
|
|
|
|
std::cout << "Scene calling HelloModule from onUpdate..." << std::endl;
|
|
|
|
|
hello->sayHello();
|
2026-02-15 17:00:39 +08:00
|
|
|
}
|
|
|
|
|
time_ = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
float time_ = 0.0f;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
(void)argc;
|
|
|
|
|
(void)argv;
|
|
|
|
|
|
2026-02-16 23:14:12 +08:00
|
|
|
std::cout << "=== Hello Module Example ===" << std::endl;
|
|
|
|
|
std::cout << "This example demonstrates how to create a custom module" << std::endl;
|
|
|
|
|
std::cout << "" << std::endl;
|
2026-02-15 17:00:39 +08:00
|
|
|
|
|
|
|
|
Application &app = Application::get();
|
|
|
|
|
|
2026-02-16 23:14:12 +08:00
|
|
|
// 注册模块
|
|
|
|
|
app.use<WindowModule>(WindowModule::Cfg{.w = 800, .h = 600});
|
|
|
|
|
app.use<RenderModule>();
|
|
|
|
|
app.use<HelloModule>(HelloModule::Cfg{.greeting = "Hello from custom module!", .repeatCount = 3});
|
2026-02-15 17:00:39 +08:00
|
|
|
|
2026-02-16 23:14:12 +08:00
|
|
|
if (!app.init()) {
|
|
|
|
|
std::cerr << "Failed to initialize application" << std::endl;
|
2026-02-15 17:00:39 +08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 23:14:12 +08:00
|
|
|
std::cout << "" << std::endl;
|
|
|
|
|
std::cout << "Application initialized successfully" << std::endl;
|
|
|
|
|
std::cout << "" << std::endl;
|
2026-02-15 17:00:39 +08:00
|
|
|
|
|
|
|
|
auto scene = HelloScene::create();
|
|
|
|
|
app.enterScene(scene);
|
|
|
|
|
|
2026-02-16 23:14:12 +08:00
|
|
|
std::cout << "Starting main loop..." << std::endl;
|
|
|
|
|
std::cout << "Press ESC or close window to exit" << std::endl;
|
|
|
|
|
std::cout << "" << std::endl;
|
2026-02-15 17:00:39 +08:00
|
|
|
|
|
|
|
|
app.run();
|
|
|
|
|
|
2026-02-16 23:14:12 +08:00
|
|
|
std::cout << "Application shutting down..." << std::endl;
|
2026-02-15 17:00:39 +08:00
|
|
|
app.shutdown();
|
2026-02-16 23:14:12 +08:00
|
|
|
std::cout << "Application shutdown complete" << std::endl;
|
2026-02-15 17:00:39 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|