Connect ImGui render to QOpenGLWidgets

This commit is contained in:
Dan Paulat 2022-11-20 22:12:05 -06:00
parent 3c69ad28c6
commit dd1b4f27c8
4 changed files with 76 additions and 6 deletions

View file

@ -21,6 +21,8 @@ int main(int argc, char* argv[])
scwx::util::Logger::Initialize(); scwx::util::Logger::Initialize();
spdlog::set_level(spdlog::level::debug); spdlog::set_level(spdlog::level::debug);
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
QApplication a(argc, argv); QApplication a(argc, argv);
QCoreApplication::setApplicationName("Supercell Wx"); QCoreApplication::setApplicationName("Supercell Wx");
@ -61,7 +63,7 @@ int main(int argc, char* argv[])
// Initialize application // Initialize application
scwx::qt::config::RadarSite::Initialize(); scwx::qt::config::RadarSite::Initialize();
scwx::qt::manager::SettingsManager::Initialize(); scwx::qt::manager::SettingsManager::Initialize();
scwx::qt::manager::ResourceManager::PreLoad(); scwx::qt::manager::ResourceManager::Initialize();
// Run Qt main loop // Run Qt main loop
int result; int result;
@ -78,6 +80,9 @@ int main(int argc, char* argv[])
work.reset(); work.reset();
threadPool.join(); threadPool.join();
// Shutdown application
scwx::qt::manager::ResourceManager::Shutdown();
// Shutdown AWS SDK // Shutdown AWS SDK
Aws::ShutdownAPI(awsSdkOptions); Aws::ShutdownAPI(awsSdkOptions);

View file

@ -2,6 +2,12 @@
#include <scwx/qt/config/county_database.hpp> #include <scwx/qt/config/county_database.hpp>
#include <scwx/qt/util/font.hpp> #include <scwx/qt/util/font.hpp>
#include <scwx/qt/util/texture_atlas.hpp> #include <scwx/qt/util/texture_atlas.hpp>
#include <scwx/util/logger.hpp>
#include <backends/imgui_impl_opengl3.h>
#include <imgui.h>
#include <QOffscreenSurface>
#include <QOpenGLContext>
namespace scwx namespace scwx
{ {
@ -11,20 +17,54 @@ namespace manager
{ {
namespace ResourceManager namespace ResourceManager
{ {
static void LoadFonts(); static const std::string logPrefix_ = "scwx::qt::manager::ResourceManager";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
void PreLoad() static void InitializeImGui();
static void LoadFonts();
static void LoadTextures();
static void ShutdownImGui();
static std::unique_ptr<QOffscreenSurface> surface_ {};
void Initialize()
{ {
config::CountyDatabase::Initialize();
InitializeImGui();
LoadFonts(); LoadFonts();
LoadTextures();
}
void Shutdown()
{
ShutdownImGui();
}
static void InitializeImGui()
{
// Create OpenGL Offscreen Surface
surface_ = std::make_unique<QOffscreenSurface>();
surface_->create();
if (!QOpenGLContext::globalShareContext()->makeCurrent(surface_.get()))
{
logger_->warn("Failed to initialize offscreen surface");
}
// Initialize ImGui
ImGui::CreateContext();
ImGui_ImplOpenGL3_Init();
QOpenGLContext::globalShareContext()->doneCurrent();
} }
static void LoadFonts() static void LoadFonts()
{ {
config::CountyDatabase::Initialize();
util::Font::Create(":/res/fonts/din1451alt.ttf"); util::Font::Create(":/res/fonts/din1451alt.ttf");
util::Font::Create(":/res/fonts/din1451alt_g.ttf"); util::Font::Create(":/res/fonts/din1451alt_g.ttf");
}
static void LoadTextures()
{
util::TextureAtlas& textureAtlas = util::TextureAtlas::Instance(); util::TextureAtlas& textureAtlas = util::TextureAtlas::Instance();
textureAtlas.RegisterTexture("lines/default-1x7", textureAtlas.RegisterTexture("lines/default-1x7",
":/res/textures/lines/default-1x7.png"); ":/res/textures/lines/default-1x7.png");
@ -33,6 +73,14 @@ static void LoadFonts()
textureAtlas.BuildAtlas(8, 8); textureAtlas.BuildAtlas(8, 8);
} }
static void ShutdownImGui()
{
QOpenGLContext::globalShareContext()->makeCurrent(surface_.get());
ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext();
QOpenGLContext::globalShareContext()->doneCurrent();
}
} // namespace ResourceManager } // namespace ResourceManager
} // namespace manager } // namespace manager
} // namespace qt } // namespace qt

View file

@ -8,7 +8,8 @@ namespace manager
{ {
namespace ResourceManager namespace ResourceManager
{ {
void PreLoad(); void Initialize();
void Shutdown();
} // namespace ResourceManager } // namespace ResourceManager
} // namespace manager } // namespace manager
} // namespace qt } // namespace qt

View file

@ -13,6 +13,8 @@
#include <scwx/util/threads.hpp> #include <scwx/util/threads.hpp>
#include <scwx/util/time.hpp> #include <scwx/util/time.hpp>
#include <backends/imgui_impl_opengl3.h>
#include <imgui.h>
#include <QApplication> #include <QApplication>
#include <QColor> #include <QColor>
#include <QDebug> #include <QDebug>
@ -682,10 +684,24 @@ void MapWidget::initializeGL()
void MapWidget::paintGL() void MapWidget::paintGL()
{ {
p->frameDraws_++; p->frameDraws_++;
// Setup ImGui Frame
ImGui::GetIO().DisplaySize = {static_cast<float>(size().width()),
static_cast<float>(size().height())};
// Start ImGui Frame
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
// Render QMapLibreGL Map
p->map_->resize(size()); p->map_->resize(size());
p->map_->setFramebufferObject(defaultFramebufferObject(), p->map_->setFramebufferObject(defaultFramebufferObject(),
size() * pixelRatio()); size() * pixelRatio());
p->map_->render(); p->map_->render();
// Render ImGui Frame
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
} }
void MapWidget::mapChanged(QMapLibreGL::Map::MapChange mapChange) void MapWidget::mapChanged(QMapLibreGL::Map::MapChange mapChange)