Skip to content

Commit

Permalink
Qt: Check version of created OpenGL context
Browse files Browse the repository at this point in the history
  • Loading branch information
GranMinigun committed Mar 16, 2024
1 parent 6412706 commit 0fb4517
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/platform/qt/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ auto create_window(QApplication& app, int argc, char** argv) -> std::unique_ptr<
}

auto window = std::make_unique<MainWindow>(&app);
if(!window->Initialize()) {
return nullptr;
}

if(!rom.empty()) {
window->LoadROM(rom.u16string());
Expand Down Expand Up @@ -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();
}
15 changes: 13 additions & 2 deletions src/platform/qt/src/widget/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ MainWindow::MainWindow(

screen = std::make_shared<Screen>(this, config);
setCentralWidget(screen.get());
screen->windowHandle()->create();
screen->Initialize();

auto menu_bar = new QMenuBar(this);
setMenuBar(menu_bar);
Expand Down Expand Up @@ -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"));

Expand Down
1 change: 1 addition & 0 deletions src/platform/qt/src/widget/main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct MainWindow : QMainWindow {

~MainWindow();

bool Initialize();
void LoadROM(std::u16string const& path);

signals:
Expand Down
31 changes: 27 additions & 4 deletions src/platform/qt/src/widget/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/qt/src/widget/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Screen : QWidget, nba::VideoDevice {
std::shared_ptr<QtConfig> config
);

void Initialize();
bool Initialize();
void Draw(u32* buffer) final;
void ReloadConfig();
QPaintEngine* paintEngine() const override { return nullptr; }; // Silence Qt.
Expand Down

0 comments on commit 0fb4517

Please sign in to comment.