From bea15c97cd9b8279a7e7842ab2ad0d33e260df76 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Thu, 2 Jan 2020 19:14:16 +1000 Subject: [PATCH] Qt: ImGui support --- src/duckstation-qt/main.cpp | 11 +++++++---- src/duckstation-qt/opengldisplaywindow.cpp | 7 ++++--- src/duckstation-qt/qtdisplaywindow.cpp | 22 ++++++++++++++++++++++ src/duckstation-qt/qtdisplaywindow.h | 12 ++++++++++-- src/duckstation-qt/qthostinterface.cpp | 18 +++++++++++++----- src/duckstation-qt/qthostinterface.h | 1 + 6 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/duckstation-qt/main.cpp b/src/duckstation-qt/main.cpp index de0189f1b..f65198d81 100644 --- a/src/duckstation-qt/main.cpp +++ b/src/duckstation-qt/main.cpp @@ -7,10 +7,13 @@ static void InitLogging() { // set log flags - // g_pLog->SetConsoleOutputParams(true); - g_pLog->SetConsoleOutputParams(true, nullptr, LOGLEVEL_PROFILE); - g_pLog->SetFilterLevel(LOGLEVEL_PROFILE); - // g_pLog->SetDebugOutputParams(true); +#ifdef Y_BUILD_CONFIG_DEBUG + g_pLog->SetConsoleOutputParams(true, nullptr, LOGLEVEL_DEBUG); + g_pLog->SetFilterLevel(LOGLEVEL_DEBUG); +#else + g_pLog->SetConsoleOutputParams(true, nullptr, LOGLEVEL_INFO); + g_pLog->SetFilterLevel(LOGLEVEL_INFO); +#endif } int main(int argc, char* argv[]) diff --git a/src/duckstation-qt/opengldisplaywindow.cpp b/src/duckstation-qt/opengldisplaywindow.cpp index c6f3c3bbc..68dd92610 100644 --- a/src/duckstation-qt/opengldisplaywindow.cpp +++ b/src/duckstation-qt/opengldisplaywindow.cpp @@ -371,6 +371,7 @@ bool OpenGLDisplayWindow::createImGuiContext() return false; ImGui_ImplOpenGL3_NewFrame(); + ImGui::NewFrame(); return true; } @@ -464,13 +465,13 @@ void OpenGLDisplayWindow::Render() renderDisplay(); - ImDrawData* draw_data = ImGui::GetDrawData(); - if (draw_data) - ImGui_ImplOpenGL3_RenderDrawData(draw_data); + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); m_gl_context->makeCurrent(this); m_gl_context->swapBuffers(this); + ImGui::NewFrame(); ImGui_ImplOpenGL3_NewFrame(); GL::Program::ResetLastProgram(); diff --git a/src/duckstation-qt/qtdisplaywindow.cpp b/src/duckstation-qt/qtdisplaywindow.cpp index 204c0dbd9..d7a72f0c8 100644 --- a/src/duckstation-qt/qtdisplaywindow.cpp +++ b/src/duckstation-qt/qtdisplaywindow.cpp @@ -37,6 +37,11 @@ void QtDisplayWindow::destroyDeviceContext() bool QtDisplayWindow::createImGuiContext() { ImGui::CreateContext(); + + auto& io = ImGui::GetIO(); + io.DisplaySize.x = static_cast(width()); + io.DisplaySize.y = static_cast(height()); + return true; } @@ -54,6 +59,17 @@ void QtDisplayWindow::destroyDeviceResources() {} void QtDisplayWindow::Render() {} +void QtDisplayWindow::onWindowResized(int width, int height) +{ + // imgui may not have been initialized yet + if (!ImGui::GetCurrentContext()) + return; + + auto& io = ImGui::GetIO(); + io.DisplaySize.x = static_cast(width); + io.DisplaySize.y = static_cast(height); +} + void QtDisplayWindow::keyPressEvent(QKeyEvent* event) { m_host_interface->handleKeyEvent(event->key(), true); @@ -63,3 +79,9 @@ void QtDisplayWindow::keyReleaseEvent(QKeyEvent* event) { m_host_interface->handleKeyEvent(event->key(), false); } + +void QtDisplayWindow::resizeEvent(QResizeEvent* event) +{ + QWindow::resizeEvent(event); + emit windowResizedEvent(event->size().width(), event->size().height()); +} diff --git a/src/duckstation-qt/qtdisplaywindow.h b/src/duckstation-qt/qtdisplaywindow.h index 1070061a0..d14f070f2 100644 --- a/src/duckstation-qt/qtdisplaywindow.h +++ b/src/duckstation-qt/qtdisplaywindow.h @@ -2,6 +2,7 @@ #include class QKeyEvent; +class QResizeEvent; class HostDisplay; @@ -23,14 +24,21 @@ public: virtual void Render(); + // this comes back on the emu thread + virtual void onWindowResized(int width, int height); + +Q_SIGNALS: + void windowResizedEvent(int width, int height); + protected: virtual bool createImGuiContext(); virtual void destroyImGuiContext(); virtual bool createDeviceResources(); virtual void destroyDeviceResources(); - void keyPressEvent(QKeyEvent* event) override; - void keyReleaseEvent(QKeyEvent* event) override; + virtual void keyPressEvent(QKeyEvent* event) override; + virtual void keyReleaseEvent(QKeyEvent* event) override; + virtual void resizeEvent(QResizeEvent* event) override; QtHostInterface* m_host_interface; }; diff --git a/src/duckstation-qt/qthostinterface.cpp b/src/duckstation-qt/qthostinterface.cpp index 7421bff09..4cadfa96a 100644 --- a/src/duckstation-qt/qthostinterface.cpp +++ b/src/duckstation-qt/qthostinterface.cpp @@ -130,6 +130,8 @@ void QtHostInterface::refreshGameList(bool invalidate_cache /*= false*/) QWidget* QtHostInterface::createDisplayWidget(QWidget* parent) { m_display_window = new OpenGLDisplayWindow(this, nullptr); + connect(m_display_window, &QtDisplayWindow::windowResizedEvent, this, &QtHostInterface::onDisplayWindowResized); + m_display.release(); m_display = std::unique_ptr(m_display_window->getHostDisplayInterface()); return QWidget::createWindowContainer(m_display_window, parent); @@ -176,6 +178,11 @@ void QtHostInterface::doHandleKeyEvent(int key, bool pressed) iter->second(pressed); } +void QtHostInterface::onDisplayWindowResized(int width, int height) +{ + m_display_window->onWindowResized(width, height); +} + void QtHostInterface::updateInputMap() { if (!isOnWorkerThread()) @@ -352,16 +359,17 @@ void QtHostInterface::threadEntryPoint() // rendering { - // DrawImGui(); - if (m_system) + { + DrawDebugWindows(); m_system->GetGPU()->ResetGraphicsAPIState(); + } + + DrawFPSWindow(); + DrawOSDMessages(); - // ImGui::Render(); m_display->Render(); - // ImGui::NewFrame(); - if (m_system) { m_system->GetGPU()->RestoreGraphicsAPIState(); diff --git a/src/duckstation-qt/qthostinterface.h b/src/duckstation-qt/qthostinterface.h index 02ee616c9..d0eeb1506 100644 --- a/src/duckstation-qt/qthostinterface.h +++ b/src/duckstation-qt/qthostinterface.h @@ -67,6 +67,7 @@ private Q_SLOTS: void doBootSystem(QString initial_filename, QString initial_save_state_filename); void doUpdateInputMap(); void doHandleKeyEvent(int key, bool pressed); + void onDisplayWindowResized(int width, int height); private: class Thread : public QThread