Extra2D/examples/hello_module/main.cpp

83 lines
2.2 KiB
C++
Raw Normal View History

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