summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-06-03 17:42:54 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-06-03 17:42:54 +0000
commit778ca33725a8bfac629b405f0bd26cfefaf47caf (patch)
tree0b20376964c8b60f30c2827b1790cb038740a00b /src
parent6276c1ff96e449e73e84977110334cd8957c31b6 (diff)
downloadmana-778ca33725a8bfac629b405f0bd26cfefaf47caf.tar.gz
mana-778ca33725a8bfac629b405f0bd26cfefaf47caf.tar.bz2
mana-778ca33725a8bfac629b405f0bd26cfefaf47caf.tar.xz
mana-778ca33725a8bfac629b405f0bd26cfefaf47caf.zip
Have ParticleEmitter load a possible particle image early on and don't cause a
crash when the image can't be found.
Diffstat (limited to 'src')
-rw-r--r--src/imageparticle.cpp6
-rw-r--r--src/imageparticle.h14
-rw-r--r--src/particle.cpp2
-rw-r--r--src/particle.h8
-rw-r--r--src/particleemitter.cpp58
-rw-r--r--src/particleemitter.h32
6 files changed, 87 insertions, 33 deletions
diff --git a/src/imageparticle.cpp b/src/imageparticle.cpp
index 410782bd..35cc21ad 100644
--- a/src/imageparticle.cpp
+++ b/src/imageparticle.cpp
@@ -31,6 +31,12 @@ ImageParticle::ImageParticle(Map *map, Image *image):
Particle(map),
mImage(image)
{
+ mImage->incRef();
+}
+
+ImageParticle::~ImageParticle()
+{
+ mImage->decRef();
}
void ImageParticle::draw(Graphics *graphics, int offsetX, int offsetY) const
diff --git a/src/imageparticle.h b/src/imageparticle.h
index afde7446..0ad515cc 100644
--- a/src/imageparticle.h
+++ b/src/imageparticle.h
@@ -29,12 +29,26 @@
class Image;
class Map;
+/**
+ * A particle that uses an image for its visualization.
+ */
class ImageParticle : public Particle
{
public:
+ /**
+ * Constructor. The image is reference counted by this particle.
+ *
+ * @param map the map this particle appears on
+ * @param image an Image instance, may not be NULL
+ */
ImageParticle(Map *map, Image *image);
/**
+ * Destructor.
+ */
+ ~ImageParticle();
+
+ /**
* Draws the particle image
*/
virtual void
diff --git a/src/particle.cpp b/src/particle.cpp
index f88b9c21..509c20ee 100644
--- a/src/particle.cpp
+++ b/src/particle.cpp
@@ -48,7 +48,7 @@ int Particle::fastPhysics = 0;
int Particle::emitterSkip = 1;
const float Particle::PARTICLE_SKY = 800.0f;
-Particle::Particle(Map *map) :
+Particle::Particle(Map *map):
mAlive(true),
mPosX(0.0f), mPosY(0.0f), mPosZ(0.0f),
mLifetimeLeft(-1),
diff --git a/src/particle.h b/src/particle.h
index 544a7f58..7a747a5f 100644
--- a/src/particle.h
+++ b/src/particle.h
@@ -54,8 +54,16 @@ class Particle : public Sprite
static int maxCount; /**< Maximum number of particles */
static int emitterSkip; /**< Duration of pause between two emitter updates in ticks */
+ /**
+ * Constructor.
+ *
+ * @param map the map this particle will add itself to, may be NULL
+ */
Particle(Map *map);
+ /**
+ * Destructor.
+ */
~Particle();
/**
diff --git a/src/particleemitter.cpp b/src/particleemitter.cpp
index 62ac6bd8..2387c652 100644
--- a/src/particleemitter.cpp
+++ b/src/particleemitter.cpp
@@ -18,6 +18,7 @@
* along with The Mana World; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
+ * $Id$
*/
#include "particleemitter.h"
@@ -37,29 +38,29 @@
#define SIN45 0.707106781f
#define DEG_RAD_FACTOR 0.017453293f
-ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map *map)
+ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map *map):
+ mParticleImage(0)
{
mMap = map;
mParticleTarget = target;
//initializing default values
- mParticlePosX.set (0.0f);
- mParticlePosY.set (0.0f);
- mParticlePosZ.set (0.0f);
- mParticleAngleHorizontal.set (0.0f);
- mParticleAngleVertical.set (0.0f);
- mParticlePower.set (0.0f);
- mParticleGravity.set (0.0f);
- mParticleRandomnes.set (0);
+ mParticlePosX.set(0.0f);
+ mParticlePosY.set(0.0f);
+ mParticlePosZ.set(0.0f);
+ mParticleAngleHorizontal.set(0.0f);
+ mParticleAngleVertical.set(0.0f);
+ mParticlePower.set(0.0f);
+ mParticleGravity.set(0.0f);
+ mParticleRandomnes.set(0);
mParticleBounce.set(0.0f);
- mParticleAcceleration.set (0.0f);
+ mParticleAcceleration.set(0.0f);
mParticleDieDistance.set(-1.0f);
- mParticleMomentum.set (1.0f);
- mParticleLifetime.set (-1);
- mParticleFadeOut.set (0);
- mParticleFadeIn.set (0);
- mOutput.set (1);
- mParticleImage = "";
+ mParticleMomentum.set(1.0f);
+ mParticleLifetime.set(-1);
+ mParticleFadeOut.set(0);
+ mParticleFadeIn.set(0);
+ mOutput.set(1);
for_each_xml_child_node(propertyNode, emitterNode)
{
@@ -82,7 +83,13 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map *
}
else if (name == "image")
{
- mParticleImage = XML::getProperty(propertyNode, "value", "");
+ std::string image = XML::getProperty(propertyNode, "value", "");
+ // Don't leak when multiple images are defined
+ if (image != "" && !mParticleImage)
+ {
+ ResourceManager *resman = ResourceManager::getInstance();
+ mParticleImage = resman->getImage(image);
+ }
}
else if (name == "horizontal-angle")
{
@@ -227,6 +234,15 @@ ParticleEmitter::ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map *
}
+ParticleEmitter::~ParticleEmitter()
+{
+ if (mParticleImage)
+ {
+ mParticleImage->decRef();
+ }
+}
+
+
template <typename T> MinMax<T>
ParticleEmitter::readMinMax(xmlNodePtr propertyNode, T def)
{
@@ -245,18 +261,16 @@ std::list<Particle *>
ParticleEmitter::createParticles()
{
std::list<Particle *> newParticles;
- ResourceManager *resman = ResourceManager::getInstance();
for (int i = mOutput.value(); i > 0; i--)
{
- //limit maximum particles
+ // Limit maximum particles
if (Particle::particleCount > Particle::maxCount) break;
Particle *newParticle;
- if (mParticleImage != "")
+ if (mParticleImage)
{
- newParticle = new ImageParticle(mMap,
- resman->getImage(mParticleImage));
+ newParticle = new ImageParticle(mMap, mParticleImage);
}
else if (mParticleAnimation.getLength() > 0)
{
diff --git a/src/particleemitter.h b/src/particleemitter.h
index dcc7069b..ca6d8622 100644
--- a/src/particleemitter.h
+++ b/src/particleemitter.h
@@ -18,6 +18,7 @@
* along with The Mana World; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
+ * $Id$
*/
#ifndef _PARTICLEEMITTER_H
@@ -30,6 +31,7 @@
#include "resources/animation.h"
+class Image;
class Map;
class Particle;
@@ -40,8 +42,15 @@ class Particle;
class ParticleEmitter
{
public:
+ /**
+ * Constructor.
+ */
+ ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map *map);
- ParticleEmitter(xmlNodePtr emitterNode, Particle *target, Map *map);
+ /**
+ * Destructor.
+ */
+ ~ParticleEmitter();
/**
* Spawns new particles
@@ -74,14 +83,14 @@ class ParticleEmitter
*/
MinMax<float> mParticlePower;
- /**
- * vector changing of particles:
+ /*
+ * Vector changing of particles:
*/
MinMax<float> mParticleGravity;
MinMax<int> mParticleRandomnes;
MinMax<float> mParticleBounce;
- /**
+ /*
* Properties of targeting particles:
*/
Particle *mParticleTarget;
@@ -89,20 +98,23 @@ class ParticleEmitter
MinMax<float> mParticleDieDistance;
MinMax<float> mParticleMomentum;
- /**
+ /*
* Behavior over time of the particles:
*/
MinMax<int> mParticleLifetime;
MinMax<int> mParticleFadeOut;
MinMax<int> mParticleFadeIn;
- Map *mMap; /**< Map the particles are supposed to spawn on */
+ Map *mMap; /**< Map the particles are spawned on */
+
+ MinMax<int> mOutput; /**< Number of particles spawned per update */
- MinMax<int> mOutput; /**< Number of particles spawned per update */
+ Image *mParticleImage; /**< Particle image, if used */
- std::string mParticleImage; /**< Filename of particle image */
- Animation mParticleAnimation; /**< Filename of particle animation file */
+ /** Filename of particle animation file */
+ Animation mParticleAnimation;
- std::list<ParticleEmitter> mParticleChildEmitters; /** List of emitters the spawned particles are equipped with */
+ /** List of emitters the spawned particles are equipped with */
+ std::list<ParticleEmitter> mParticleChildEmitters;
};
#endif