jubilant-funicular
ParticleBatch2D.cpp
1 #include <algorithm>
2 
3 #include "nta/ParticleBatch2D.h"
4 
5 namespace nta {
7  }
9  if (m_particles) {
10  delete[] m_particles;
11  }
12  }
13  void ParticleBatch2D::init(float il, float dr, float r, int mp, int tex, std::function<void(Particle2D&, float)> updateFunc) {
14  m_initialLife = il;
15  m_decayRate = dr;
16  m_radius = r;
17  m_maxParticles = mp;
18  m_textureID = tex;
19  m_updateFunction = updateFunc;
20  m_particles = new Particle2D[mp];
21  for (int i = 0; i < mp; i++) {
22  m_particles[i].life = 0;
23  }
24  }
26  int minLife = 0;
27  for (int i = 1; i < m_maxParticles; i++) {
28  if (m_particles[i].life <= 0) {
29  return i;
30  } else if (m_particles[i].life < m_particles[minLife].life) {
31  minLife = i;
32  }
33  }
34  return minLife;
35  }
37  int index = getFreeParticle();
38  m_particles[index] = p;
39  m_particles[index].life = m_initialLife;
40  }
41  void ParticleBatch2D::draw(SpriteBatch& batch) const {
42  for (int i = 0; i < m_maxParticles; i++) {
43  if (m_particles[i].life > 0) {
44  batch.addGlyph(glm::vec4(m_particles[i].center+glm::vec2(-m_radius,m_radius), 2.f*glm::vec2(m_radius)),
45  glm::vec4(0,0,1,1), m_textureID, 0, m_particles[i].color);
46  }
47  }
48  }
49  void ParticleBatch2D::update(float dt) {
50  for (int i = 0; i < m_maxParticles; i++) {
51  if (m_particles[i].life > 0) {
53  m_particles[i].life -= m_decayRate*dt;
54  }
55  }
56  }
58  for (int i = 0; i < m_maxParticles; i++) {
59  m_particles[i].life = 0;
60  }
61  }
62 }
nta::ParticleBatch2D::m_radius
float m_radius
the radius of an individual particle
Definition: ParticleBatch2D.h:34
nta::ParticleBatch2D::ParticleBatch2D
ParticleBatch2D()
basic constructor
Definition: ParticleBatch2D.cpp:6
nta::ParticleBatch2D::update
void update(float dt)
updates the particles
Definition: ParticleBatch2D.cpp:49
nta::ParticleBatch2D::draw
void draw(SpriteBatch &batch) const
draws all the particles
Definition: ParticleBatch2D.cpp:41
nta::SpriteBatch
Definition: SpriteBatch.h:87
nta::ParticleBatch2D::m_textureID
int m_textureID
texture used by the particles
Definition: ParticleBatch2D.h:38
nta::ParticleBatch2D::getFreeParticle
int getFreeParticle() const
returns index of particle with the minimum life
Definition: ParticleBatch2D.cpp:25
nta::SpriteBatch::addGlyph
void addGlyph(crvec4 posRect, GLuint texture=0, crvec4 uvRect=glm::vec4(0, 0, 1, 1), crvec4 color=glm::vec4(1), float depth=0.5)
Definition: SpriteBatch.cpp:100
nta::Particle2D
Represents a simple 2d particle.
Definition: ParticleBatch2D.h:10
nta::ParticleBatch2D::clear
void clear()
removes all particles
Definition: ParticleBatch2D.cpp:57
nta
Definition: Animation2D.h:6
nta::ParticleBatch2D::m_maxParticles
int m_maxParticles
maximum number of particles allowed
Definition: ParticleBatch2D.h:36
nta::ParticleBatch2D::m_decayRate
float m_decayRate
the rate with which particles lose life
Definition: ParticleBatch2D.h:32
nta::ParticleBatch2D::addParticle
void addParticle(Particle2D p)
adds a particle to the batch
Definition: ParticleBatch2D.cpp:36
nta::ParticleBatch2D::m_updateFunction
std::function< void(Particle2D &, float)> m_updateFunction
the function used to update the particles
Definition: ParticleBatch2D.h:26
nta::ParticleBatch2D::m_initialLife
float m_initialLife
the life every particle starts out with
Definition: ParticleBatch2D.h:30
nta::ParticleBatch2D::~ParticleBatch2D
~ParticleBatch2D()
deletes particles
Definition: ParticleBatch2D.cpp:8
nta::ParticleBatch2D::m_particles
Particle2D * m_particles
the particles themselves
Definition: ParticleBatch2D.h:28
nta::ParticleBatch2D::init
void init(float il, float dr, float r, int mp, int tex, std::function< void(Particle2D &, float)> updateFunc)
initializes particle batch by specifying properties
Definition: ParticleBatch2D.cpp:13