/** * @file main.cpp * @brief Extra2D 场景图测试示例 */ #include #include #include #include #include #include #include #include using namespace extra2d; void createSceneGraph(Scene *scene) { float width = scene->getWidth(); float height = scene->getHeight(); auto root = makeShared(); root->setName("Root"); root->setPos(width / 2, height / 2); scene->addChild(root); auto parent1 = makeShared(); parent1->setName("Parent1"); parent1->setPos(-200, 0); root->addChild(parent1); auto rect1 = ShapeNode::createFilledRect(Rect(-50, -50, 100, 100), Color(1.0f, 0.4f, 0.4f, 1.0f)); rect1->setName("RedRect"); parent1->addChild(rect1); auto child1 = makeShared(); child1->setName("Child1"); child1->setPos(80, 0); child1->setRotation(45); child1->setScale(0.5f); parent1->addChild(child1); auto smallRect = ShapeNode::createFilledRect(Rect(-30, -30, 60, 60), Color(1.0f, 0.8f, 0.4f, 1.0f)); smallRect->setName("OrangeRect"); child1->addChild(smallRect); auto parent2 = makeShared(); parent2->setName("Parent2"); parent2->setPos(200, 0); root->addChild(parent2); auto circle1 = ShapeNode::createFilledCircle(Vec2(0, 0), 60, Color(0.4f, 0.4f, 1.0f, 1.0f)); circle1->setName("BlueCircle"); parent2->addChild(circle1); auto child2 = makeShared(); child2->setName("Child2"); child2->setPos(0, 100); parent2->addChild(child2); auto triangle = ShapeNode::createFilledTriangle( Vec2(0, -40), Vec2(-35, 30), Vec2(35, 30), Color(0.4f, 1.0f, 0.4f, 1.0f)); triangle->setName("GreenTriangle"); child2->addChild(triangle); auto line = ShapeNode::createLine(Vec2(-300, -200), Vec2(300, -200), Color(1.0f, 1.0f, 1.0f, 1.0f), 2.0f); line->setName("BottomLine"); root->addChild(line); auto polygon = ShapeNode::createFilledPolygon( {Vec2(0, -50), Vec2(50, 0), Vec2(30, 50), Vec2(-30, 50), Vec2(-50, 0)}, Color(1.0f, 0.4f, 1.0f, 1.0f)); polygon->setName("PurplePolygon"); polygon->setPos(0, -150); root->addChild(polygon); std::cout << "\n=== Scene Graph Structure ===" << std::endl; std::cout << "Scene (root)" << std::endl; std::cout << " └── Root (center)" << std::endl; std::cout << " ├── Parent1 (left)" << std::endl; std::cout << " │ ├── RedRect (100x100)" << std::endl; std::cout << " │ └── Child1 (rotated 45, scaled 0.5)" << std::endl; std::cout << " │ └── OrangeRect (60x60)" << std::endl; std::cout << " ├── Parent2 (right)" << std::endl; std::cout << " │ ├── BlueCircle (radius 60)" << std::endl; std::cout << " │ └── Child2 (below)" << std::endl; std::cout << " │ └── GreenTriangle" << std::endl; std::cout << " ├── BottomLine" << std::endl; std::cout << " └── PurplePolygon (pentagon)" << std::endl; std::cout << "=============================\n" << std::endl; } int main(int argc, char *argv[]) { (void)argc; (void)argv; std::cout << "Extra2D Scene Graph Demo - Starting..." << std::endl; Application &app = Application::get(); // 注册模块(按优先级顺序) WindowModule::Cfg winCfg; winCfg.w = 1280; winCfg.h = 720; winCfg.priority = 0; winCfg.backend = "glfw"; app.use(winCfg); RenderModule::Cfg renderCfg; renderCfg.priority = 10; app.use(renderCfg); InputModule::Cfg inputCfg; inputCfg.priority = 20; app.use(inputCfg); std::cout << "Initializing application..." << std::endl; if (!app.init()) { std::cerr << "Failed to initialize application!" << std::endl; return -1; } std::cout << "Application initialized successfully!" << std::endl; auto *win = app.window(); if (win) { std::cout << "Window: " << win->width() << "x" << win->height() << std::endl; } auto eventService = ServiceLocator::instance().getService(); if (eventService) { eventService->addListener(EventType::KeyPressed, [](Event &e) { auto &keyEvent = std::get(e.data); if (keyEvent.keyCode == static_cast(Key::Escape)) { e.handled = true; Application::get().quit(); } }); eventService->addListener(EventType::MouseButtonPressed, [](Event &e) { auto &mouseEvent = std::get(e.data); std::cout << "[Click] Button " << mouseEvent.button << " at (" << mouseEvent.position.x << ", " << mouseEvent.position.y << ")" << std::endl; }); } auto scene = Scene::create(); scene->setBackgroundColor(Color(0.12f, 0.12f, 0.16f, 1.0f)); if (win) { scene->setViewportSize(static_cast(win->width()), static_cast(win->height())); } auto cameraService = ServiceLocator::instance().getService(); if (cameraService && win) { ViewportConfig vpConfig; vpConfig.logicWidth = static_cast(win->width()); vpConfig.logicHeight = static_cast(win->height()); vpConfig.mode = ViewportMode::AspectRatio; cameraService->setViewportConfig(vpConfig); cameraService->updateViewport(win->width(), win->height()); cameraService->applyViewportAdapter(); } createSceneGraph(scene.get()); app.enterScene(scene); std::cout << "\nControls:" << std::endl; std::cout << " ESC - Exit" << std::endl; std::cout << " Mouse Click - Print position" << std::endl; std::cout << "\nRunning main loop...\n" << std::endl; app.run(); std::cout << "Shutting down..." << std::endl; app.shutdown(); std::cout << "Goodbye!" << std::endl; return 0; }