From 0fb4517a398c8667319b0440225e01cc51c46bf9 Mon Sep 17 00:00:00 2001 From: GranMinigun <11149380+GranMinigun@users.noreply.github.com> Date: Thu, 14 Mar 2024 18:52:58 +0500 Subject: [PATCH] Qt: Check version of created OpenGL context --- src/platform/qt/src/main.cpp | 6 +++++ src/platform/qt/src/widget/main_window.cpp | 15 +++++++++-- src/platform/qt/src/widget/main_window.hpp | 1 + src/platform/qt/src/widget/screen.cpp | 31 +++++++++++++++++++--- src/platform/qt/src/widget/screen.hpp | 2 +- 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/platform/qt/src/main.cpp b/src/platform/qt/src/main.cpp index 9a4a6b96..e85cc979 100644 --- a/src/platform/qt/src/main.cpp +++ b/src/platform/qt/src/main.cpp @@ -48,6 +48,9 @@ auto create_window(QApplication& app, int argc, char** argv) -> std::unique_ptr< } auto window = std::make_unique(&app); + if(!window->Initialize()) { + return nullptr; + } if(!rom.empty()) { window->LoadROM(rom.u16string()); @@ -104,6 +107,9 @@ int main(int argc, char** argv) { QGuiApplication::setDesktopFileName("io.github.nba_emuNanoBoyAdvance"); auto window = create_window(app, argc, argv); + if(!window) { + return EXIT_FAILURE; + } return app.exec(); } diff --git a/src/platform/qt/src/widget/main_window.cpp b/src/platform/qt/src/widget/main_window.cpp index a13d9d92..222e3c17 100644 --- a/src/platform/qt/src/widget/main_window.cpp +++ b/src/platform/qt/src/widget/main_window.cpp @@ -45,8 +45,6 @@ MainWindow::MainWindow( screen = std::make_shared(this, config); setCentralWidget(screen.get()); - screen->windowHandle()->create(); - screen->Initialize(); auto menu_bar = new QMenuBar(this); setMenuBar(menu_bar); @@ -93,6 +91,19 @@ MainWindow::~MainWindow() { delete controller_manager; } +bool MainWindow::Initialize() { + screen->windowHandle()->create(); + if(!screen->Initialize()) { + QMessageBox::critical(this, QApplication::instance()->applicationName(), + tr("Failed to initialize graphics subsystem.\n\n" + "Make sure that your hardware supports OpenGL 3.3 or later, " + "and you have the latest driver version installed.")); + return false; + } + + return true; +} + void MainWindow::CreateFileMenu() { auto file_menu = menuBar()->addMenu(tr("File")); diff --git a/src/platform/qt/src/widget/main_window.hpp b/src/platform/qt/src/widget/main_window.hpp index a5ddead8..32c0be86 100644 --- a/src/platform/qt/src/widget/main_window.hpp +++ b/src/platform/qt/src/widget/main_window.hpp @@ -39,6 +39,7 @@ struct MainWindow : QMainWindow { ~MainWindow(); + bool Initialize(); void LoadROM(std::u16string const& path); signals: diff --git a/src/platform/qt/src/widget/screen.cpp b/src/platform/qt/src/widget/screen.cpp index 867c3155..8c5a8b45 100644 --- a/src/platform/qt/src/widget/screen.cpp +++ b/src/platform/qt/src/widget/screen.cpp @@ -28,15 +28,38 @@ static auto get_proc_address(const char* proc_name) { return QOpenGLContext::currentContext()->getProcAddress(proc_name); } -void Screen::Initialize() { +bool Screen::Initialize() { context = new QOpenGLContext(); - context->create(); - context->makeCurrent(this->windowHandle()); + if(!context->create()) { + delete context; + context = nullptr; + return false; + } + if(context->format().majorVersion() < 3 || + context->format().majorVersion() == 3 && context->format().minorVersion() < 3) { + delete context; + context = nullptr; + return false; + } + + if(!context->makeCurrent(this->windowHandle())) { + delete context; + context = nullptr; + return false; + } + + if(!gladLoadGL(get_proc_address)) { + delete context; + context = nullptr; + return false; + } - gladLoadGL(get_proc_address); ogl_video_device.Initialize(); + UpdateViewport(); context->doneCurrent(); + + return true; } void Screen::Draw(u32* buffer) { diff --git a/src/platform/qt/src/widget/screen.hpp b/src/platform/qt/src/widget/screen.hpp index ed16feaa..b69bfb74 100644 --- a/src/platform/qt/src/widget/screen.hpp +++ b/src/platform/qt/src/widget/screen.hpp @@ -19,7 +19,7 @@ struct Screen : QWidget, nba::VideoDevice { std::shared_ptr config ); - void Initialize(); + bool Initialize(); void Draw(u32* buffer) final; void ReloadConfig(); QPaintEngine* paintEngine() const override { return nullptr; }; // Silence Qt.