aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Graphics/particles/je_particle_system.cpp
blob: a81a3c9d8ee4d612b53bf483f1994d3ad4322ad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "je_particle_system.h"

namespace JinEngine
{
    namespace Graphics
    {
        namespace Particles
        {

            ParticleSystem::ParticleSystem(const ParticleSystemDef& def)
                : mDef(def)
                , mEmitter(*this)
                , mParticlePool(def.maxParticleCount, sizeof(Particle))
            {
            }

            ParticleSystem::~ParticleSystem()
            {
            }

            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(float x, float y, float sx /* = 1 */, float sy /* = 1 */, float r /* = 0 */, float ax /* = 0 */, float ay /* = 0 */)
            {
                for (Particle* p : mAliveParticles)
                    p->render();
            }

            void ParticleSystem::setGraphic(const Graphic* graphic)
            {
                mGraphic = graphic;
            }

            Particle* ParticleSystem::claim()
            {
                Particle* p = new (mParticlePool.GetNextWithoutInitializing()) Particle(mGraphic);
                mAliveParticles.push_back(p);
                return p;
            }

            void ParticleSystem::recycle(int i, Particle* p)
            {
                if (i >= mAliveParticles.size())
                    return;
                mAliveParticles.erase(mAliveParticles.begin() + i);
                mParticlePool.Delete(p);
            }

        }
    }
}