]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
particle delete fix
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 7 Jan 2017 00:09:11 +0000 (19:09 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 7 Jan 2017 00:09:11 +0000 (19:09 -0500)
include/particle.hpp
main.cpp
src/particle.cpp

index 4d42b39c934c05c9f0f0fefab40e6cce823dcc40..a022583d26d5bda01bde335791bdb5a7cbe99e1c 100644 (file)
@@ -34,7 +34,7 @@ public:
        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;
index 0c5a6f204e8557a61b2d8be7a43f793757c7a10f..d2ae4195e053f0ada8c37637362baebb60ae6482 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -224,7 +224,7 @@ int main(int argc, char *argv[])
                while (run) {
                        fpsInternal++;
                        render();
-                       game::engine.resetRender(0);
+                       game::engine.resetRender(0); // TODO stupid
                }
 
                // on game end, get back together
@@ -321,7 +321,7 @@ void render() {
        //player->inv->draw();
 
        game::engine.render(0);
-       
+
        // draw the fade overlay
        ui::drawFade();
 
index 52a563521b77ce13c5116a40de8955d39df75459..1cab0e9125f1e2e871f8201e1e202f3d8fa8e419 100644 (file)
@@ -26,7 +26,7 @@ void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std
                parts.emplace_back(f(), type, timeleft);
 }
 
-void ParticleSystem::render(void) const
+void ParticleSystem::render(void)
 {
        static GLuint particleVBO = 9999, texVBO = 0;
 
@@ -50,9 +50,14 @@ void ParticleSystem::render(void) const
 
        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,
@@ -61,11 +66,11 @@ void ParticleSystem::render(void) const
                        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();
@@ -103,8 +108,6 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e
 
                // update timers
                p.timeLeft -= dt;
-               if (p.timeLeft <= 0)
-                       parts.erase(part);
 
                // update movement
                switch (p.type) {
@@ -131,11 +134,9 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e
                                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;
                }
 
@@ -154,4 +155,3 @@ int ParticleSystem::getCount(void) const
 {
        return parts.size();
 }
-