aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics/particles
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-02-11 11:29:07 +0800
committerchai <chaifix@163.com>2020-02-11 11:29:07 +0800
commit160e1299ef3d95f8e8c48706d7f61dd3dc6c6b60 (patch)
treeabe5ae5242d9cc6caf6edf103e662c44e978fca0 /src/libjin/graphics/particles
parente095043485d1d298571af6d9eca7f0db9009ea7a (diff)
*修改 tab大小HEADmaster
Diffstat (limited to 'src/libjin/graphics/particles')
-rw-r--r--src/libjin/graphics/particles/particle.cpp352
-rw-r--r--src/libjin/graphics/particles/particle.h410
-rw-r--r--src/libjin/graphics/particles/particle_emitter.cpp200
-rw-r--r--src/libjin/graphics/particles/particle_emitter.h246
-rw-r--r--src/libjin/graphics/particles/particle_pool.h20
-rw-r--r--src/libjin/graphics/particles/particle_system.cpp514
-rw-r--r--src/libjin/graphics/particles/particle_system.h334
7 files changed, 1038 insertions, 1038 deletions
diff --git a/src/libjin/graphics/particles/particle.cpp b/src/libjin/graphics/particles/particle.cpp
index 74600e0..ba07344 100644
--- a/src/libjin/graphics/particles/particle.cpp
+++ b/src/libjin/graphics/particles/particle.cpp
@@ -11,180 +11,180 @@ using namespace JinEngine::Math;
namespace JinEngine
{
- namespace Graphics
- {
- namespace Particles
- {
-
- static RandomGenerator rng(0xEA44944);
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // ScaledRangedValue
- //////////////////////////////////////////////////////////////////////////////////////////////////////
-
- ScaledRangedValue::ScaledRangedValue(Percentage* points, uint n)
- {
- for (int i = 0; i < n; ++i)
- {
- float x = points[2 * i].value;
- float y = points[2 * i + 1].value;
- addPoint(x, y);
- }
- }
-
- void ScaledRangedValue::set(Math::Percentage* points, uint n)
- {
- clear();
- for (int i = 0; i < n; ++i)
- {
- float x = points[2 * i].value;
- float y = points[2 * i + 1].value;
- addPoint(x, y);
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // GradientColorValue
- //////////////////////////////////////////////////////////////////////////////////////////////////////
-
- GradientColorValue::GradientColorValue()
- : mCount(0)
- {
- }
-
- void GradientColorValue::addColor(Color col, Percentage time)
- {
- mColors.push_back(col);
- mTimeline.push_back(time);
- ++mCount;
- }
-
- Color GradientColorValue::getColor(Percentage time)
- {
- int endIndex = -1;
- int n = mCount;
- for (int i = 1; i < n; i++) {
- Percentage t = mTimeline[i];
- if (t.value > time.value) {
- endIndex = i;
- break;
- }
- }
- if (endIndex == -1) return mColors[n - 1];
- int startIndex = endIndex - 1;
- Color& startValue = mColors[startIndex];
- Color& endValue = mColors[endIndex];
- float t = time.value;
- float start = mTimeline[startIndex].value;
- float end = mTimeline[endIndex].value;
- Color c;
- c.r = startValue.r + (endValue.r - startValue.r) * ((t - start) / (end - start));
- c.g = startValue.g + (endValue.g - startValue.g) * ((t - start) / (end - start));
- c.b = startValue.b + (endValue.b - startValue.b) * ((t - start) / (end - start));
- c.a = startValue.a + (endValue.a - startValue.a) * ((t - start) / (end - start));
- return c;
- }
-
- void GradientColorValue::insertColor(uint i, Color col, Math::Percentage time)
- {
- mColors.insert(mColors.begin() + i, col);
- mTimeline.insert(mTimeline.begin() + i, time);
- ++mCount;
- }
-
- void GradientColorValue::removeColor(uint i)
- {
- mColors.erase(mColors.begin() + i);
- mTimeline.erase(mTimeline.begin() + i);
- --mCount;
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // Particle
- //////////////////////////////////////////////////////////////////////////////////////////////////////
-
- Particle::Particle()
- {
- reset();
- }
-
- void Particle::reset()
- {
- updateFlags = ParticleUpdateMask::NONE;
- transform.set(0, 0, 1, 1, 0, 0, 0);
- lifeTime = 1.0f;
- life = 0.0f;
- velocity.set(0, 0);
- linearAcceleration.set(0, 0);
- radialAcceleration = 0.0f;
- angularSpeed = 0;
- color = Color::WHITE;
- alive = true;
- spriteIndex = 0;
- }
-
- void Particle::update(float dt)
- {
- life += dt;
- alive = life < lifeTime;
- if (!alive)
- return;
- float t = life / lifeTime;
- if ((updateFlags & UPDATE_COLOR) != 0)
- color = def->colorDef.overTime.value.getColor(t);
- // transparency
- if (def->transparencyDef.overTime.enable)
- color.a = 0xff * def->transparencyDef.overTime.value.getValue(t);
- else
- color.a = 0xff * def->transparencyDef.transparency.value;
- if ((updateFlags & UPDATE_SCALE) != 0)
- {
- // Lerp scale.
- float scale = def->scaleDef.overTime.value.getValue(t);
- transform.setScale(scale, scale);
- }
- if ((updateFlags & UPDATE_POSITION) != 0)
- {
- // Calculate position.
- if((updateFlags & UPDATE_VELOCITY) != 0)
- velocity += linearAcceleration * dt;
- transform.move(velocity * dt);
- }
- if ((updateFlags & UPDATE_ROTATION) != 0)
- {
- // Calculate rotation.
- angularSpeed += radialAcceleration * dt;
- transform.rotate(angularSpeed * dt);
- }
- if ((updateFlags & UPDATE_SPRITE) != 0)
- {
- int n = def->spritesDef.sprites.size();
- if (def->spritesDef.mode == SpriteMode::ANIMATED)
- spriteIndex = lerp<int>(0, n - 1, t);
- //jin_log_info("sprite index %d", spriteIndex);
- }
- }
-
- void Particle::render()
- {
- // Set blend.
- OpenGL::BlendMode blend = gl.getBlendMode();
- if(def->blendDef.additive)
- gl.setBlendMode(OpenGL::BlendMode::ADDITIVE);
- Color c = gl.getColor();
- gl.setColor(color);
- const Sprite* sprite = def->spritesDef.sprites[spriteIndex];
- if (sprite != nullptr)
- {
- Vector2<float>& position = transform.getPosition();
- Vector2<float>& scale = transform.getScale();
- float r = transform.getRotation();
- sprite->render(position.x(), position.y(), scale.x(), scale.y(), r);
- }
- gl.setColor(c);
- gl.setBlendMode(blend);
- }
-
- }
- }
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ static RandomGenerator rng(0xEA44944);
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ // ScaledRangedValue
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ ScaledRangedValue::ScaledRangedValue(Percentage* points, uint n)
+ {
+ for (int i = 0; i < n; ++i)
+ {
+ float x = points[2 * i].value;
+ float y = points[2 * i + 1].value;
+ addPoint(x, y);
+ }
+ }
+
+ void ScaledRangedValue::set(Math::Percentage* points, uint n)
+ {
+ clear();
+ for (int i = 0; i < n; ++i)
+ {
+ float x = points[2 * i].value;
+ float y = points[2 * i + 1].value;
+ addPoint(x, y);
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ // GradientColorValue
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ GradientColorValue::GradientColorValue()
+ : mCount(0)
+ {
+ }
+
+ void GradientColorValue::addColor(Color col, Percentage time)
+ {
+ mColors.push_back(col);
+ mTimeline.push_back(time);
+ ++mCount;
+ }
+
+ Color GradientColorValue::getColor(Percentage time)
+ {
+ int endIndex = -1;
+ int n = mCount;
+ for (int i = 1; i < n; i++) {
+ Percentage t = mTimeline[i];
+ if (t.value > time.value) {
+ endIndex = i;
+ break;
+ }
+ }
+ if (endIndex == -1) return mColors[n - 1];
+ int startIndex = endIndex - 1;
+ Color& startValue = mColors[startIndex];
+ Color& endValue = mColors[endIndex];
+ float t = time.value;
+ float start = mTimeline[startIndex].value;
+ float end = mTimeline[endIndex].value;
+ Color c;
+ c.r = startValue.r + (endValue.r - startValue.r) * ((t - start) / (end - start));
+ c.g = startValue.g + (endValue.g - startValue.g) * ((t - start) / (end - start));
+ c.b = startValue.b + (endValue.b - startValue.b) * ((t - start) / (end - start));
+ c.a = startValue.a + (endValue.a - startValue.a) * ((t - start) / (end - start));
+ return c;
+ }
+
+ void GradientColorValue::insertColor(uint i, Color col, Math::Percentage time)
+ {
+ mColors.insert(mColors.begin() + i, col);
+ mTimeline.insert(mTimeline.begin() + i, time);
+ ++mCount;
+ }
+
+ void GradientColorValue::removeColor(uint i)
+ {
+ mColors.erase(mColors.begin() + i);
+ mTimeline.erase(mTimeline.begin() + i);
+ --mCount;
+ }
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ // Particle
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ Particle::Particle()
+ {
+ reset();
+ }
+
+ void Particle::reset()
+ {
+ updateFlags = ParticleUpdateMask::NONE;
+ transform.set(0, 0, 1, 1, 0, 0, 0);
+ lifeTime = 1.0f;
+ life = 0.0f;
+ velocity.set(0, 0);
+ linearAcceleration.set(0, 0);
+ radialAcceleration = 0.0f;
+ angularSpeed = 0;
+ color = Color::WHITE;
+ alive = true;
+ spriteIndex = 0;
+ }
+
+ void Particle::update(float dt)
+ {
+ life += dt;
+ alive = life < lifeTime;
+ if (!alive)
+ return;
+ float t = life / lifeTime;
+ if ((updateFlags & UPDATE_COLOR) != 0)
+ color = def->colorDef.overTime.value.getColor(t);
+ // transparency
+ if (def->transparencyDef.overTime.enable)
+ color.a = 0xff * def->transparencyDef.overTime.value.getValue(t);
+ else
+ color.a = 0xff * def->transparencyDef.transparency.value;
+ if ((updateFlags & UPDATE_SCALE) != 0)
+ {
+ // Lerp scale.
+ float scale = def->scaleDef.overTime.value.getValue(t);
+ transform.setScale(scale, scale);
+ }
+ if ((updateFlags & UPDATE_POSITION) != 0)
+ {
+ // Calculate position.
+ if((updateFlags & UPDATE_VELOCITY) != 0)
+ velocity += linearAcceleration * dt;
+ transform.move(velocity * dt);
+ }
+ if ((updateFlags & UPDATE_ROTATION) != 0)
+ {
+ // Calculate rotation.
+ angularSpeed += radialAcceleration * dt;
+ transform.rotate(angularSpeed * dt);
+ }
+ if ((updateFlags & UPDATE_SPRITE) != 0)
+ {
+ int n = def->spritesDef.sprites.size();
+ if (def->spritesDef.mode == SpriteMode::ANIMATED)
+ spriteIndex = lerp<int>(0, n - 1, t);
+ //jin_log_info("sprite index %d", spriteIndex);
+ }
+ }
+
+ void Particle::render()
+ {
+ // Set blend.
+ OpenGL::BlendMode blend = gl.getBlendMode();
+ if(def->blendDef.additive)
+ gl.setBlendMode(OpenGL::BlendMode::ADDITIVE);
+ Color c = gl.getColor();
+ gl.setColor(color);
+ const Sprite* sprite = def->spritesDef.sprites[spriteIndex];
+ if (sprite != nullptr)
+ {
+ Vector2<float>& position = transform.getPosition();
+ Vector2<float>& scale = transform.getScale();
+ float r = transform.getRotation();
+ sprite->render(position.x(), position.y(), scale.x(), scale.y(), r);
+ }
+ gl.setColor(c);
+ gl.setBlendMode(blend);
+ }
+
+ }
+ }
} \ No newline at end of file
diff --git a/src/libjin/graphics/particles/particle.h b/src/libjin/graphics/particles/particle.h
index 081ad26..25db6ae 100644
--- a/src/libjin/graphics/particles/particle.h
+++ b/src/libjin/graphics/particles/particle.h
@@ -12,211 +12,211 @@
namespace JinEngine
{
- namespace Graphics
- {
- namespace Particles
- {
-
- class ParticleEmitter;
-
- class ScaledRangedValue : public Math::RangedValue
- {
- public:
- ScaledRangedValue() {};
- ScaledRangedValue(Math::Percentage* points, uint n);
- void set(Math::Percentage* points, uint n);
-
- };
-
- class GradientColorValue
- {
- public:
- GradientColorValue();
-
- void addColor(Color col, Math::Percentage time);
- Color getColor(Math::Percentage time);
- void insertColor(uint i, Color col, Math::Percentage time);
- void removeColor(uint i);
-
- private:
- std::vector<Color> mColors;
- std::vector<Math::Percentage> mTimeline;
- uint mCount;
-
- };
-
- ///
- ///
- ///
- struct LifeTimeDef
- {
- bool enableRandom = false;
- Struct(life,
- struct
- {
- float floor, ceil;
- } random;
- float life = 1.0f;
- );
- };
-
- struct ScaleDef
- {
- float scale = 1;
- Struct(overTime,
- bool enable = false;
- ScaledRangedValue value;
- );
- };
-
- struct ColorDef
- {
- Color color = Color::WHITE;
- Struct(overTime,
- bool enable = false;
- GradientColorValue value;
- );
- };
-
- struct TransparencyDef
- {
- Math::Percentage transparency = 1.f;
- Struct(overTime,
- bool enable = false;
- ScaledRangedValue value;
- );
- };
-
- struct linearAccelarationDef
- {
- Math::Vector2<float> linearAccelaration;
- };
-
- struct RadialAccelarationDef
- {
- float radialAccelaration = 0.f;
- };
-
- struct AngularSpeedDef
- {
- bool enableRandom = false;
- Struct(angularSpeed,
- struct
- {
- float floor = 0;
- float ceil = 0;
- } random;
- float angularSpeed = 0;
- );
- };
-
- enum SpriteMode
- {
- SINGLE = 1,
- RANDOM = 2,
- ANIMATED = 3,
- };
-
- struct SpritesDef
- {
- SpriteMode mode = SpriteMode::SINGLE;
- std::vector<const Sprite*> sprites;
- };
-
- struct BlendDef
- {
- bool additive = false;
- };
-
- ///
- ///
- ///
- struct ParticleDef
- {
- public:
- // Basic definitions.
- LifeTimeDef lifeTimeDef; ///<
- linearAccelarationDef linearAccelarationDef; ///<
- RadialAccelarationDef radialAccelarationDef; ///<
- AngularSpeedDef angularSpeedDef; ///<
- SpritesDef spritesDef; ///<
- BlendDef blendDef; ///<
- // Optional definitions.
- ScaleDef scaleDef; ///<
- ColorDef colorDef; ///<
- TransparencyDef transparencyDef; ///<
-
- private:
- friend class ParticleEmitter;
-
- };
-
- ///
- /// A single particle contains various properties of particle, such as position, accelaration, color
- /// and other attributes changed over time.
- ///
- class Particle : public Renderable, public Game::GameObject
- {
- public:
- enum ParticleUpdateMask
- {
- NONE = 0,
- UPDATE_COLOR = 1 << 0,
- UPDATE_SCALE = 1 << 1,
- UPDATE_POSITION = 1 << 2,
- UPDATE_ROTATION = 1 << 3,
- UPDATE_SPRITE = 1 << 4,
- UPDATE_VELOCITY = 1 << 5,
- };
-
- ///
- /// Default constructor.
- ///
- Particle();
-
- ///
- /// Reset to default.
- ///
- void reset();
-
- ///
- ///
- ///
- void update(float dt);
-
- ///
- ///
- ///
- void render();
-
- // Methods end.
-
- ParticleDef* def;
-
- uint updateFlags = ParticleUpdateMask::NONE;
-
- float lifeTime = 1.0f;
-
- float life = 0.0f;
-
- int spriteIndex = 0;
-
- Color color = Color::WHITE;
-
- Math::Transform transform;
-
- Math::Vector2<float> velocity;
- Math::Vector2<float> linearAcceleration;
-
- float angularSpeed;
- float radialAcceleration = 0;
-
- bool alive = true;
-
- };
-
- } // namespace Particles
- } // namespace Graphics
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ class ParticleEmitter;
+
+ class ScaledRangedValue : public Math::RangedValue
+ {
+ public:
+ ScaledRangedValue() {};
+ ScaledRangedValue(Math::Percentage* points, uint n);
+ void set(Math::Percentage* points, uint n);
+
+ };
+
+ class GradientColorValue
+ {
+ public:
+ GradientColorValue();
+
+ void addColor(Color col, Math::Percentage time);
+ Color getColor(Math::Percentage time);
+ void insertColor(uint i, Color col, Math::Percentage time);
+ void removeColor(uint i);
+
+ private:
+ std::vector<Color> mColors;
+ std::vector<Math::Percentage> mTimeline;
+ uint mCount;
+
+ };
+
+ ///
+ ///
+ ///
+ struct LifeTimeDef
+ {
+ bool enableRandom = false;
+ Struct(life,
+ struct
+ {
+ float floor, ceil;
+ } random;
+ float life = 1.0f;
+ );
+ };
+
+ struct ScaleDef
+ {
+ float scale = 1;
+ Struct(overTime,
+ bool enable = false;
+ ScaledRangedValue value;
+ );
+ };
+
+ struct ColorDef
+ {
+ Color color = Color::WHITE;
+ Struct(overTime,
+ bool enable = false;
+ GradientColorValue value;
+ );
+ };
+
+ struct TransparencyDef
+ {
+ Math::Percentage transparency = 1.f;
+ Struct(overTime,
+ bool enable = false;
+ ScaledRangedValue value;
+ );
+ };
+
+ struct linearAccelarationDef
+ {
+ Math::Vector2<float> linearAccelaration;
+ };
+
+ struct RadialAccelarationDef
+ {
+ float radialAccelaration = 0.f;
+ };
+
+ struct AngularSpeedDef
+ {
+ bool enableRandom = false;
+ Struct(angularSpeed,
+ struct
+ {
+ float floor = 0;
+ float ceil = 0;
+ } random;
+ float angularSpeed = 0;
+ );
+ };
+
+ enum SpriteMode
+ {
+ SINGLE = 1,
+ RANDOM = 2,
+ ANIMATED = 3,
+ };
+
+ struct SpritesDef
+ {
+ SpriteMode mode = SpriteMode::SINGLE;
+ std::vector<const Sprite*> sprites;
+ };
+
+ struct BlendDef
+ {
+ bool additive = false;
+ };
+
+ ///
+ ///
+ ///
+ struct ParticleDef
+ {
+ public:
+ // Basic definitions.
+ LifeTimeDef lifeTimeDef; ///<
+ linearAccelarationDef linearAccelarationDef; ///<
+ RadialAccelarationDef radialAccelarationDef; ///<
+ AngularSpeedDef angularSpeedDef; ///<
+ SpritesDef spritesDef; ///<
+ BlendDef blendDef; ///<
+ // Optional definitions.
+ ScaleDef scaleDef; ///<
+ ColorDef colorDef; ///<
+ TransparencyDef transparencyDef; ///<
+
+ private:
+ friend class ParticleEmitter;
+
+ };
+
+ ///
+ /// A single particle contains various properties of particle, such as position, accelaration, color
+ /// and other attributes changed over time.
+ ///
+ class Particle : public Renderable, public Game::GameObject
+ {
+ public:
+ enum ParticleUpdateMask
+ {
+ NONE = 0,
+ UPDATE_COLOR = 1 << 0,
+ UPDATE_SCALE = 1 << 1,
+ UPDATE_POSITION = 1 << 2,
+ UPDATE_ROTATION = 1 << 3,
+ UPDATE_SPRITE = 1 << 4,
+ UPDATE_VELOCITY = 1 << 5,
+ };
+
+ ///
+ /// Default constructor.
+ ///
+ Particle();
+
+ ///
+ /// Reset to default.
+ ///
+ void reset();
+
+ ///
+ ///
+ ///
+ void update(float dt);
+
+ ///
+ ///
+ ///
+ void render();
+
+ // Methods end.
+
+ ParticleDef* def;
+
+ uint updateFlags = ParticleUpdateMask::NONE;
+
+ float lifeTime = 1.0f;
+
+ float life = 0.0f;
+
+ int spriteIndex = 0;
+
+ Color color = Color::WHITE;
+
+ Math::Transform transform;
+
+ Math::Vector2<float> velocity;
+ Math::Vector2<float> linearAcceleration;
+
+ float angularSpeed;
+ float radialAcceleration = 0;
+
+ bool alive = true;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
} // namespace JinEngine
#endif \ No newline at end of file
diff --git a/src/libjin/graphics/particles/particle_emitter.cpp b/src/libjin/graphics/particles/particle_emitter.cpp
index 725b826..56dfb23 100644
--- a/src/libjin/graphics/particles/particle_emitter.cpp
+++ b/src/libjin/graphics/particles/particle_emitter.cpp
@@ -9,110 +9,110 @@ using namespace JinEngine::Math;
namespace JinEngine
{
- namespace Graphics
- {
- namespace Particles
- {
+ namespace Graphics
+ {
+ namespace Particles
+ {
- static const uint8 ACCURACY_4 = 4;
- static const uint8 ACCURACY_5 = 5;
- static const uint8 ACCURACY_6 = 6;
+ static const uint8 ACCURACY_4 = 4;
+ static const uint8 ACCURACY_5 = 5;
+ static const uint8 ACCURACY_6 = 6;
- // Particle emitter
- static RandomGenerator rng(0xEA44944);
+ // Particle emitter
+ static RandomGenerator rng(0xEA44944);
- ParticleEmitter::ParticleEmitter(ParticleSystem& ps)
- : mPS(ps)
- , mDef(ps.mDef.emitterDef)
- , mPDef(ps.mDef.particleDef)
- , mTime(0)
- {
- }
+ ParticleEmitter::ParticleEmitter(ParticleSystem& ps)
+ : mPS(ps)
+ , mDef(ps.mDef.emitterDef)
+ , mPDef(ps.mDef.particleDef)
+ , mTime(0)
+ {
+ }
- void ParticleEmitter::update(float dt)
- {
- mTime += dt;
- for (;mTime >= mInterval; mTime -= mInterval)
- {
- emit();
- // Random rate.
- if (mDef.emitRateDef.enableRandom)
- mInterval = rng.randf(mDef.emitRateDef.rate.random.floor, mDef.emitRateDef.rate.random.ceil, ACCURACY_5);
- else
- mInterval = mDef.emitRateDef.rate.rate;
- }
- }
+ void ParticleEmitter::update(float dt)
+ {
+ mTime += dt;
+ for (;mTime >= mInterval; mTime -= mInterval)
+ {
+ emit();
+ // Random rate.
+ if (mDef.emitRateDef.enableRandom)
+ mInterval = rng.randf(mDef.emitRateDef.rate.random.floor, mDef.emitRateDef.rate.random.ceil, ACCURACY_5);
+ else
+ mInterval = mDef.emitRateDef.rate.rate;
+ }
+ }
- void ParticleEmitter::emit()
- {
- Particle* p = mPS.claim();
- if (p == nullptr)
- return;
- p->reset();
- p->def = &mPDef;
- // Init position.
- if (mDef.positionDef.enableRandom)
- {
- float x = rng.randf(mDef.positionDef.position.random.floor.x(), mDef.positionDef.position.random.ceil.x(), ACCURACY_4);
- float y = rng.randf(mDef.positionDef.position.random.floor.y(), mDef.positionDef.position.random.ceil.y(), ACCURACY_4);
- x += mPS.mPosition.x();
- y += mPS.mPosition.y();
- p->transform.setPosition(x, y);
- }
- else
- {
- p->transform.setPosition(mDef.positionDef.position.position + mPS.mPosition);
- }
- // Init speed.
- float r = 0;
- if (mDef.directionDef.enableRandom)
- r = rng.randf(mDef.directionDef.direction.random.floor, mDef.directionDef.direction.random.ceil, ACCURACY_4);
- else
- r = mDef.directionDef.direction.direction;
- float f = 0;
- if (mDef.forceDef.enableRandom)
- f = rng.randf(mDef.forceDef.force.random.floor, mDef.forceDef.force.random.ceil, ACCURACY_4);
- else
- f = mDef.forceDef.force.force;
- p->velocity.set(f*cos(r), f*sin(r));
- if (f != 0) p->updateFlags |= Particle::UPDATE_POSITION;
- // Init life time.
- if (mPDef.lifeTimeDef.enableRandom)
- p->lifeTime = rng.randf(mPDef.lifeTimeDef.life.random.floor, mPDef.lifeTimeDef.life.random.ceil, ACCURACY_4);
- else
- p->lifeTime = mPDef.lifeTimeDef.life.life;
- // Init linear accelaration.
- p->linearAcceleration = mPDef.linearAccelarationDef.linearAccelaration;
- if(!p->linearAcceleration.isZero())
- p->updateFlags |= (Particle::UPDATE_VELOCITY | Particle::UPDATE_POSITION);
- // Init angular accelaration.
- p->radialAcceleration = mPDef.radialAccelarationDef.radialAccelaration;
- // Init Angular speed.
- if (mPDef.angularSpeedDef.enableRandom)
- p->angularSpeed = rng.randf(mPDef.angularSpeedDef.angularSpeed.random.floor, mPDef.angularSpeedDef.angularSpeed.random.ceil, ACCURACY_4);
- else
- p->angularSpeed = mPDef.angularSpeedDef.angularSpeed.angularSpeed;
- if (p->angularSpeed != 0)
- p->updateFlags |= Particle::UPDATE_ROTATION;
- // Scale over time.
- if (mPDef.scaleDef.overTime.enable)
- p->updateFlags |= Particle::UPDATE_SCALE;
- else
- p->transform.setScale(mPDef.scaleDef.scale, mPDef.scaleDef.scale);
- // Color over time.
- if (mPDef.colorDef.overTime.enable)
- p->updateFlags |= Particle::UPDATE_COLOR;
- else
- p->color = mPDef.colorDef.color;
- // Sprite
- if (mPDef.spritesDef.mode != SpriteMode::SINGLE)
- {
- p->updateFlags |= Particle::UPDATE_SPRITE;
- if (mPDef.spritesDef.mode == SpriteMode::RANDOM)
- p->spriteIndex = rng.rand(0, mPDef.spritesDef.sprites.size() - 1);
- }
- }
+ void ParticleEmitter::emit()
+ {
+ Particle* p = mPS.claim();
+ if (p == nullptr)
+ return;
+ p->reset();
+ p->def = &mPDef;
+ // Init position.
+ if (mDef.positionDef.enableRandom)
+ {
+ float x = rng.randf(mDef.positionDef.position.random.floor.x(), mDef.positionDef.position.random.ceil.x(), ACCURACY_4);
+ float y = rng.randf(mDef.positionDef.position.random.floor.y(), mDef.positionDef.position.random.ceil.y(), ACCURACY_4);
+ x += mPS.mPosition.x();
+ y += mPS.mPosition.y();
+ p->transform.setPosition(x, y);
+ }
+ else
+ {
+ p->transform.setPosition(mDef.positionDef.position.position + mPS.mPosition);
+ }
+ // Init speed.
+ float r = 0;
+ if (mDef.directionDef.enableRandom)
+ r = rng.randf(mDef.directionDef.direction.random.floor, mDef.directionDef.direction.random.ceil, ACCURACY_4);
+ else
+ r = mDef.directionDef.direction.direction;
+ float f = 0;
+ if (mDef.forceDef.enableRandom)
+ f = rng.randf(mDef.forceDef.force.random.floor, mDef.forceDef.force.random.ceil, ACCURACY_4);
+ else
+ f = mDef.forceDef.force.force;
+ p->velocity.set(f*cos(r), f*sin(r));
+ if (f != 0) p->updateFlags |= Particle::UPDATE_POSITION;
+ // Init life time.
+ if (mPDef.lifeTimeDef.enableRandom)
+ p->lifeTime = rng.randf(mPDef.lifeTimeDef.life.random.floor, mPDef.lifeTimeDef.life.random.ceil, ACCURACY_4);
+ else
+ p->lifeTime = mPDef.lifeTimeDef.life.life;
+ // Init linear accelaration.
+ p->linearAcceleration = mPDef.linearAccelarationDef.linearAccelaration;
+ if(!p->linearAcceleration.isZero())
+ p->updateFlags |= (Particle::UPDATE_VELOCITY | Particle::UPDATE_POSITION);
+ // Init angular accelaration.
+ p->radialAcceleration = mPDef.radialAccelarationDef.radialAccelaration;
+ // Init Angular speed.
+ if (mPDef.angularSpeedDef.enableRandom)
+ p->angularSpeed = rng.randf(mPDef.angularSpeedDef.angularSpeed.random.floor, mPDef.angularSpeedDef.angularSpeed.random.ceil, ACCURACY_4);
+ else
+ p->angularSpeed = mPDef.angularSpeedDef.angularSpeed.angularSpeed;
+ if (p->angularSpeed != 0)
+ p->updateFlags |= Particle::UPDATE_ROTATION;
+ // Scale over time.
+ if (mPDef.scaleDef.overTime.enable)
+ p->updateFlags |= Particle::UPDATE_SCALE;
+ else
+ p->transform.setScale(mPDef.scaleDef.scale, mPDef.scaleDef.scale);
+ // Color over time.
+ if (mPDef.colorDef.overTime.enable)
+ p->updateFlags |= Particle::UPDATE_COLOR;
+ else
+ p->color = mPDef.colorDef.color;
+ // Sprite
+ if (mPDef.spritesDef.mode != SpriteMode::SINGLE)
+ {
+ p->updateFlags |= Particle::UPDATE_SPRITE;
+ if (mPDef.spritesDef.mode == SpriteMode::RANDOM)
+ p->spriteIndex = rng.rand(0, mPDef.spritesDef.sprites.size() - 1);
+ }
+ }
- }
- }
+ }
+ }
} \ No newline at end of file
diff --git a/src/libjin/graphics/particles/particle_emitter.h b/src/libjin/graphics/particles/particle_emitter.h
index 9f9465a..8e386da 100644
--- a/src/libjin/graphics/particles/particle_emitter.h
+++ b/src/libjin/graphics/particles/particle_emitter.h
@@ -8,129 +8,129 @@
namespace JinEngine
{
- namespace Graphics
- {
- namespace Particles
- {
-
- struct PositionDef
- {
- bool enableRandom = false;
- Struct(position,
- struct
- {
- Math::Vector2<float> floor;
- Math::Vector2<float> ceil;
- } random;
- Math::Vector2<float> position;
- );
- };
-
- struct DirectionDef
- {
- bool enableRandom = false;
- Struct(direction,
- struct
- {
- float floor = 0;
- float ceil = 0;
- } random;
- float direction = 0;
- );
- };
-
- ///
- /// How many particles emitted per second.
- ///
- struct EmitRateDef
- {
- bool enableRandom = false;
- Struct(rate,
- struct
- {
- float floor = 1;
- float ceil = 1;
- } random;
- float rate = 1;
- );
- };
-
- ///
- /// Initial speed of particle.
- ///
- struct ForceDef
- {
- bool enableRandom = false;
- Struct(force,
- struct
- {
- float floor = 1;
- float ceil = 1;
- } random;
- float force = 1;
- );
- };
-
- ///
- /// Definition of particle emitter.
- ///
- struct ParticleEmitterDef
- {
- EmitRateDef emitRateDef; ///< Emit rate.
-
- PositionDef positionDef; ///< Emit position(relativily to the particle system center).
- DirectionDef directionDef; ///< Emit direction.
- ForceDef forceDef; ///< Emit force.
- };
-
- class ParticleSystem;
-
- ///
- /// Emit a single particle.
- ///
- class ParticleEmitter
- {
- public:
- ///
- ///
- ///
- ParticleEmitter(ParticleSystem& ps);
-
- ///
- ///
- ///
- void update(float dt);
-
- private:
- ///
- ///
- ///
- ParticleSystem& mPS;
-
- ParticleEmitterDef& mDef;
-
- ParticleDef& mPDef;
-
- ///
- /// Emit particle according to emitter definition and particle definition, particle system should
- /// assign particle value to the particle in particle pool, but not use this return particle.
- ///
- void emit();
-
- ///
- ///
- ///
- float mTime;
-
- ///
- ///
- ///
- float mInterval;
-
- };
-
- } // namespace Particles
- } // namespace Graphics
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ struct PositionDef
+ {
+ bool enableRandom = false;
+ Struct(position,
+ struct
+ {
+ Math::Vector2<float> floor;
+ Math::Vector2<float> ceil;
+ } random;
+ Math::Vector2<float> position;
+ );
+ };
+
+ struct DirectionDef
+ {
+ bool enableRandom = false;
+ Struct(direction,
+ struct
+ {
+ float floor = 0;
+ float ceil = 0;
+ } random;
+ float direction = 0;
+ );
+ };
+
+ ///
+ /// How many particles emitted per second.
+ ///
+ struct EmitRateDef
+ {
+ bool enableRandom = false;
+ Struct(rate,
+ struct
+ {
+ float floor = 1;
+ float ceil = 1;
+ } random;
+ float rate = 1;
+ );
+ };
+
+ ///
+ /// Initial speed of particle.
+ ///
+ struct ForceDef
+ {
+ bool enableRandom = false;
+ Struct(force,
+ struct
+ {
+ float floor = 1;
+ float ceil = 1;
+ } random;
+ float force = 1;
+ );
+ };
+
+ ///
+ /// Definition of particle emitter.
+ ///
+ struct ParticleEmitterDef
+ {
+ EmitRateDef emitRateDef; ///< Emit rate.
+
+ PositionDef positionDef; ///< Emit position(relativily to the particle system center).
+ DirectionDef directionDef; ///< Emit direction.
+ ForceDef forceDef; ///< Emit force.
+ };
+
+ class ParticleSystem;
+
+ ///
+ /// Emit a single particle.
+ ///
+ class ParticleEmitter
+ {
+ public:
+ ///
+ ///
+ ///
+ ParticleEmitter(ParticleSystem& ps);
+
+ ///
+ ///
+ ///
+ void update(float dt);
+
+ private:
+ ///
+ ///
+ ///
+ ParticleSystem& mPS;
+
+ ParticleEmitterDef& mDef;
+
+ ParticleDef& mPDef;
+
+ ///
+ /// Emit particle according to emitter definition and particle definition, particle system should
+ /// assign particle value to the particle in particle pool, but not use this return particle.
+ ///
+ void emit();
+
+ ///
+ ///
+ ///
+ float mTime;
+
+ ///
+ ///
+ ///
+ float mInterval;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
} // namespace JinEngine
#endif \ No newline at end of file
diff --git a/src/libjin/graphics/particles/particle_pool.h b/src/libjin/graphics/particles/particle_pool.h
index 618a965..b72a1ab 100644
--- a/src/libjin/graphics/particles/particle_pool.h
+++ b/src/libjin/graphics/particles/particle_pool.h
@@ -7,18 +7,18 @@
namespace JinEngine
{
- namespace Graphics
- {
- namespace Particles
- {
+ namespace Graphics
+ {
+ namespace Particles
+ {
- ///
- /// Particle pool for reducing memory fragmentation.
- ///
- typedef Pool<Particle> ParticlePool;
+ ///
+ /// Particle pool for reducing memory fragmentation.
+ ///
+ typedef Pool<Particle> ParticlePool;
- } // namespace Particles
- } // namespace Graphics
+ } // namespace Particles
+ } // namespace Graphics
} // namespace JinEngine
#endif \ No newline at end of file
diff --git a/src/libjin/graphics/particles/particle_system.cpp b/src/libjin/graphics/particles/particle_system.cpp
index 984f42b..2552c5a 100644
--- a/src/libjin/graphics/particles/particle_system.cpp
+++ b/src/libjin/graphics/particles/particle_system.cpp
@@ -6,261 +6,261 @@
namespace JinEngine
{
- namespace Graphics
- {
- namespace Particles
- {
-
- ParticleSystem::ParticleSystem(uint maxCount)
- : mEmitter(*this)
- , mParticlePool(maxCount, sizeof(Particle))
- {
- }
-
- ParticleSystem::ParticleSystem(const ParticleSystemDef& def)
- : mDef(def)
- , mEmitter(*this)
- , mParticlePool(def.maxParticleCount, sizeof(Particle))
- {
- }
-
- ParticleSystem::~ParticleSystem()
- {
- }
-
- void ParticleSystem::setPosition(float x, float y)
- {
- mPosition.x() = x;
- mPosition.y() = y;
- }
-
- void ParticleSystem::update(float dt)
- {
- mEmitter.update(dt);
- for (int i = 0; i < mAliveParticles.size(); ++i)
- {
- Particle* p = mAliveParticles[i];
- if (p->alive == false)
- {
- recycle(i, p);
- --i;
- }
- else
- {
- p->update(dt);
- }
- }
- }
-
- void ParticleSystem::render()
- {
- for (Particle* p : mAliveParticles)
- p->render();
- }
-
- Particle* ParticleSystem::claim()
- {
- Particle* p = new (mParticlePool.GetNextWithoutInitializing()) Particle();
- mAliveParticles.push_back(p);
- //jin_log_info("particle count %d", mAliveParticles.size());
- return p;
- }
-
- void ParticleSystem::recycle(int i, Particle* p)
- {
- if (i >= mAliveParticles.size())
- return;
- mAliveParticles.erase(mAliveParticles.begin() + i);
- mParticlePool.Delete(p);
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // Particle Emitter modification.
- //////////////////////////////////////////////////////////////////////////////////////////////////////
-
- void ParticleSystem::setEmitRate(float floor, float ceil)
- {
- mDef.emitterDef.emitRateDef.enableRandom = true;
- mDef.emitterDef.emitRateDef.rate.random.floor = floor;
- mDef.emitterDef.emitRateDef.rate.random.ceil = ceil;
- }
-
- void ParticleSystem::setEmitRate(float rate)
- {
- mDef.emitterDef.emitRateDef.enableRandom = false;
- mDef.emitterDef.emitRateDef.rate.rate = rate;
- }
-
- void ParticleSystem::setEmitForce(float floor, float ceil)
- {
- mDef.emitterDef.forceDef.enableRandom = true;
- mDef.emitterDef.forceDef.force.random.floor = floor;
- mDef.emitterDef.forceDef.force.random.ceil = ceil;
- }
-
- void ParticleSystem::setEmitForce(float force)
- {
- mDef.emitterDef.forceDef.enableRandom = false;
- mDef.emitterDef.forceDef.force.force = force;
- }
-
- void ParticleSystem::setEmitDirection(float floor, float ceil)
- {
- mDef.emitterDef.directionDef.enableRandom = true;
- mDef.emitterDef.directionDef.direction.random.floor = floor;
- mDef.emitterDef.directionDef.direction.random.ceil = ceil;
- }
-
- void ParticleSystem::setEmitDirection(float dir)
- {
- mDef.emitterDef.directionDef.enableRandom = false;
- mDef.emitterDef.directionDef.direction.direction = dir;
- }
-
- void ParticleSystem::setEmitPosition(const Math::Vector2<float>& floor, const Math::Vector2<float>& ceil)
- {
- mDef.emitterDef.positionDef.enableRandom = true;
- mDef.emitterDef.positionDef.position.random.floor = floor;
- mDef.emitterDef.positionDef.position.random.ceil = ceil;
- }
-
- void ParticleSystem::setEmitPosition(const Math::Vector2<float>& position)
- {
- mDef.emitterDef.positionDef.enableRandom = false;
- mDef.emitterDef.positionDef.position.position = position;
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // Particle Emitter modification.
- //////////////////////////////////////////////////////////////////////////////////////////////////////
-
- void ParticleSystem::setParticleLife(float floor, float ceil)
- {
- mDef.particleDef.lifeTimeDef.enableRandom = true;
- mDef.particleDef.lifeTimeDef.life.random.floor = floor;
- mDef.particleDef.lifeTimeDef.life.random.ceil = ceil;
- }
-
- void ParticleSystem::setParticleLife(float time)
- {
- mDef.particleDef.lifeTimeDef.enableRandom = true;
- mDef.particleDef.lifeTimeDef.life.life = time;
- }
-
- void ParticleSystem::setParticleLinearAccelaration(Math::Vector2<float> ac)
- {
- mDef.particleDef.linearAccelarationDef.linearAccelaration = ac;
- }
-
- void ParticleSystem::setParticleRadialAccelaration(float ra)
- {
- mDef.particleDef.radialAccelarationDef.radialAccelaration = ra;
- }
-
- void ParticleSystem::setParticleAngularSpeed(float floor, float ceil)
- {
- mDef.particleDef.angularSpeedDef.enableRandom = true;
- mDef.particleDef.angularSpeedDef.angularSpeed.random.floor = floor;
- mDef.particleDef.angularSpeedDef.angularSpeed.random.ceil = ceil;
- }
-
- void ParticleSystem::setParticleAngularSpeed(float speed)
- {
- mDef.particleDef.angularSpeedDef.enableRandom = false;
- mDef.particleDef.angularSpeedDef.angularSpeed.angularSpeed = speed;
- }
-
- void ParticleSystem::setParticleSpritesMode(SpriteMode mode)
- {
- mDef.particleDef.spritesDef.mode = mode;
- }
-
- void ParticleSystem::addParticleSprite(const Sprite* sprite)
- {
- mDef.particleDef.spritesDef.sprites.push_back(sprite);
- }
-
- void ParticleSystem::addParticleSprites(uint count, ...)
- {
- va_list args;
- va_start(args, count);
- while (count--)
- {
- Sprite* spr = va_arg(args, Sprite*);
- addParticleSprite(spr);
- }
- va_end(args);
- }
-
- void ParticleSystem::addParticleSprites(const std::vector<const Sprite*>& sprs)
- {
- for (const Sprite* spr : sprs)
- {
- addParticleSprite(spr);
- }
- }
-
- void ParticleSystem::removeParticleSprite(uint i)
- {
- mDef.particleDef.spritesDef.sprites.erase(mDef.particleDef.spritesDef.sprites.begin() + i);
- }
-
- void ParticleSystem::enableParticleBlendAdditive(bool enable)
- {
- mDef.particleDef.blendDef.additive = enable;
- }
-
- void ParticleSystem::setParticleScale(float scale)
- {
- mDef.particleDef.scaleDef.overTime.enable = false;
- mDef.particleDef.scaleDef.scale = scale;
- }
-
- void ParticleSystem::addParticleScalePoint(float scale, float t)
- {
- mDef.particleDef.scaleDef.overTime.enable = true;
- mDef.particleDef.scaleDef.overTime.value.addPoint(t, scale);
- }
-
- void ParticleSystem::removeParticleScalePoint(uint i)
- {
- mDef.particleDef.scaleDef.overTime.value.removePoint(i);
- }
-
- void ParticleSystem::setParticleColor(Color tint)
- {
- mDef.particleDef.colorDef.overTime.enable = false;
- mDef.particleDef.colorDef.color = tint;
- }
-
- void ParticleSystem::addParticleColorPoint(Color tint, float t)
- {
- mDef.particleDef.colorDef.overTime.enable = true;
- mDef.particleDef.colorDef.overTime.value.addColor(tint, t);
- }
-
- void ParticleSystem::removeParticleColorPoint(uint i)
- {
- mDef.particleDef.colorDef.overTime.value.removeColor(i);
- }
-
- void ParticleSystem::setParticleTransparency(float transparency)
- {
- mDef.particleDef.transparencyDef.overTime.enable = false;
- mDef.particleDef.transparencyDef.transparency = transparency;
- }
-
- void ParticleSystem::addParticleTransparencyPoint(float transparency, float t)
- {
- mDef.particleDef.transparencyDef.overTime.enable = true;
- mDef.particleDef.transparencyDef.overTime.value.addPoint(t, transparency);
- }
-
- void ParticleSystem::removeParticleTransparencyPoint(uint i)
- {
- mDef.particleDef.transparencyDef.overTime.value.removePoint(i);
- }
-
- }
- }
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ ParticleSystem::ParticleSystem(uint maxCount)
+ : mEmitter(*this)
+ , mParticlePool(maxCount, sizeof(Particle))
+ {
+ }
+
+ ParticleSystem::ParticleSystem(const ParticleSystemDef& def)
+ : mDef(def)
+ , mEmitter(*this)
+ , mParticlePool(def.maxParticleCount, sizeof(Particle))
+ {
+ }
+
+ ParticleSystem::~ParticleSystem()
+ {
+ }
+
+ void ParticleSystem::setPosition(float x, float y)
+ {
+ mPosition.x() = x;
+ mPosition.y() = y;
+ }
+
+ void ParticleSystem::update(float dt)
+ {
+ mEmitter.update(dt);
+ for (int i = 0; i < mAliveParticles.size(); ++i)
+ {
+ Particle* p = mAliveParticles[i];
+ if (p->alive == false)
+ {
+ recycle(i, p);
+ --i;
+ }
+ else
+ {
+ p->update(dt);
+ }
+ }
+ }
+
+ void ParticleSystem::render()
+ {
+ for (Particle* p : mAliveParticles)
+ p->render();
+ }
+
+ Particle* ParticleSystem::claim()
+ {
+ Particle* p = new (mParticlePool.GetNextWithoutInitializing()) Particle();
+ mAliveParticles.push_back(p);
+ //jin_log_info("particle count %d", mAliveParticles.size());
+ return p;
+ }
+
+ void ParticleSystem::recycle(int i, Particle* p)
+ {
+ if (i >= mAliveParticles.size())
+ return;
+ mAliveParticles.erase(mAliveParticles.begin() + i);
+ mParticlePool.Delete(p);
+ }
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ // Particle Emitter modification.
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ void ParticleSystem::setEmitRate(float floor, float ceil)
+ {
+ mDef.emitterDef.emitRateDef.enableRandom = true;
+ mDef.emitterDef.emitRateDef.rate.random.floor = floor;
+ mDef.emitterDef.emitRateDef.rate.random.ceil = ceil;
+ }
+
+ void ParticleSystem::setEmitRate(float rate)
+ {
+ mDef.emitterDef.emitRateDef.enableRandom = false;
+ mDef.emitterDef.emitRateDef.rate.rate = rate;
+ }
+
+ void ParticleSystem::setEmitForce(float floor, float ceil)
+ {
+ mDef.emitterDef.forceDef.enableRandom = true;
+ mDef.emitterDef.forceDef.force.random.floor = floor;
+ mDef.emitterDef.forceDef.force.random.ceil = ceil;
+ }
+
+ void ParticleSystem::setEmitForce(float force)
+ {
+ mDef.emitterDef.forceDef.enableRandom = false;
+ mDef.emitterDef.forceDef.force.force = force;
+ }
+
+ void ParticleSystem::setEmitDirection(float floor, float ceil)
+ {
+ mDef.emitterDef.directionDef.enableRandom = true;
+ mDef.emitterDef.directionDef.direction.random.floor = floor;
+ mDef.emitterDef.directionDef.direction.random.ceil = ceil;
+ }
+
+ void ParticleSystem::setEmitDirection(float dir)
+ {
+ mDef.emitterDef.directionDef.enableRandom = false;
+ mDef.emitterDef.directionDef.direction.direction = dir;
+ }
+
+ void ParticleSystem::setEmitPosition(const Math::Vector2<float>& floor, const Math::Vector2<float>& ceil)
+ {
+ mDef.emitterDef.positionDef.enableRandom = true;
+ mDef.emitterDef.positionDef.position.random.floor = floor;
+ mDef.emitterDef.positionDef.position.random.ceil = ceil;
+ }
+
+ void ParticleSystem::setEmitPosition(const Math::Vector2<float>& position)
+ {
+ mDef.emitterDef.positionDef.enableRandom = false;
+ mDef.emitterDef.positionDef.position.position = position;
+ }
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+ // Particle Emitter modification.
+ //////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ void ParticleSystem::setParticleLife(float floor, float ceil)
+ {
+ mDef.particleDef.lifeTimeDef.enableRandom = true;
+ mDef.particleDef.lifeTimeDef.life.random.floor = floor;
+ mDef.particleDef.lifeTimeDef.life.random.ceil = ceil;
+ }
+
+ void ParticleSystem::setParticleLife(float time)
+ {
+ mDef.particleDef.lifeTimeDef.enableRandom = true;
+ mDef.particleDef.lifeTimeDef.life.life = time;
+ }
+
+ void ParticleSystem::setParticleLinearAccelaration(Math::Vector2<float> ac)
+ {
+ mDef.particleDef.linearAccelarationDef.linearAccelaration = ac;
+ }
+
+ void ParticleSystem::setParticleRadialAccelaration(float ra)
+ {
+ mDef.particleDef.radialAccelarationDef.radialAccelaration = ra;
+ }
+
+ void ParticleSystem::setParticleAngularSpeed(float floor, float ceil)
+ {
+ mDef.particleDef.angularSpeedDef.enableRandom = true;
+ mDef.particleDef.angularSpeedDef.angularSpeed.random.floor = floor;
+ mDef.particleDef.angularSpeedDef.angularSpeed.random.ceil = ceil;
+ }
+
+ void ParticleSystem::setParticleAngularSpeed(float speed)
+ {
+ mDef.particleDef.angularSpeedDef.enableRandom = false;
+ mDef.particleDef.angularSpeedDef.angularSpeed.angularSpeed = speed;
+ }
+
+ void ParticleSystem::setParticleSpritesMode(SpriteMode mode)
+ {
+ mDef.particleDef.spritesDef.mode = mode;
+ }
+
+ void ParticleSystem::addParticleSprite(const Sprite* sprite)
+ {
+ mDef.particleDef.spritesDef.sprites.push_back(sprite);
+ }
+
+ void ParticleSystem::addParticleSprites(uint count, ...)
+ {
+ va_list args;
+ va_start(args, count);
+ while (count--)
+ {
+ Sprite* spr = va_arg(args, Sprite*);
+ addParticleSprite(spr);
+ }
+ va_end(args);
+ }
+
+ void ParticleSystem::addParticleSprites(const std::vector<const Sprite*>& sprs)
+ {
+ for (const Sprite* spr : sprs)
+ {
+ addParticleSprite(spr);
+ }
+ }
+
+ void ParticleSystem::removeParticleSprite(uint i)
+ {
+ mDef.particleDef.spritesDef.sprites.erase(mDef.particleDef.spritesDef.sprites.begin() + i);
+ }
+
+ void ParticleSystem::enableParticleBlendAdditive(bool enable)
+ {
+ mDef.particleDef.blendDef.additive = enable;
+ }
+
+ void ParticleSystem::setParticleScale(float scale)
+ {
+ mDef.particleDef.scaleDef.overTime.enable = false;
+ mDef.particleDef.scaleDef.scale = scale;
+ }
+
+ void ParticleSystem::addParticleScalePoint(float scale, float t)
+ {
+ mDef.particleDef.scaleDef.overTime.enable = true;
+ mDef.particleDef.scaleDef.overTime.value.addPoint(t, scale);
+ }
+
+ void ParticleSystem::removeParticleScalePoint(uint i)
+ {
+ mDef.particleDef.scaleDef.overTime.value.removePoint(i);
+ }
+
+ void ParticleSystem::setParticleColor(Color tint)
+ {
+ mDef.particleDef.colorDef.overTime.enable = false;
+ mDef.particleDef.colorDef.color = tint;
+ }
+
+ void ParticleSystem::addParticleColorPoint(Color tint, float t)
+ {
+ mDef.particleDef.colorDef.overTime.enable = true;
+ mDef.particleDef.colorDef.overTime.value.addColor(tint, t);
+ }
+
+ void ParticleSystem::removeParticleColorPoint(uint i)
+ {
+ mDef.particleDef.colorDef.overTime.value.removeColor(i);
+ }
+
+ void ParticleSystem::setParticleTransparency(float transparency)
+ {
+ mDef.particleDef.transparencyDef.overTime.enable = false;
+ mDef.particleDef.transparencyDef.transparency = transparency;
+ }
+
+ void ParticleSystem::addParticleTransparencyPoint(float transparency, float t)
+ {
+ mDef.particleDef.transparencyDef.overTime.enable = true;
+ mDef.particleDef.transparencyDef.overTime.value.addPoint(t, transparency);
+ }
+
+ void ParticleSystem::removeParticleTransparencyPoint(uint i)
+ {
+ mDef.particleDef.transparencyDef.overTime.value.removePoint(i);
+ }
+
+ }
+ }
} \ No newline at end of file
diff --git a/src/libjin/graphics/particles/particle_system.h b/src/libjin/graphics/particles/particle_system.h
index 83351a9..d3e3369 100644
--- a/src/libjin/graphics/particles/particle_system.h
+++ b/src/libjin/graphics/particles/particle_system.h
@@ -14,182 +14,182 @@
namespace JinEngine
{
- namespace Graphics
- {
- namespace Particles
- {
-
- ///
- /// Definition of particle system.
- ///
- struct ParticleSystemDef
- {
- ParticleSystemDef() {}
- uint maxParticleCount = 1; ///< Max count of particles in pool. 1 by default.
- ParticleEmitterDef emitterDef; ///< Particle emitter definition.
- ParticleDef particleDef; ///< Particle definition.
- };
-
- ///
- /// Particle emitter, handle all particles it emitts.
- ///
- class ParticleSystem : public Renderable, public Game::GameObject
- {
- public:
- ///
- /// Particle system constructor
- ///
- /// @param def Definition of particle system.
- ///
- ParticleSystem(const ParticleSystemDef& def);
-
- ///
- ///
- ///
- ParticleSystem(uint maxCount = 64);
-
- ///
- ///
- ///
- ParticleSystem(const std::string & config);
-
- ///
- /// Particle system destructor.
- ///
- ~ParticleSystem();
-
- ///
- ///
- ///
- void setDefinition(const ParticleSystemDef& def);
-
- ///
- /// Load definition from config.
- ///
- void setDefinition(const std::string& config);
-
- ///
- /// Update particle system and all alive particles.
- ///
- void update(float dt);
-
- ///
- /// Render particle system.
- ///
- void render();
-
- ///
- /// Set particle system position.
- ///
- void setPosition(float x, float y);
-
- ///
- /// Set scale.
- ///
- void setScale(float sx, float sy);
-
- ///
- /// Pause particle spawn.
- ///
- void pause(bool isPause);
-
- ///
- /// Clear all particles.
- ///
- void clear();
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // Particle Emitter modification.
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- void setEmitRate(float floor, float ceil);
- void setEmitRate(float rate);
-
- void setEmitForce(float floor, float ceil);
- void setEmitForce(float force);
-
- void setEmitDirection(float floor, float ceil);
- void setEmitDirection(float dir);
-
- void setEmitPosition(const Math::Vector2<float>& floor, const Math::Vector2<float>& ceil);
- void setEmitPosition(const Math::Vector2<float>& position);
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // Particle modification.
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- void setParticleLife(float floor, float ceil);
- void setParticleLife(float time);
-
- void setParticleLinearAccelaration(Math::Vector2<float> ac);
-
- void setParticleRadialAccelaration(float ra);
-
- void setParticleAngularSpeed(float floor, float ceil);
- void setParticleAngularSpeed(float speed);
-
- void setParticleSpritesMode(SpriteMode mode);
- void addParticleSprite(const Sprite* sprite);
- void addParticleSprites(uint count, ...);
- void addParticleSprites(const std::vector<const Sprite*>& sprs);
- void removeParticleSprite(uint i);
-
- void enableParticleBlendAdditive(bool enable);
-
- void setParticleScale(float scale);
- void addParticleScalePoint(float scale, float t);
- void removeParticleScalePoint(uint i);
-
- void setParticleColor(Color tint);
- void addParticleColorPoint(Color tint, float t);
- void removeParticleColorPoint(uint i);
-
- void setParticleTransparency(float transparency);
- void addParticleTransparencyPoint(float transparency, float t);
- void removeParticleTransparencyPoint(uint i);
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ ///
+ /// Definition of particle system.
+ ///
+ struct ParticleSystemDef
+ {
+ ParticleSystemDef() {}
+ uint maxParticleCount = 1; ///< Max count of particles in pool. 1 by default.
+ ParticleEmitterDef emitterDef; ///< Particle emitter definition.
+ ParticleDef particleDef; ///< Particle definition.
+ };
+
+ ///
+ /// Particle emitter, handle all particles it emitts.
+ ///
+ class ParticleSystem : public Renderable, public Game::GameObject
+ {
+ public:
+ ///
+ /// Particle system constructor
+ ///
+ /// @param def Definition of particle system.
+ ///
+ ParticleSystem(const ParticleSystemDef& def);
+
+ ///
+ ///
+ ///
+ ParticleSystem(uint maxCount = 64);
+
+ ///
+ ///
+ ///
+ ParticleSystem(const std::string & config);
+
+ ///
+ /// Particle system destructor.
+ ///
+ ~ParticleSystem();
+
+ ///
+ ///
+ ///
+ void setDefinition(const ParticleSystemDef& def);
+
+ ///
+ /// Load definition from config.
+ ///
+ void setDefinition(const std::string& config);
+
+ ///
+ /// Update particle system and all alive particles.
+ ///
+ void update(float dt);
+
+ ///
+ /// Render particle system.
+ ///
+ void render();
+
+ ///
+ /// Set particle system position.
+ ///
+ void setPosition(float x, float y);
+
+ ///
+ /// Set scale.
+ ///
+ void setScale(float sx, float sy);
+
+ ///
+ /// Pause particle spawn.
+ ///
+ void pause(bool isPause);
+
+ ///
+ /// Clear all particles.
+ ///
+ void clear();
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////
+ // Particle Emitter modification.
+ //////////////////////////////////////////////////////////////////////////////////////////////////
+
+ void setEmitRate(float floor, float ceil);
+ void setEmitRate(float rate);
+
+ void setEmitForce(float floor, float ceil);
+ void setEmitForce(float force);
+
+ void setEmitDirection(float floor, float ceil);
+ void setEmitDirection(float dir);
+
+ void setEmitPosition(const Math::Vector2<float>& floor, const Math::Vector2<float>& ceil);
+ void setEmitPosition(const Math::Vector2<float>& position);
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////
+ // Particle modification.
+ //////////////////////////////////////////////////////////////////////////////////////////////////
+
+ void setParticleLife(float floor, float ceil);
+ void setParticleLife(float time);
+
+ void setParticleLinearAccelaration(Math::Vector2<float> ac);
+
+ void setParticleRadialAccelaration(float ra);
+
+ void setParticleAngularSpeed(float floor, float ceil);
+ void setParticleAngularSpeed(float speed);
+
+ void setParticleSpritesMode(SpriteMode mode);
+ void addParticleSprite(const Sprite* sprite);
+ void addParticleSprites(uint count, ...);
+ void addParticleSprites(const std::vector<const Sprite*>& sprs);
+ void removeParticleSprite(uint i);
+
+ void enableParticleBlendAdditive(bool enable);
+
+ void setParticleScale(float scale);
+ void addParticleScalePoint(float scale, float t);
+ void removeParticleScalePoint(uint i);
+
+ void setParticleColor(Color tint);
+ void addParticleColorPoint(Color tint, float t);
+ void removeParticleColorPoint(uint i);
+
+ void setParticleTransparency(float transparency);
+ void addParticleTransparencyPoint(float transparency, float t);
+ void removeParticleTransparencyPoint(uint i);
- private:
- friend class ParticleEmitter;
-
- ///
- /// Particle system position.
- ///
- Math::Vector2<float> mPosition;
+ private:
+ friend class ParticleEmitter;
+
+ ///
+ /// Particle system position.
+ ///
+ Math::Vector2<float> mPosition;
- ///
- ///
- ///
- Particle* claim();
+ ///
+ ///
+ ///
+ Particle* claim();
- ///
- ///
- ///
- void recycle(int i, Particle* p);
+ ///
+ ///
+ ///
+ void recycle(int i, Particle* p);
- ///
- /// Particle system definition.
- ///
- ParticleSystemDef mDef;
+ ///
+ /// Particle system definition.
+ ///
+ ParticleSystemDef mDef;
- ///
- /// Particle emitter.
- ///
- ParticleEmitter mEmitter;
+ ///
+ /// Particle emitter.
+ ///
+ ParticleEmitter mEmitter;
- ///
- /// Particle pool.
- ///
- ParticlePool mParticlePool;
+ ///
+ /// Particle pool.
+ ///
+ ParticlePool mParticlePool;
- ///
- /// Alive particles, that means these particles could join to the life cycle loop.
- ///
- std::vector<Particle*> mAliveParticles;
+ ///
+ /// Alive particles, that means these particles could join to the life cycle loop.
+ ///
+ std::vector<Particle*> mAliveParticles;
- };
+ };
- } // namespace Particles
- } // namespace Graphics
+ } // namespace Particles
+ } // namespace Graphics
} // namespace JinEngine
#endif \ No newline at end of file