hx3d  1
2D/3D Simple Game Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
particle_emitter.cpp
1 /*
2  Particle emitter.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/particle_emitter.hpp"
22 
23 #include "hx3d/math/random.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 
28 ParticleEmitter::ParticleEmitter(const unsigned int maxParticles):
29  rotationSpeed(0.f), particles(maxParticles), texture(nullptr)
30 {}
31 
32 void ParticleEmitter::emit(const unsigned int qty) {
33  for (unsigned int i = 0; i < qty; ++i) {
34  emitOne();
35  }
36 }
37 
38 void ParticleEmitter::update(const float delta) {
39  const auto& working = particles.getWorking();
40 
41  for (auto i = working.begin(); i != working.end();) {
42  const Ptr<Particle>& p = *i;
43  if (p->dead) {
44  particles.release(p);
45  } else {
46  p->update(delta);
47  ++i;
48  }
49  }
50 }
51 
53  const auto& working = particles.getWorking();
54  for (auto it = working.begin(); it != working.end(); ++it) {
55  const Ptr<Particle> &p = *it;
56  p->draw(batch);
57  }
58 }
59 
61  this->texture = texture;
62 }
63 
65  return particles.getWorking().size();
66 }
67 
69 
70 void ParticleEmitter::emitOne() {
71  Ptr<Particle> p = particles.take();
72  if (!p) {
73  return;
74  }
75 
76  p->setTexture(texture);
77  p->rotationSpeed = rotationSpeed;
78  p->position.x = math::random(position.x - emitter_size.x / 2.f, position.x + emitter_size.x / 2.f);
79  p->position.y = math::random(position.y - emitter_size.y / 2.f, position.y + emitter_size.y / 2.f);
80  p->size.x = particle_size.x;
81  p->size.y = particle_size.y;
82  p->velocity.x = velocity.x;
83  p->velocity.y = velocity.y;
84  p->gravity.x = gravity.x;
85  p->gravity.y = gravity.y;
86  p->baseLife = p->life = life;
87 }
88 
89 } /* graphics */
90 } /* hx3d */
float rotationSpeed
Particles Z-rotation speed.
void draw(Batch &batch)
Draw the particles.
hx3d framework namespace
Definition: audio.hpp:26
glm::vec3 position
Emitter position.
void update(const float delta)
Update the emitter.
ParticleEmitter(const unsigned int maxParticles)
Construct an emitter with a max particle number.
Simple base batch implementation. Draw at each draw call.
Definition: batch.hpp:36
glm::vec3 particle_size
Particles size.
glm::vec3 emitter_size
Emitter size.
glm::vec3 velocity
Particles velocity.
int random(int min, int max)
Generate a random integer between min and max.
Definition: random.cpp:74
unsigned int getParticleCount()
Get the active particle count.
void emit(const unsigned int qty)
Emit particles.
glm::vec3 gravity
Particles gravity.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
void setTexture(const Ptr< Texture > &texture)
Set the emitter texture.