main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <QDateTime>
  2. #include <QDebug>
  3. #include <QDir>
  4. #include <QFile>
  5. #include <QGuiApplication>
  6. #include <QOpenGLContext>
  7. #include <QQmlApplicationEngine>
  8. #include <QQmlContext>
  9. #include <QQuickWindow>
  10. #include <QSGRendererInterface>
  11. #include <QSurfaceFormat>
  12. #include <QTextStream>
  13. #include "app/core/game_engine.h"
  14. #include "app/core/language_manager.h"
  15. #include "ui/gl_view.h"
  16. #include "ui/theme.h"
  17. int main(int argc, char *argv[]) {
  18. if (qEnvironmentVariableIsSet("WAYLAND_DISPLAY") &&
  19. qEnvironmentVariableIsSet("DISPLAY")) {
  20. qputenv("QT_QPA_PLATFORM", "xcb");
  21. }
  22. qputenv("QT_OPENGL", "desktop");
  23. qputenv("QSG_RHI_BACKEND", "opengl");
  24. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  25. QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGLRhi);
  26. #endif
  27. QSurfaceFormat fmt;
  28. fmt.setVersion(3, 3);
  29. fmt.setProfile(QSurfaceFormat::CoreProfile);
  30. fmt.setDepthBufferSize(24);
  31. fmt.setStencilBufferSize(8);
  32. fmt.setSamples(0);
  33. QSurfaceFormat::setDefaultFormat(fmt);
  34. QGuiApplication app(argc, argv);
  35. auto languageManager = new LanguageManager();
  36. auto gameEngine = new GameEngine();
  37. QQmlApplicationEngine engine;
  38. engine.rootContext()->setContextProperty("languageManager", languageManager);
  39. engine.rootContext()->setContextProperty("game", gameEngine);
  40. engine.addImportPath("qrc:/StandardOfIron/ui/qml");
  41. qmlRegisterType<GLView>("StandardOfIron", 1, 0, "GLView");
  42. // Register Theme singleton
  43. qmlRegisterSingletonType<Theme>("StandardOfIron.UI", 1, 0, "Theme",
  44. &Theme::create);
  45. engine.load(QUrl(QStringLiteral("qrc:/StandardOfIron/ui/qml/Main.qml")));
  46. if (engine.rootObjects().isEmpty()) {
  47. qWarning() << "Failed to load QML file";
  48. return -1;
  49. }
  50. QObject *rootObj = engine.rootObjects().first();
  51. QQuickWindow *window = qobject_cast<QQuickWindow *>(rootObj);
  52. if (!window)
  53. window = rootObj->findChild<QQuickWindow *>();
  54. if (!window) {
  55. qWarning() << "No QQuickWindow found for OpenGL initialization.";
  56. return -2;
  57. }
  58. gameEngine->setWindow(window);
  59. QObject::connect(
  60. window, &QQuickWindow::sceneGraphInitialized, window, [window]() {
  61. if (auto *ri = window->rendererInterface()) {
  62. auto api = ri->graphicsApi();
  63. const char *name =
  64. api == QSGRendererInterface::OpenGLRhi ? "OpenGLRhi"
  65. : api == QSGRendererInterface::VulkanRhi ? "VulkanRhi"
  66. : api == QSGRendererInterface::Direct3D11Rhi ? "D3D11Rhi"
  67. : api == QSGRendererInterface::MetalRhi ? "MetalRhi"
  68. : api == QSGRendererInterface::Software ? "Software"
  69. : "Unknown";
  70. qInfo() << "QSG graphicsApi:" << name;
  71. }
  72. });
  73. QObject::connect(window, &QQuickWindow::sceneGraphError, &app,
  74. [&](QQuickWindow::SceneGraphError, const QString &msg) {
  75. qCritical()
  76. << "Failed to initialize OpenGL scene graph:" << msg;
  77. app.exit(3);
  78. });
  79. return app.exec();
  80. }