| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #include <QDateTime>
- #include <QDebug>
- #include <QDir>
- #include <QFile>
- #include <QGuiApplication>
- #include <QOpenGLContext>
- #include <QQmlApplicationEngine>
- #include <QQmlContext>
- #include <QQuickWindow>
- #include <QSGRendererInterface>
- #include <QSurfaceFormat>
- #include <QTextStream>
- #include "app/core/game_engine.h"
- #include "ui/gl_view.h"
- #include "ui/theme.h"
- int main(int argc, char *argv[]) {
- if (qEnvironmentVariableIsSet("WAYLAND_DISPLAY") &&
- qEnvironmentVariableIsSet("DISPLAY")) {
- qputenv("QT_QPA_PLATFORM", "xcb");
- }
- qputenv("QT_OPENGL", "desktop");
- qputenv("QSG_RHI_BACKEND", "opengl");
- #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
- QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGLRhi);
- #endif
- QSurfaceFormat fmt;
- fmt.setVersion(3, 3);
- fmt.setProfile(QSurfaceFormat::CoreProfile);
- fmt.setDepthBufferSize(24);
- fmt.setStencilBufferSize(8);
- fmt.setSamples(0);
- QSurfaceFormat::setDefaultFormat(fmt);
- QGuiApplication app(argc, argv);
- auto gameEngine = new GameEngine();
- QQmlApplicationEngine engine;
- engine.rootContext()->setContextProperty("game", gameEngine);
- engine.addImportPath("qrc:/StandardOfIron/ui/qml");
- qmlRegisterType<GLView>("StandardOfIron", 1, 0, "GLView");
- // Register Theme singleton
- qmlRegisterSingletonType<Theme>("StandardOfIron.UI", 1, 0, "Theme",
- &Theme::create);
- engine.load(QUrl(QStringLiteral("qrc:/StandardOfIron/ui/qml/Main.qml")));
- if (engine.rootObjects().isEmpty()) {
- qWarning() << "Failed to load QML file";
- return -1;
- }
- QObject *rootObj = engine.rootObjects().first();
- QQuickWindow *window = qobject_cast<QQuickWindow *>(rootObj);
- if (!window)
- window = rootObj->findChild<QQuickWindow *>();
- if (!window) {
- qWarning() << "No QQuickWindow found for OpenGL initialization.";
- return -2;
- }
- gameEngine->setWindow(window);
- QObject::connect(
- window, &QQuickWindow::sceneGraphInitialized, window, [window]() {
- if (auto *ri = window->rendererInterface()) {
- auto api = ri->graphicsApi();
- const char *name =
- api == QSGRendererInterface::OpenGLRhi ? "OpenGLRhi"
- : api == QSGRendererInterface::VulkanRhi ? "VulkanRhi"
- : api == QSGRendererInterface::Direct3D11Rhi ? "D3D11Rhi"
- : api == QSGRendererInterface::MetalRhi ? "MetalRhi"
- : api == QSGRendererInterface::Software ? "Software"
- : "Unknown";
- qInfo() << "QSG graphicsApi:" << name;
- }
- });
- QObject::connect(window, &QQuickWindow::sceneGraphError, &app,
- [&](QQuickWindow::SceneGraphError, const QString &msg) {
- qCritical()
- << "Failed to initialize OpenGL scene graph:" << msg;
- app.exit(3);
- });
- return app.exec();
- }
|