mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 04:30:05 +00:00 
			
		
		
		
	Use ImGui Manager to create contexts
This commit is contained in:
		
							parent
							
								
									ee2ec329da
								
							
						
					
					
						commit
						0f8b8d73f9
					
				
					 3 changed files with 37 additions and 80 deletions
				
			
		|  | @ -4,12 +4,6 @@ | |||
| #include <scwx/qt/util/texture_atlas.hpp> | ||||
| #include <scwx/util/logger.hpp> | ||||
| 
 | ||||
| #include <backends/imgui_impl_opengl3.h> | ||||
| #include <backends/imgui_impl_qt.hpp> | ||||
| #include <imgui.h> | ||||
| #include <QOffscreenSurface> | ||||
| #include <QOpenGLContext> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
|  | @ -18,58 +12,21 @@ namespace manager | |||
| { | ||||
| namespace ResourceManager | ||||
| { | ||||
| static const std::string logPrefix_ = "scwx::qt::manager::ResourceManager"; | ||||
| static const std::string logPrefix_ = "scwx::qt::manager::resource_manager"; | ||||
| static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | ||||
| 
 | ||||
| static void InitializeImGui(); | ||||
| static void LoadFonts(); | ||||
| static void LoadTextures(); | ||||
| static void ShutdownImGui(); | ||||
| 
 | ||||
| static std::unique_ptr<QOffscreenSurface> surface_ {}; | ||||
| static ImGuiContext*                      imGuiContext_ {}; | ||||
| 
 | ||||
| void Initialize() | ||||
| { | ||||
|    config::CountyDatabase::Initialize(); | ||||
| 
 | ||||
|    InitializeImGui(); | ||||
|    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
 | ||||
|    imGuiContext_ = ImGui::CreateContext(); | ||||
|    ImGui_ImplQt_Init(); | ||||
|    ImGui_ImplOpenGL3_Init(); | ||||
|    QOpenGLContext::globalShareContext()->doneCurrent(); | ||||
| 
 | ||||
|    // ImGui Configuration
 | ||||
|    auto& io = ImGui::GetIO(); | ||||
| 
 | ||||
|    // Disable automatic configuration loading/saving
 | ||||
|    io.IniFilename = nullptr; | ||||
| 
 | ||||
|    // Style
 | ||||
|    auto& style = ImGui::GetStyle(); | ||||
| 
 | ||||
|    style.WindowMinSize = {10.0f, 10.0f}; | ||||
| } | ||||
| void Shutdown() {} | ||||
| 
 | ||||
| static void LoadFonts() | ||||
| { | ||||
|  | @ -87,16 +44,6 @@ static void LoadTextures() | |||
|    textureAtlas.BuildAtlas(8, 8); | ||||
| } | ||||
| 
 | ||||
| static void ShutdownImGui() | ||||
| { | ||||
|    QOpenGLContext::globalShareContext()->makeCurrent(surface_.get()); | ||||
|    ImGui::SetCurrentContext(imGuiContext_); | ||||
|    ImGui_ImplOpenGL3_Shutdown(); | ||||
|    ImGui_ImplQt_Shutdown(); | ||||
|    ImGui::DestroyContext(); | ||||
|    QOpenGLContext::globalShareContext()->doneCurrent(); | ||||
| } | ||||
| 
 | ||||
| } // namespace ResourceManager
 | ||||
| } // namespace manager
 | ||||
| } // namespace qt
 | ||||
|  |  | |||
|  | @ -1,5 +1,6 @@ | |||
| #include <scwx/qt/map/map_widget.hpp> | ||||
| #include <scwx/qt/gl/gl.hpp> | ||||
| #include <scwx/qt/manager/imgui_manager.hpp> | ||||
| #include <scwx/qt/manager/radar_product_manager.hpp> | ||||
| #include <scwx/qt/manager/settings_manager.hpp> | ||||
| #include <scwx/qt/map/alert_layer.hpp> | ||||
|  | @ -61,7 +62,7 @@ public: | |||
|        settings_(settings), | ||||
|        map_(), | ||||
|        layerList_ {}, | ||||
|        imGuiContext_ {ImGui::CreateContext()}, | ||||
|        imGuiRendererInitialized_ {false}, | ||||
|        radarProductManager_ {nullptr}, | ||||
|        radarProductLayer_ {nullptr}, | ||||
|        alertLayer_ {std::make_shared<AlertLayer>(context_)}, | ||||
|  | @ -82,14 +83,11 @@ public: | |||
|       SetRadarSite(scwx::qt::manager::SettingsManager::general_settings() | ||||
|                       ->default_radar_site()); | ||||
| 
 | ||||
|       // Set ImGui Context
 | ||||
|       ImGui::SetCurrentContext(imGuiContext_); | ||||
| 
 | ||||
|       // ImGui Configuration
 | ||||
|       auto& io = ImGui::GetIO(); | ||||
| 
 | ||||
|       // Disable automatic configuration loading/saving
 | ||||
|       io.IniFilename = nullptr; | ||||
|       // Create ImGui Context
 | ||||
|       static size_t currentMapId_ {0u}; | ||||
|       imGuiContextName_ = std::format("Map {}", ++currentMapId_); | ||||
|       imGuiContext_ = | ||||
|          manager::ImGuiManager::Instance().CreateContext(imGuiContextName_); | ||||
| 
 | ||||
|       // Initialize ImGui Qt backend
 | ||||
|       ImGui_ImplQt_Init(); | ||||
|  | @ -102,9 +100,14 @@ public: | |||
|       ImGui::SetCurrentContext(imGuiContext_); | ||||
| 
 | ||||
|       // Shutdown ImGui Context
 | ||||
|       ImGui_ImplOpenGL3_Shutdown(); | ||||
|       if (imGuiRendererInitialized_) | ||||
|       { | ||||
|          ImGui_ImplOpenGL3_Shutdown(); | ||||
|       } | ||||
|       ImGui_ImplQt_Shutdown(); | ||||
|       ImGui::DestroyContext(imGuiContext_); | ||||
| 
 | ||||
|       // Destroy ImGui Context
 | ||||
|       manager::ImGuiManager::Instance().DestroyContext(imGuiContextName_); | ||||
|    } | ||||
| 
 | ||||
|    void AddLayer(const std::string&            id, | ||||
|  | @ -129,6 +132,8 @@ public: | |||
|    std::list<std::string>            layerList_; | ||||
| 
 | ||||
|    ImGuiContext* imGuiContext_; | ||||
|    std::string   imGuiContextName_; | ||||
|    bool          imGuiRendererInitialized_; | ||||
| 
 | ||||
|    std::shared_ptr<manager::RadarProductManager> radarProductManager_; | ||||
| 
 | ||||
|  | @ -676,6 +681,7 @@ void MapWidget::initializeGL() | |||
|    // Initialize ImGui OpenGL3 backend
 | ||||
|    ImGui::SetCurrentContext(p->imGuiContext_); | ||||
|    ImGui_ImplOpenGL3_Init(); | ||||
|    p->imGuiRendererInitialized_ = true; | ||||
| 
 | ||||
|    p->map_.reset( | ||||
|       new QMapLibreGL::Map(nullptr, p->settings_, size(), pixelRatio())); | ||||
|  |  | |||
|  | @ -1,4 +1,5 @@ | |||
| #include <scwx/qt/ui/imgui_debug_widget.hpp> | ||||
| #include <scwx/qt/manager/imgui_manager.hpp> | ||||
| 
 | ||||
| #include <imgui.h> | ||||
| #include <backends/imgui_impl_opengl3.h> | ||||
|  | @ -16,17 +17,12 @@ static const std::string logPrefix_ = "scwx::qt::ui::imgui_debug_widget"; | |||
| class ImGuiDebugWidgetImpl | ||||
| { | ||||
| public: | ||||
|    explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) : | ||||
|        self_ {self}, context_ {ImGui::CreateContext()} | ||||
|    explicit ImGuiDebugWidgetImpl(ImGuiDebugWidget* self) : self_ {self} | ||||
|    { | ||||
|       // Set ImGui Context
 | ||||
|       ImGui::SetCurrentContext(context_); | ||||
| 
 | ||||
|       // ImGui Configuration
 | ||||
|       auto& io = ImGui::GetIO(); | ||||
| 
 | ||||
|       // Disable automatic configuration loading/saving
 | ||||
|       io.IniFilename = nullptr; | ||||
|       // Create ImGui Context
 | ||||
|       static size_t currentIndex_ {0u}; | ||||
|       contextName_ = std::format("ImGui Debug {}", ++currentIndex_); | ||||
|       context_ = manager::ImGuiManager::Instance().CreateContext(contextName_); | ||||
| 
 | ||||
|       // Initialize ImGui Qt backend
 | ||||
|       ImGui_ImplQt_Init(); | ||||
|  | @ -39,15 +35,22 @@ public: | |||
|       ImGui::SetCurrentContext(context_); | ||||
| 
 | ||||
|       // Shutdown ImGui Context
 | ||||
|       ImGui_ImplOpenGL3_Shutdown(); | ||||
|       if (imGuiRendererInitialized_) | ||||
|       { | ||||
|          ImGui_ImplOpenGL3_Shutdown(); | ||||
|       } | ||||
|       ImGui_ImplQt_Shutdown(); | ||||
|       ImGui::DestroyContext(context_); | ||||
| 
 | ||||
|       // Destroy ImGui Context
 | ||||
|       manager::ImGuiManager::Instance().DestroyContext(contextName_); | ||||
|    } | ||||
| 
 | ||||
|    ImGuiDebugWidget* self_; | ||||
|    ImGuiContext*     context_; | ||||
|    std::string       contextName_; | ||||
| 
 | ||||
|    bool firstRender_ {true}; | ||||
|    bool imGuiRendererInitialized_ {false}; | ||||
| }; | ||||
| 
 | ||||
| ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) : | ||||
|  | @ -57,6 +60,8 @@ ImGuiDebugWidget::ImGuiDebugWidget(QWidget* parent) : | |||
|    setFocusPolicy(Qt::StrongFocus); | ||||
| } | ||||
| 
 | ||||
| ImGuiDebugWidget::~ImGuiDebugWidget() {} | ||||
| 
 | ||||
| void ImGuiDebugWidget::initializeGL() | ||||
| { | ||||
|    makeCurrent(); | ||||
|  | @ -64,6 +69,7 @@ void ImGuiDebugWidget::initializeGL() | |||
|    // Initialize ImGui OpenGL3 backend
 | ||||
|    ImGui::SetCurrentContext(p->context_); | ||||
|    ImGui_ImplOpenGL3_Init(); | ||||
|    p->imGuiRendererInitialized_ = true; | ||||
| } | ||||
| 
 | ||||
| void ImGuiDebugWidget::paintGL() | ||||
|  | @ -93,8 +99,6 @@ void ImGuiDebugWidget::paintGL() | |||
|    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | ||||
| } | ||||
| 
 | ||||
| ImGuiDebugWidget::~ImGuiDebugWidget() {} | ||||
| 
 | ||||
| } // namespace ui
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat