main.cpp 2.9 KB

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