Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ImGui, GLFW] Fix crash when quitting #142

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions SofaGLFW/src/SofaGLFW/SofaGLFWBaseGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@

#include <SofaGLFW/SofaGLFWWindow.h>

#include <sofa/helper/logging/Messaging.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/simulation/Node.h>
#include <sofa/simulation/Simulation.h>
#include <sofa/core/visual/VisualParams.h>
#include <SofaGLFW/SofaGLFWMouseManager.h>

#include <sofa/component/visual/InteractiveCamera.h>
#include <sofa/component/visual/VisualStyle.h>

#include <sofa/helper/logging/Messaging.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/io/STBImage.h>

#include <algorithm>
#include <sofa/helper/system/FileRepository.h>
#include <sofa/simulation/SimulationLoop.h>

#include <sofa/helper/system/FileSystem.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/objectmodel/KeypressedEvent.h>
#include <sofa/core/objectmodel/KeyreleasedEvent.h>
#include <sofa/simulation/Node.h>
#include <sofa/simulation/Simulation.h>
#include <sofa/simulation/SimulationLoop.h>
#include <sofa/component/visual/InteractiveCamera.h>
#include <sofa/component/visual/VisualStyle.h>
#include <sofa/gui/common/BaseViewer.h>
#include <sofa/gui/common/BaseGUI.h>
#include <sofa/gui/common/PickHandler.h>
#include <sofa/helper/system/FileSystem.h>

#include <algorithm>

using namespace sofa;
using namespace sofa::gui::common;

Expand Down Expand Up @@ -275,6 +274,7 @@ bool SofaGLFWBaseGUI::createWindow(int width, int height, const char* title, boo
{
glfwWindow = glfwCreateWindow(width > 0 ? width : 100, height > 0 ? height : 100, title, nullptr, m_firstWindow);
}
s_numberOfActiveWindows++;

setWindowIcon(glfwWindow);

Expand Down Expand Up @@ -458,22 +458,22 @@ std::size_t SofaGLFWBaseGUI::runLoop(std::size_t targetNbIterations)
bool running = true;
std::size_t currentNbIterations = 0;
std::stringstream tmpStr;
while (!s_mapWindows.empty() && running)
while (s_numberOfActiveWindows > 0 && running)
{
SIMULATION_LOOP_SCOPE

// Keep running
runStep();

for (auto& [glfwWindow, sofaGlfwWindow] : s_mapWindows)
{
if (sofaGlfwWindow)
if (glfwWindow && sofaGlfwWindow)
{
// while user did not request to close this window (i.e press escape), draw
if (!glfwWindowShouldClose(glfwWindow))
{
makeCurrentContext(glfwWindow);

m_guiEngine->beforeDraw(glfwWindow);
sofaGlfwWindow->draw(m_groot, m_vparams);
m_guiEngine->afterDraw();
Expand All @@ -486,7 +486,6 @@ std::size_t SofaGLFWBaseGUI::runLoop(std::size_t targetNbIterations)

m_viewPortHeight = m_vparams->viewport()[3];
m_viewPortWidth = m_vparams->viewport()[2];

}
else
{
Expand Down Expand Up @@ -861,8 +860,9 @@ void SofaGLFWBaseGUI::close_callback(GLFWwindow* window)
{
glfwWindow->close();
delete glfwWindow;
glfwWindow = nullptr;
s_numberOfActiveWindows--;
}
s_mapWindows.erase(window);
}
}

Expand Down
1 change: 1 addition & 0 deletions SofaGLFW/src/SofaGLFW/SofaGLFWBaseGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class SOFAGLFW_API SofaGLFWBaseGUI : public BaseViewer

inline static std::map<GLFWwindow*, SofaGLFWWindow*> s_mapWindows{};
inline static std::map<GLFWwindow*, SofaGLFWBaseGUI*> s_mapGUIs{};
inline static std::size_t s_numberOfActiveWindows = 0;

bool m_bGlfwIsInitialized{ false };
bool m_bGlewIsInitialized{ false };
Expand Down
7 changes: 4 additions & 3 deletions SofaGLFW/src/SofaGLFW/SofaGLFWWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void SofaGLFWWindow::close()
}


void SofaGLFWWindow::draw(simulation::NodeSPtr groot, core::visual::VisualParams* vparams){
void SofaGLFWWindow::draw(simulation::NodeSPtr groot, core::visual::VisualParams* vparams)
{
glClearColor(m_backgroundColor.r(), m_backgroundColor.g(), m_backgroundColor.b(), m_backgroundColor.a());
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Expand All @@ -65,9 +66,9 @@ void SofaGLFWWindow::draw(simulation::NodeSPtr groot, core::visual::VisualParams
msg_error("SofaGLFWGUI") << "No camera defined.";
return;
}

if (groot->f_bbox.getValue().isValid())
{
{
vparams->sceneBBox() = groot->f_bbox.getValue();
m_currentCamera->setBoundingBox(vparams->sceneBBox().minBBox(), vparams->sceneBBox().maxBBox());
}
Expand Down
6 changes: 3 additions & 3 deletions SofaImGui/src/SofaImGui/ImGuiGUIEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void ImGuiGUIEngine::loadFile(sofaglfw::SofaGLFWBaseGUI* baseGUI, sofa::core::sp
if( !groot )
groot = sofa::simulation::getSimulation()->createNewGraph("");
baseGUI->setSimulation(groot, filePathName);

sofa::simulation::node::initRoot(groot.get());
auto camera = baseGUI->findCamera(groot);
if (camera)
Expand Down Expand Up @@ -361,8 +361,8 @@ void ImGuiGUIEngine::startFrame(sofaglfw::SofaGLFWBaseGUI* baseGUI)
ImGui::Separator();
if (ImGui::MenuItem("Exit"))
{
//TODO: brutal exit, need to clean up everything (simulation, window, opengl, imgui etc)
exit(EXIT_SUCCESS);
this->terminate();
return;
}
ImGui::EndMenu();
}
Expand Down
Loading