From eae79f653dbbbb0084626ccd39ede1542d62e934 Mon Sep 17 00:00:00 2001 From: Giorge Koulin Date: Tue, 1 Oct 2024 06:31:57 +0100 Subject: [PATCH] tidy up on shutdown to allow multiple `init`/`shutdown` cycles per process - Hide window. - Clear `contextStack`. - Reset cached `lazy` properties. Had to convert from namespace to struct to allow default initialisation reset. - Set `state::initialized = false`. --- src/polyscope.cpp | 50 +++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/polyscope.cpp b/src/polyscope.cpp index b26dec14..30f2e83e 100644 --- a/src/polyscope.cpp +++ b/src/polyscope.cpp @@ -934,6 +934,8 @@ bool windowRequestsClose() { return false; } +void resetLazy(); + void shutdown() { // TODO should we make an effort to destruct everything here? @@ -941,7 +943,11 @@ void shutdown() { writePrefsFile(); } + render::engine->hideWindow(); render::engine->shutdownImGui(); + contextStack.clear(); + resetLazy(); + state::initialized = false; } bool registerStructure(Structure* s, bool replaceIfPresent) { @@ -1179,7 +1185,7 @@ void refresh() { } // Cached versions of lazy properties used for updates -namespace lazy { +struct Lazy { TransparencyMode transparencyMode = TransparencyMode::None; int transparencyRenderPasses = 8; int ssaaFactor = 1; @@ -1187,8 +1193,10 @@ bool groundPlaneEnabled = true; GroundPlaneMode groundPlaneMode = GroundPlaneMode::TileReflection; ScaledValue groundPlaneHeightFactor = 0; int shadowBlurIters = 2; -float shadowDarkness = .4; -} // namespace lazy +float shadowDarkness = .4f; +}; +static Lazy lazy{}; + void processLazyProperties() { @@ -1203,49 +1211,53 @@ void processLazyProperties() { // transparency mode - if (lazy::transparencyMode != options::transparencyMode) { - lazy::transparencyMode = options::transparencyMode; + if (lazy.transparencyMode != options::transparencyMode) { + lazy.transparencyMode = options::transparencyMode; render::engine->setTransparencyMode(options::transparencyMode); } // transparency render passes - if (lazy::transparencyRenderPasses != options::transparencyRenderPasses) { - lazy::transparencyRenderPasses = options::transparencyRenderPasses; + if (lazy.transparencyRenderPasses != options::transparencyRenderPasses) { + lazy.transparencyRenderPasses = options::transparencyRenderPasses; requestRedraw(); } // ssaa - if (lazy::ssaaFactor != options::ssaaFactor) { - lazy::ssaaFactor = options::ssaaFactor; + if (lazy.ssaaFactor != options::ssaaFactor) { + lazy.ssaaFactor = options::ssaaFactor; render::engine->setSSAAFactor(options::ssaaFactor); } // ground plane - if (lazy::groundPlaneEnabled != options::groundPlaneEnabled || lazy::groundPlaneMode != options::groundPlaneMode) { - lazy::groundPlaneEnabled = options::groundPlaneEnabled; + if (lazy.groundPlaneEnabled != options::groundPlaneEnabled || lazy.groundPlaneMode != options::groundPlaneMode) { + lazy.groundPlaneEnabled = options::groundPlaneEnabled; if (!options::groundPlaneEnabled) { // if the (depecated) groundPlaneEnabled = false, set mode to None, so we only have one variable to check options::groundPlaneMode = GroundPlaneMode::None; } - lazy::groundPlaneMode = options::groundPlaneMode; + lazy.groundPlaneMode = options::groundPlaneMode; render::engine->groundPlane.prepare(); requestRedraw(); } - if (lazy::groundPlaneHeightFactor.asAbsolute() != options::groundPlaneHeightFactor.asAbsolute() || - lazy::groundPlaneHeightFactor.isRelative() != options::groundPlaneHeightFactor.isRelative()) { - lazy::groundPlaneHeightFactor = options::groundPlaneHeightFactor; + if (lazy.groundPlaneHeightFactor.asAbsolute() != options::groundPlaneHeightFactor.asAbsolute() || + lazy.groundPlaneHeightFactor.isRelative() != options::groundPlaneHeightFactor.isRelative()) { + lazy.groundPlaneHeightFactor = options::groundPlaneHeightFactor; requestRedraw(); } - if (lazy::shadowBlurIters != options::shadowBlurIters) { - lazy::shadowBlurIters = options::shadowBlurIters; + if (lazy.shadowBlurIters != options::shadowBlurIters) { + lazy.shadowBlurIters = options::shadowBlurIters; requestRedraw(); } - if (lazy::shadowDarkness != options::shadowDarkness) { - lazy::shadowDarkness = options::shadowDarkness; + if (lazy.shadowDarkness != options::shadowDarkness) { + lazy.shadowDarkness = options::shadowDarkness; requestRedraw(); } }; +void resetLazy() { + lazy = {}; +} + void updateStructureExtents() { if (!options::automaticallyComputeSceneExtents) {