From 565a08b95a56db2abdae315300b71f9facd3b612 Mon Sep 17 00:00:00 2001 From: "florent.teppe" Date: Mon, 12 Sep 2022 19:13:02 +0200 Subject: [PATCH] crashfix on game exit --- components/debug/debugdraw.cpp | 18 +++++++++++++----- components/debug/debugdraw.hpp | 12 ++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/components/debug/debugdraw.cpp b/components/debug/debugdraw.cpp index 1180ceb513..8a2274d9a1 100644 --- a/components/debug/debugdraw.cpp +++ b/components/debug/debugdraw.cpp @@ -258,7 +258,7 @@ namespace Debug ext->glUniform1i(normalAsColorLocation, false); - for (const auto& shapeToDraw : mShapesToDraw) + for (const auto& shapeToDraw : *mShapesToDraw) { osg::Vec3f translation = shapeToDraw.mPosition; osg::Vec3f color = shapeToDraw.mColor; @@ -281,7 +281,7 @@ namespace Debug break; } } - mShapesToDraw.clear(); + mShapesToDraw->clear(); static_cast(mLinesToDraw->getVertexArray())->clear(); static_cast(mLinesToDraw->getNormalArray())->clear(); } @@ -377,6 +377,8 @@ Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_p for (std::size_t i = 0; i < mShapesToDraw.size(); i++) { + mShapesToDraw[i] = std::make_shared>(); + mCustomDebugDrawer[i] = new DebugCustomDraw(mShapesToDraw[i], mDebugLines->mLinesGeom[i]); mCustomDebugDrawer[i]->setStateSet(stateset); mCustomDebugDrawer[i]->mWireCubeGeometry = wireCube; @@ -390,11 +392,17 @@ Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_p Debug::DebugDrawer::~DebugDrawer() { -} + auto parentsList = mDebugDrawSceneObjects->getParents(); + + for (auto parent : parentsList) + { + parent->removeChild(mDebugDrawSceneObjects); + } +} void Debug::DebugDrawer::drawCube(osg::Vec3f mPosition, osg::Vec3f mDims, osg::Vec3f mColor) { - mShapesToDraw[getIdexBufferWriteFromFrame(this->mCurrentFrame)].push_back({ mPosition, mDims, mColor, DrawShape::Cube }); + mShapesToDraw[getIdexBufferWriteFromFrame(this->mCurrentFrame)]->push_back({ mPosition, mDims, mColor, DrawShape::Cube }); } void Debug::DebugDrawer::drawCubeMinMax(osg::Vec3f min, osg::Vec3f max, osg::Vec3f color) @@ -406,7 +414,7 @@ void Debug::DebugDrawer::drawCubeMinMax(osg::Vec3f min, osg::Vec3f max, osg::Vec void Debug::DebugDrawer::addDrawCall(const DrawCall& draw) { - mShapesToDraw[getIdexBufferWriteFromFrame(this->mCurrentFrame)].push_back(draw); + mShapesToDraw[getIdexBufferWriteFromFrame(this->mCurrentFrame)]->push_back(draw); } void Debug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color) diff --git a/components/debug/debugdraw.hpp b/components/debug/debugdraw.hpp index dcf9132712..dde84c65af 100644 --- a/components/debug/debugdraw.hpp +++ b/components/debug/debugdraw.hpp @@ -58,10 +58,14 @@ namespace Debug class DebugCustomDraw : public osg::Drawable { public: - DebugCustomDraw(std::vector& cubesToDraw, osg::ref_ptr& linesToDraw) : mShapesToDraw(cubesToDraw), mLinesToDraw(linesToDraw) {} + DebugCustomDraw(std::shared_ptr> cubesToDraw, osg::ref_ptr& linesToDraw) + : mShapesToDraw(cubesToDraw) + , mLinesToDraw(linesToDraw) + { + } - std::vector& mShapesToDraw; - osg::ref_ptr& mLinesToDraw; + std::shared_ptr> mShapesToDraw; + osg::ref_ptr mLinesToDraw; osg::ref_ptr mCubeGeometry; osg::ref_ptr mCylinderGeometry; @@ -87,7 +91,7 @@ namespace Debug private: std::unique_ptr mDebugLines; - std::array, 2> mShapesToDraw; + std::array>, 2> mShapesToDraw; long long int mCurrentFrame; std::array, 2> mCustomDebugDrawer;