aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/particles
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Graphics/particles')
-rw-r--r--src/libjin/Graphics/particles/je_particle.cpp0
-rw-r--r--src/libjin/Graphics/particles/je_particle.h157
-rw-r--r--src/libjin/Graphics/particles/je_particle_emitter.cpp14
-rw-r--r--src/libjin/Graphics/particles/je_particle_emitter.h99
-rw-r--r--src/libjin/Graphics/particles/je_particle_pool.cpp0
-rw-r--r--src/libjin/Graphics/particles/je_particle_pool.h56
-rw-r--r--src/libjin/Graphics/particles/je_particle_system.cpp11
-rw-r--r--src/libjin/Graphics/particles/je_particle_system.h94
8 files changed, 431 insertions, 0 deletions
diff --git a/src/libjin/Graphics/particles/je_particle.cpp b/src/libjin/Graphics/particles/je_particle.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle.cpp
diff --git a/src/libjin/Graphics/particles/je_particle.h b/src/libjin/Graphics/particles/je_particle.h
new file mode 100644
index 0000000..819b95c
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle.h
@@ -0,0 +1,157 @@
+#ifndef __JE_PARTICLE_H
+#define __JE_PARTICLE_H
+
+#include "../../math/je_vector2.hpp"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ class ParticleEmitter;
+
+ struct LifeTimeDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ float min, max;
+ } random;
+ float life;
+ } life;
+ };
+
+ struct LinearAccelaration
+ {
+
+ };
+
+ struct SpeedOverTimeDef
+ {
+ bool enable = false;
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ Math::Vector2<float> startFloor;
+ Math::Vector2<float> startCeil;
+ Math::Vector2<float> endFloor;
+ Math::Vector2<float> endCeil;
+ } random;
+ struct
+ {
+ Math::Vector2<float> start;
+ Math::Vector2<float> end;
+ } speed;
+ } speed;
+ };
+
+ struct SizeOverTimeDef
+ {
+ bool enable = false;
+ bool enableRandom = false;
+ union {
+ struct {
+ float startFloor = 1;
+ float startCeil = 1;
+ float endFloor = 1;
+ float endCeil = 1;
+ } random;
+ struct {
+ float start = 1;
+ float end = 1;
+ } size;
+ } size;
+ };
+
+ struct ColorOverTime
+ {
+ bool enable = false;
+ Color colorStart = Color::WHITE;
+ Color colorEnd = Color::WHITE;
+ };
+
+ ///
+ ///
+ ///
+ struct ParticleDef
+ {
+ private:
+ friend class ParticleEmitter;
+
+ public:
+ // Basic definitions.
+ LifeTimeDef lifeTimeDef; ///<
+ // Optional definitions.
+
+ SpeedOverTimeDef speedOverTimeDef; ///<
+ SizeOverTimeDef sizeOverTimeDef; ///<
+ ColorOverTime colorOverTimeDef; ///<
+ };
+
+ ///
+ /// A single particle contains various properties of particle, such as position, accelaration, color and
+ /// other attributes changed over time.
+ ///
+ struct Particle
+ {
+ Particle(const ParticleDef& particleDef);
+ ///
+ /// Whole life time.
+ ///
+ float lifeTime = 0.0f;
+
+ ///
+ /// Current life time.
+ ///
+ float life = 0.0f;
+
+ ///
+ /// Current position.
+ ///
+ float position[2] = { 0,0 };
+
+ ///
+ /// Emitte direction.
+ ///
+ float direction = 0;
+
+ Math::Vector2<float> speed;
+ Math::Vector2<float> linearAcceleration;
+ float radialAcceleration = 0;
+ float tangetialAcceleration = 0;
+
+ ///
+ /// Size over lifetime.
+ ///
+ float size = 1;
+ float sizeBegin = 1;
+ float sizeEnd = 1;
+
+ float rotation = 0;
+ float angle = 0;
+
+ ///
+ /// Color over lifetime.
+ ///
+ Color color = Color::WHITE;
+ Color colorStart = Color::WHITE;
+ Color colorEnd = Color::WHITE;
+
+ ///
+ /// Is particle still alive? Alive is equivalent to NOT available in particle pool.
+ ///
+ bool alive = false;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle_emitter.cpp b/src/libjin/Graphics/particles/je_particle_emitter.cpp
new file mode 100644
index 0000000..0ab9cf1
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_emitter.cpp
@@ -0,0 +1,14 @@
+#ifndef __JE_PARTICLE_SYSTEM_H
+#define __JE_PARTICLE_SYSTEM_H
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+
+
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle_emitter.h b/src/libjin/Graphics/particles/je_particle_emitter.h
new file mode 100644
index 0000000..c6e4321
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_emitter.h
@@ -0,0 +1,99 @@
+#ifndef __JE_PARTICLE_EMITTER_H
+#define __JE_PARTICLE_EMITTER_H
+
+#include "../../common/je_temporary.h"
+#include "../../math/je_vector2.hpp"
+
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ struct PositionDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ Math::Vector2<float> min;
+ Math::Vector2<float> max;
+ } random;
+ Math::Vector2<float> position;
+ } position;
+ };
+
+ struct DirectionDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ float min = 0;
+ float max = 0;
+ } random;
+ float direction = 0;
+ } direction;
+ };
+
+ struct EmitRateDef
+ {
+ bool enableRandom = false;
+ union
+ {
+ struct
+ {
+ float min = 1;
+ float max = 1;
+ } random;
+ float rate = 1;
+ } rate;
+ };
+
+ ///
+ /// Definition of particle emitter.
+ ///
+ struct ParticleEmitterDef : public Temporary
+ {
+ PositionDef positionDef; ///< Emit position(relativily to the particle system center).
+ DirectionDef directionDef; ///< Emit direction.
+ EmitRateDef emitRateDef; ///< Emit rate.
+ };
+
+ ///
+ /// Emit a single particle.
+ ///
+ class ParticleEmitter
+ {
+ public:
+ ///
+ /// ParticleEmitter constructor.
+ ///
+ /// @param emitterDef Definition of particle emitter.
+ /// @param particleDef Definition of particle.
+ ///
+ ParticleEmitter(const ParticleEmitterDef& emitterDef, const ParticleDef& particleDef);
+
+ ///
+ /// Emit a 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.
+ ///
+ Particle emit();
+
+ private:
+ float mDirection;
+ ParticleEmitterDef mEmitterDef;
+ ParticleDef mParticleDef;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle_pool.cpp b/src/libjin/Graphics/particles/je_particle_pool.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_pool.cpp
diff --git a/src/libjin/Graphics/particles/je_particle_pool.h b/src/libjin/Graphics/particles/je_particle_pool.h
new file mode 100644
index 0000000..9bc7d78
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_pool.h
@@ -0,0 +1,56 @@
+#ifndef __JE_PARTICLE_BATCH_H
+#define __JE_PARTICLE_BATCH_H
+
+#include <list>
+
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ ///
+ /// Particle pool for reducing memory fragmentation.
+ ///
+ class ParticlePool
+ {
+ public:
+
+ ///
+ /// Particle pool constructor.
+ ///
+ /// @param count Max count of particles.
+ ///
+ ParticlePool(uint count);
+
+ ///
+ /// Particle pool destructor.
+ ///
+ ~ParticlePool();
+
+ ///
+ /// Claim a particle if available.
+ ///
+ Particle* claim();
+
+ ///
+ /// Recycle particle if the particle is no more alive.
+ ///
+ void recycle(Particle* particle);
+
+ private:
+ ///
+ /// All particles include available and inavailable particles.
+ ///
+ std::list<Particle> particles;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/particles/je_particle_system.cpp b/src/libjin/Graphics/particles/je_particle_system.cpp
new file mode 100644
index 0000000..68f8f21
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_system.cpp
@@ -0,0 +1,11 @@
+#include "je_particle_system.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+
+
+
+ }
+}
diff --git a/src/libjin/Graphics/particles/je_particle_system.h b/src/libjin/Graphics/particles/je_particle_system.h
new file mode 100644
index 0000000..aa7ff99
--- /dev/null
+++ b/src/libjin/Graphics/particles/je_particle_system.h
@@ -0,0 +1,94 @@
+#ifndef __JE_PARTICLE_EMMITTER_H
+#define __JE_PARTICLE_EMMITTER_H
+
+#include <vector>
+
+#include "../../common/je_temporary.h"
+#include "../../game/je_gameobject.h"
+
+#include "../je_sprite.h"
+
+#include "je_particle_emitter.h"
+#include "je_particle_pool.h"
+#include "je_particle.h"
+
+namespace JinEngine
+{
+ namespace Graphics
+ {
+ namespace Particles
+ {
+
+ ///
+ /// Definition of particle system.
+ ///
+ struct ParticleSystemDef : public Temporary
+ {
+ 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 Game::GameObject
+ {
+ public:
+ ///
+ /// Particle system constructor
+ ///
+ /// @param def Definition of particle system.
+ ///
+ ParticleSystem(const ParticleSystemDef& def);
+
+ ///
+ /// Particle system destructor.
+ ///
+ ~ParticleSystem();
+
+ ///
+ /// Render particle system's all particles.
+ ///
+ void render(int x, int y, float sx = 1, float sy = 1, float r = 0, float ax = 0, float ay = 0);
+
+ ///
+ /// Set sprite to render.
+ ///
+ /// @param sprite Sprite to render.
+ ///
+ void setSprite(Sprite* sprite);
+
+ ///
+ /// Release particle and make it available in particle pool.
+ ///
+ void releaseParticle();
+
+ private:
+ // Disable default constructor.
+ ParticleSystem();
+
+ ///
+ /// Sprite to be drawn.
+ ///
+ Sprite* mSprite;
+
+ ///
+ /// Particle emitter.
+ ///
+ ParticleEmitter mEmitter;
+ ParticlePool mParticlePool;
+
+ ///
+ /// Alive particles, that means these particles could join to the life cycle loop.
+ ///
+ std::vector<Particle*> mAliveParticles;
+
+ };
+
+ } // namespace Particles
+ } // namespace Graphics
+} // namespace JinEngine
+
+#endif \ No newline at end of file