From 875eb957909ca88463ee51e5b786663960966218 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 24 Sep 2013 03:44:15 +0000 Subject: [PATCH] bugfix for particle cleanup --- source/glest_game/graphics/renderer.cpp | 4 ++ source/glest_game/graphics/renderer.h | 1 + source/glest_game/type_instances/unit.cpp | 60 ++++++++++--------- source/shared_lib/include/graphics/particle.h | 1 + .../shared_lib/sources/graphics/particle.cpp | 21 +++++++ 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 70f679c0..85c553f9 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -969,6 +969,10 @@ bool Renderer::validateParticleSystemStillExists(ParticleSystem * particleSystem return particleManager[rs]->validateParticleSystemStillExists(particleSystem); } +void Renderer::removeParticleSystemsForParticleOwner(ParticleOwner * particleOwner,ResourceScope rs) { + return particleManager[rs]->removeParticleSystemsForParticleOwner(particleOwner); +} + void Renderer::cleanupParticleSystems(vector &particleSystems, ResourceScope rs) { particleManager[rs]->cleanupParticleSystems(particleSystems); } diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index c53240de..d37ac6f5 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -476,6 +476,7 @@ public: void cleanupParticleSystems(vector &particleSystems,ResourceScope rs); void cleanupUnitParticleSystems(vector &particleSystems,ResourceScope rs); bool validateParticleSystemStillExists(ParticleSystem * particleSystem,ResourceScope rs) const; + void removeParticleSystemsForParticleOwner(ParticleOwner * particleOwner,ResourceScope rs); void updateParticleManager(ResourceScope rs,int renderFps=-1); void renderParticleManager(ResourceScope rs); void swapBuffers(); diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 9de9b618..435f3863 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -585,7 +585,9 @@ Unit::~Unit() { delete currentAttackBoostOriginatorEffect.currentAppliedEffect; currentAttackBoostOriginatorEffect.currentAppliedEffect = NULL; - Renderer::getInstance().cleanupParticleSystems(attackParticleSystems,rsGame); + //Renderer::getInstance().cleanupParticleSystems(attackParticleSystems,rsGame); + Renderer::getInstance().removeParticleSystemsForParticleOwner(this,rsGame); + #ifdef LEAK_CHECK_UNITS Unit::mapMemoryList2[this->unitPath] = this->getId(); @@ -630,6 +632,34 @@ void Unit::dumpMemoryList() { } #endif +void Unit::end(ParticleSystem *particleSystem) { + vector::iterator iterFind = find(attackParticleSystems.begin(),attackParticleSystems.end(),particleSystem); + if(iterFind != attackParticleSystems.end()) { + attackParticleSystems.erase(iterFind); + } + vector::iterator iterFind1 = find(smokeParticleSystems.begin(),smokeParticleSystems.end(),particleSystem); + if(iterFind1 != smokeParticleSystems.end()) { + smokeParticleSystems.erase(iterFind1); + } + iterFind = find(fireParticleSystems.begin(),fireParticleSystems.end(),particleSystem); + if(iterFind != fireParticleSystems.end()) { + fireParticleSystems.erase(iterFind); + } + iterFind1 = find(damageParticleSystems.begin(),damageParticleSystems.end(),particleSystem); + if(iterFind1 != damageParticleSystems.end()) { + damageParticleSystems.erase(iterFind1); + } + + iterFind1 = find(unitParticleSystems.begin(),unitParticleSystems.end(),particleSystem); + if(iterFind1 != unitParticleSystems.end()) { + unitParticleSystems.erase(iterFind1); + } + + if(particleSystem == fire) { + fire = NULL; + } +} + //bool Unit::isUnitDeleted(void *unit) { // bool result = false; // MutexSafeWrapper safeMutex(&mutexDeletedUnits,string(__FILE__) + "_" + intToStr(__LINE__)); @@ -4092,34 +4122,6 @@ bool Unit::showTranslatedTechTree() const { return (this->game != NULL ? this->game->showTranslatedTechTree() : true); } -void Unit::end(ParticleSystem *particleSystem) { - vector::iterator iterFind = find(attackParticleSystems.begin(),attackParticleSystems.end(),particleSystem); - if(iterFind != attackParticleSystems.end()) { - attackParticleSystems.erase(iterFind); - } - vector::iterator iterFind1 = find(smokeParticleSystems.begin(),smokeParticleSystems.end(),particleSystem); - if(iterFind1 != smokeParticleSystems.end()) { - smokeParticleSystems.erase(iterFind1); - } - iterFind = find(fireParticleSystems.begin(),fireParticleSystems.end(),particleSystem); - if(iterFind != fireParticleSystems.end()) { - fireParticleSystems.erase(iterFind); - } - iterFind1 = find(damageParticleSystems.begin(),damageParticleSystems.end(),particleSystem); - if(iterFind1 != damageParticleSystems.end()) { - damageParticleSystems.erase(iterFind1); - } - - iterFind1 = find(unitParticleSystems.begin(),unitParticleSystems.end(),particleSystem); - if(iterFind1 != unitParticleSystems.end()) { - unitParticleSystems.erase(iterFind1); - } - - if(particleSystem == fire) { - fire = NULL; - } -} - string Unit::getNetworkCRCDecHpList() const { string result = ""; for(unsigned int index = 0; index < networkCRCDecHpList.size(); ++index) { diff --git a/source/shared_lib/include/graphics/particle.h b/source/shared_lib/include/graphics/particle.h index bd241ae0..0ee0cf35 100644 --- a/source/shared_lib/include/graphics/particle.h +++ b/source/shared_lib/include/graphics/particle.h @@ -625,6 +625,7 @@ public: void cleanupUnitParticleSystems(vector &particleSystems); int findParticleSystems(ParticleSystem *psFind, const vector &particleSystems) const; bool validateParticleSystemStillExists(ParticleSystem * particleSystem) const; + void removeParticleSystemsForParticleOwner(ParticleOwner * particleOwner); bool hasActiveParticleSystem(ParticleSystem::ParticleSystemType type) const; }; diff --git a/source/shared_lib/sources/graphics/particle.cpp b/source/shared_lib/sources/graphics/particle.cpp index 5060b57c..3860addb 100644 --- a/source/shared_lib/sources/graphics/particle.cpp +++ b/source/shared_lib/sources/graphics/particle.cpp @@ -2198,6 +2198,22 @@ bool ParticleManager::validateParticleSystemStillExists(ParticleSystem * particl return (index >= 0); } +void ParticleManager::removeParticleSystemsForParticleOwner(ParticleOwner *particleOwner) { + if(particleOwner != NULL && particleSystems.empty() == false) { + vector cleanupParticleSystemsList; + + for(unsigned int index = 0; index < particleSystems.size(); ++index) { + ParticleSystem *ps= particleSystems[index]; + if(ps != NULL && ps->getParticleOwner() == particleOwner) { + cleanupParticleSystemsList.push_back(ps); + } + } + if(cleanupParticleSystemsList.empty() == false) { + cleanupParticleSystems(cleanupParticleSystemsList); + } + } +} + int ParticleManager::findParticleSystems(ParticleSystem *psFind, const vector &particleSystems) const{ int result= -1; for(unsigned int i= 0; i < particleSystems.size(); i++){ @@ -2221,6 +2237,11 @@ void ParticleManager::cleanupParticleSystems(ParticleSystem *ps) { // } // deleteList[ps]++; + // This code causes segfault on game end, no need to fade, just delete + //if(ps->getState() != ParticleSystem::sFade) { + // ps->fade(); + //} + if(ps != NULL) { ps->callParticleOwnerEnd(ps); }