From b38844700debb737dad4090774ad0426a0ebcc02 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 10 Mar 2013 23:20:37 +0300 Subject: Improve particle classes. --- src/particle.cpp | 42 +++++++++++++++++++++--------------------- src/particlecontainer.cpp | 24 ++++++++++++++---------- src/particleemitter.cpp | 45 ++++++++++++++++++++------------------------- src/particleemitterprop.h | 3 ++- 4 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/particle.cpp b/src/particle.cpp index 19d732653..a0a01a09a 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -207,10 +207,11 @@ bool Particle::update() FOR_EACH (EmitterConstIterator, e, mChildEmitters) { Particles newParticles = (*e)->createParticles(mLifetimePast); - FOR_EACH (ParticleConstIterator, p, newParticles) + FOR_EACH (ParticleConstIterator, it, newParticles) { - (*p)->moveBy(mPos); - mChildParticles.push_back (*p); + Particle *const p = *it; + p->moveBy(mPos); + mChildParticles.push_back (p); } } } @@ -236,18 +237,19 @@ bool Particle::update() for (ParticleIterator p = mChildParticles.begin(), p2 = mChildParticles.end(); p != p2; ) { + Particle *const particle = *p; //move particle with its parent if desired - if ((*p)->doesFollow()) - (*p)->moveBy(change); + if (particle->doesFollow()) + particle->moveBy(change); //update particle - if ((*p)->update()) + if (particle->update()) { ++p; } else { - delete (*p); + delete particle; p = mChildParticles.erase(p); } } @@ -262,8 +264,9 @@ void Particle::moveBy(const Vector &change) mPos += change; FOR_EACH (ParticleConstIterator, p, mChildParticles) { - if ((*p)->doesFollow()) - (*p)->moveBy(change); + Particle *const particle = *p; + if (particle->doesFollow()) + particle->moveBy(change); } } @@ -285,11 +288,9 @@ Particle *Particle::addEffect(const std::string &particleEffectFile, { Particle *newParticle = nullptr; - size_t pos = particleEffectFile.find('|'); - std::string dyePalettes; - if (pos != std::string::npos) - dyePalettes = particleEffectFile.substr(pos + 1); - + const size_t pos = particleEffectFile.find('|'); + const std::string dyePalettes = (pos != std::string::npos) + ? particleEffectFile.substr(pos + 1) : ""; XML::Document doc(particleEffectFile.substr(0, pos)); const XmlNodePtr rootNode = doc.rootNode(); @@ -346,9 +347,9 @@ Particle *Particle::addEffect(const std::string &particleEffectFile, effectChildNode, "position-y", 0)); const float offsetZ = static_cast(XML::getFloatProperty( effectChildNode, "position-z", 0)); - Vector position (mPos.x + static_cast(pixelX) + offsetX, - mPos.y + static_cast(pixelY) + offsetY, - mPos.z + offsetZ); + const Vector position (mPos.x + static_cast(pixelX) + offsetX, + mPos.y + static_cast(pixelY) + offsetY, + mPos.z + offsetZ); newParticle->moveTo(position); const int lifetime = XML::getProperty(effectChildNode, "lifetime", -1); @@ -363,14 +364,13 @@ Particle *Particle::addEffect(const std::string &particleEffectFile, { if (xmlNameEqual(emitterNode, "emitter")) { - ParticleEmitter *newEmitter; - newEmitter = new ParticleEmitter(emitterNode, newParticle, - mMap, rotation, dyePalettes); + ParticleEmitter *const newEmitter = new ParticleEmitter( + emitterNode, newParticle, mMap, rotation, dyePalettes); newParticle->addEmitter(newEmitter); } else if (xmlNameEqual(emitterNode, "deatheffect")) { - std::string deathEffect = reinterpret_cast( + const std::string deathEffect = reinterpret_cast( emitterNode->xmlChildrenNode->content); char deathEffectConditions = 0x00; diff --git a/src/particlecontainer.cpp b/src/particlecontainer.cpp index 4994ddadd..efab44619 100644 --- a/src/particlecontainer.cpp +++ b/src/particlecontainer.cpp @@ -32,7 +32,8 @@ ParticleContainer::ParticleContainer(ParticleContainer *const parent, const bool delParent): mDelParent(delParent), mNext(parent) -{} +{ +} ParticleContainer::~ParticleContainer() { @@ -82,9 +83,10 @@ void ParticleList::removeLocally(const Particle *const particle) for (std::list::iterator it = mElements.begin(); it != mElements.end(); ) { - if (*it == particle) + Particle *const p = *it; + if (p == particle) { - (*it)->kill(); + p->kill(); it = mElements.erase(it); } else @@ -109,10 +111,11 @@ void ParticleList::moveTo(const float x, const float y) for (std::list::iterator it = mElements.begin(); it != mElements.end(); ) { - (*it)->moveTo(x, y); - if ((*it)->isExtinct()) + Particle *const p = *it; + p->moveTo(x, y); + if (p->isExtinct()) { - (*it)->kill(); + p->kill(); it = mElements.erase(it); } else @@ -176,13 +179,14 @@ void ParticleVector::moveTo(const float x, const float y) for (std::vector::iterator it = mIndexedElements.begin(); it != mIndexedElements.end(); ++it) { - if (*it) + Particle *const p = *it; + if (p) { - (*it)->moveTo(x, y); + p->moveTo(x, y); - if ((*it)->isExtinct()) + if (p->isExtinct()) { - (*it)->kill(); + p->kill(); *it = nullptr; } } diff --git a/src/particleemitter.cpp b/src/particleemitter.cpp index 0aea1eaca..71eff9c3f 100644 --- a/src/particleemitter.cpp +++ b/src/particleemitter.cpp @@ -76,7 +76,8 @@ ParticleEmitter::ParticleEmitter(const XmlNodePtr emitterNode, { if (xmlNameEqual(propertyNode, "property")) { - std::string name = XML::getProperty(propertyNode, "name", ""); + const std::string name = XML::getProperty( + propertyNode, "name", ""); if (name == "position-x") { @@ -227,14 +228,13 @@ ParticleEmitter::ParticleEmitter(const XmlNodePtr emitterNode, { const int delay = XML::getIntProperty( frameNode, "delay", 0, 0, 100000); - int offsetX = XML::getProperty(frameNode, "offsetX", 0); - int offsetY = XML::getProperty(frameNode, "offsetY", 0); + const int offsetX = XML::getProperty(frameNode, "offsetX", 0) + - imageset->getWidth() / 2 + 16; + const int offsetY = XML::getProperty(frameNode, "offsetY", 0) + - imageset->getHeight() + 32; const int rand = XML::getIntProperty( frameNode, "rand", 100, 0, 100); - offsetY -= imageset->getHeight() - 32; - offsetX -= imageset->getWidth() / 2 - 16; - if (xmlNameEqual(frameNode, "frame")) { const int index = XML::getProperty(frameNode, "index", -1); @@ -270,7 +270,6 @@ ParticleEmitter::ParticleEmitter(const XmlNodePtr emitterNode, while (end >= start) { Image *const img = imageset->get(start); - if (!img) { logger->log("No image at index %d", start); @@ -279,7 +278,7 @@ ParticleEmitter::ParticleEmitter(const XmlNodePtr emitterNode, mParticleRotation.addFrame(img, delay, offsetX, offsetY, rand); - start++; + start ++; } } else if (xmlNameEqual(frameNode, "end")) @@ -309,16 +308,15 @@ ParticleEmitter::ParticleEmitter(const XmlNodePtr emitterNode, const int delay = XML::getIntProperty( frameNode, "delay", 0, 0, 100000); const int offsetX = XML::getProperty(frameNode, "offsetX", 0) - - (imageset->getWidth() / 2 - 16); + - imageset->getWidth() / 2 + 16; const int offsetY = XML::getProperty(frameNode, "offsetY", 0) - - (imageset->getHeight() - 32); + - imageset->getHeight() + 32; const int rand = XML::getIntProperty( frameNode, "rand", 100, 0, 100); if (xmlNameEqual(frameNode, "frame")) { const int index = XML::getProperty(frameNode, "index", -1); - if (index < 0) { logger->log1("No valid value for 'index'"); @@ -468,7 +466,6 @@ ParticleEmitter::~ParticleEmitter() } } - template ParticleEmitterProp ParticleEmitter::readParticleEmitterProp(XmlNodePtr propertyNode, T def) { @@ -480,12 +477,13 @@ ParticleEmitter::readParticleEmitterProp(XmlNodePtr propertyNode, T def) static_cast(def))), static_cast(XML::getFloatProperty( propertyNode, "max", static_cast(def)))); - std::string change = XML::getProperty(propertyNode, "change-func", "none"); + const std::string change = XML::getProperty( + propertyNode, "change-func", "none"); T amplitude = static_cast(XML::getFloatProperty(propertyNode, "change-amplitude", 0.0)); - int period = XML::getProperty(propertyNode, "change-period", 0); - int phase = XML::getProperty(propertyNode, "change-phase", 0); + const int period = XML::getProperty(propertyNode, "change-period", 0); + const int phase = XML::getProperty(propertyNode, "change-phase", 0); if (change == "saw" || change == "sawtooth") retval.setFunction(FUNC_SAW, amplitude, period, phase); else if (change == "sine" || change == "sinewave") @@ -498,14 +496,13 @@ ParticleEmitter::readParticleEmitterProp(XmlNodePtr propertyNode, T def) return retval; } - std::list ParticleEmitter::createParticles(const int tick) { std::list newParticles; if (mOutputPauseLeft > 0) { - mOutputPauseLeft--; + mOutputPauseLeft --; return newParticles; } mOutputPauseLeft = mOutputPause.value(tick); @@ -519,7 +516,7 @@ std::list ParticleEmitter::createParticles(const int tick) Particle *newParticle; if (mParticleImage) { - std::string name = mParticleImage->getIdPath(); + const std::string name = mParticleImage->getIdPath(); if (ImageParticle::imageParticleCountByName.find(name) == ImageParticle::imageParticleCountByName.end()) { @@ -546,9 +543,9 @@ std::list ParticleEmitter::createParticles(const int tick) newParticle = new Particle(mMap); } - Vector position(mParticlePosX.value(tick), - mParticlePosY.value(tick), - mParticlePosZ.value(tick)); + const Vector position(mParticlePosX.value(tick), + mParticlePosY.value(tick), + mParticlePosZ.value(tick)); newParticle->moveTo(position); const float angleH = mParticleAngleHorizontal.value(tick); @@ -593,11 +590,9 @@ void ParticleEmitter::adjustSize(const int w, const int h) return; // new dimensions are illegal // calculate the old rectangle - const int oldWidth = static_cast( - mParticlePosX.maxVal - mParticlePosX.minVal); - const int oldHeight = static_cast( + const int oldArea = static_cast( + mParticlePosX.maxVal - mParticlePosX.minVal) * static_cast( mParticlePosX.maxVal - mParticlePosY.minVal); - const int oldArea = oldWidth * oldHeight; if (oldArea == 0) { //when the effect has no dimension it is diff --git a/src/particleemitterprop.h b/src/particleemitterprop.h index aad16c99a..644c66ce3 100644 --- a/src/particleemitterprop.h +++ b/src/particleemitterprop.h @@ -58,7 +58,8 @@ template struct ParticleEmitterProp final set(val, val); } - void setFunction(ChangeFunc func, T amplitude, int period, int phase) + void setFunction(ChangeFunc func, T amplitude, + const int period, const int phase) { changeFunc = func; changeAmplitude = amplitude; -- cgit v1.2.3-70-g09d2