void add(const vec2& pos, const ParticleType& type, const int& timeleft = 3000);
void addMultiple(const int& count, const ParticleType& type, std::function<vec2(void)> f, const int& timeleft = 3000);
- void render(void) const;
+ void render(void);
void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
int getCount(void) const;
parts.emplace_back(f(), type, timeleft);
}
-void ParticleSystem::render(void) const
+void ParticleSystem::render(void)
{
static GLuint particleVBO = 9999, texVBO = 0;
glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
+ // clear dead particles
+ parts.erase(std::remove_if(parts.begin(), parts.end(),
+ [](const Particle& p) { return p.timeLeft <= 0; }), parts.end());
+
// copy data into VBO
int offset = 0;
for (const auto& p : parts) {
+
GLfloat coords[18] = {
p.location.x, p.location.y, -1,
p.location.x, p.location.y + 5, -1,
p.location.x + 5, p.location.y, -1,
p.location.x, p.location.y, -1
};
-
+
glBufferSubData(GL_ARRAY_BUFFER, offset, 18 * sizeof(GLfloat), coords);
offset += 18 * sizeof(GLfloat);
}
-
+
Render::worldShader.use();
Render::worldShader.enable();
Colors::blue.use();
// update timers
p.timeLeft -= dt;
- if (p.timeLeft <= 0)
- parts.erase(part);
// update movement
switch (p.type) {
if ((p.velocity.x > -0.01 && p.velocity.x < 0.01) &&
(p.velocity.y > -0.01 && p.velocity.y < 0.01)) {
p.timeLeft = 0;
- if (p.timeLeft <= 0)
- parts.erase(part);
}
}
-
+
break;
}
{
return parts.size();
}
-