From d80fa387b9949bc8fad407a4847862f88973ce94 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sat, 28 May 2016 16:16:48 +0200 Subject: [PATCH] Fixed some particle crashes after 99a831a03be068dd0c40ee3a7ca162cdcfb5e85d --- src/graphics/engine/particle.cpp | 24 ++++++++++++------------ src/graphics/engine/particle.h | 6 +++++- src/object/old_object.cpp | 1 + 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/graphics/engine/particle.cpp b/src/graphics/engine/particle.cpp index 2642b74e..5f8b1601 100644 --- a/src/graphics/engine/particle.cpp +++ b/src/graphics/engine/particle.cpp @@ -641,22 +641,22 @@ void CParticle::GetRankFromChannel(int &channel) int uniqueStamp = (channel>>16)&0xffff; channel &= 0xffff; - assert(channel >= 0 && channel < MAXPARTICULE*MAXPARTITYPE); - - if (!m_particle[channel].used) assert(!!"Tried to access invalid particle channel (used=false) !\n"); - if (m_particle[channel].uniqueStamp != uniqueStamp) assert(!!"Tried to access invalid particle channel (uniqueStamp changed) !\n"); + if (channel < 0 || channel >= MAXPARTICULE*MAXPARTITYPE) throw std::runtime_error("Tried to access invalid particle channel (invalid ID)"); + if (!m_particle[channel].used) throw std::runtime_error("Tried to access invalid particle channel (used=false)"); + if (m_particle[channel].uniqueStamp != uniqueStamp) throw std::runtime_error("Tried to access invalid particle channel (uniqueStamp changed)"); } bool CParticle::ParticleExists(int channel) { - int uniqueStamp = (channel>>16)&0xffff; - channel &= 0xffff; - - assert(channel >= 0 && channel < MAXPARTICULE*MAXPARTITYPE); - - if (!m_particle[channel].used) return false; - if (m_particle[channel].uniqueStamp != uniqueStamp) return false; - return true; + try + { + GetRankFromChannel(channel); + return true; + } + catch (const std::runtime_error& e) + { + return false; + } } void CParticle::DeleteRank(int rank) diff --git a/src/graphics/engine/particle.h b/src/graphics/engine/particle.h index 55ef7baa..ec8d8820 100644 --- a/src/graphics/engine/particle.h +++ b/src/graphics/engine/particle.h @@ -297,7 +297,11 @@ public: protected: //! Removes a particle of given rank void DeleteRank(int rank); - //! Adapts the channel so it can be used as an offset in m_particle + /** + * \brief Adapts the channel so it can be used as an offset in m_particle + * \param channel Channel number to process, will be modified to be index of particle in m_particle + * \throw std::runtime_error if this particle does not exist any more + **/ void GetRankFromChannel(int &channel); //! Draws a triangular particle void DrawParticleTriangle(int i); diff --git a/src/object/old_object.cpp b/src/object/old_object.cpp index 7c2682d8..d945c5c8 100644 --- a/src/object/old_object.cpp +++ b/src/object/old_object.cpp @@ -2975,6 +2975,7 @@ void COldObject::UpdateSelectParticle() // Updates lens. for ( i=0 ; i<4 ; i++ ) { + if (m_partiSel[i] == -1) continue; pos[i] = Math::Transform(m_objectPart[0].matWorld, pos[i]); dim[i].y = dim[i].x; m_particle->SetParam(m_partiSel[i], pos[i], dim[i], zoom[i], angle, 1.0f);