Example #1
0
void ParticleSystem::update(float delta) {
    if (sprite == NULL) {
        std::cout << "sprite was null" << std::endl;
        return;
        return;
    }
    removeMe.clear();

    //std::cout << "1" << std::endl;


    for(unsigned int i = 0; i < emitters.size(); i++) {
        ParticleEmitter* emitter = emitters.at(i);
        if (emitter->isEnabled()) {
            //std::cout << "en" << std::endl;
            emitter->update(this, delta);
            //std::cout << "en2" << std::endl;
            if (removeCompletedEmitters) {
                if (emitter->isComplete()) {
                    removeMe.push_back(emitter);
                    particlesByEmitter.erase(emitter);
                }
            }
        }
    }

    //std::cout << "2" << std::endl;

    // remove items in removeMe from emitters.
    vector<ParticleEmitter*>::iterator ito = emitters.begin();
    while (ito != emitters.end()) {
        ParticleEmitter* e = (*ito);

        vector<ParticleEmitter*>::iterator ito2 = removeMe.begin();
        while (ito2 != removeMe.end()) {
            ParticleEmitter* e2 = (*ito2);

            if (e == e2) {
                ito = emitters.erase(ito);
                continue;
            }

            ito2++;
        }

        ito++;
    }

    //std::cout << "3" << std::endl;

    pCount = 0;

    if (!particlesByEmitter.empty()) {
        //std::cout << "4: " << particlesByEmitter.size() << std::endl;
        map<ParticleEmitter*, ParticlePool*>::iterator it = particlesByEmitter.begin();
        while (it != particlesByEmitter.end()) {
            ParticleEmitter* emitter = it->first;
            if (emitter->isEnabled()) {
                ParticlePool* pool = it->second;
                //std::cout << "5" << std::endl;
                for (unsigned int i = 0; i < pool->particlesActive.size(); i++) {
                    if (pool->particlesActive.at(i)->getLife() > 0) {
                        pool->particlesActive.at(i)->update(delta);
                        pCount++;
                    }
                }
            }
            it++;
            //std::cout << "6" << std::endl;
        }
    }
}
Example #2
0
void ParticleSystem::render(float x, float y) {
    if (sprite == NULL) {
        std::cout << "sprite was null" << std::endl;
        return;
    }
    if (!m_visible) {
        std::cout << "invisible" << std::endl;
        return;
    }

    Renderer* g = ARK2D::getRenderer();

    g->pushMatrix();
    g->translate(x, y);
    if (blendingMode == BLEND_ADDITIVE) { // TODO: g->setBlendMode(Renderer::BLEND_ADDITIVE);
        //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
        g->setBlendMode(Renderer::BLEND_ADDITIVE);
    }
    //if (isUsePoints()) {
    //	glEnable(GL_POINT_SMOOTH);
    //}
    for(unsigned int i = 0; i < emitters.size(); i++) {
        ParticleEmitter* emitter = emitters.at(i);
        if (!emitter->isEnabled()) {
            continue;
        }

        //std::cout << "render" << std::endl;
        // check for additive override and enable when set
        if (emitter->useAdditive()) {
            //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
            g->setBlendMode(Renderer::BLEND_ADDITIVE);
        }

        // now get the particle pool for this emitter and render all particles that are in use
        ParticlePool* pool = particlesByEmitter.find(emitter)->second;
        Image* image = emitter->getImage();
        if (image == NULL && !emitter->usePoints()) {// && (!usePoints)) {
            //image = sprite;
            //emitter->u
            RendererState::start(RendererState::TEXTURE, sprite->getTexture()->getId());
            //sprite->bind(); // we need to cancel this out if we're rendering points.
        }

        //if (!emitter->isOriented() && !emitter->usePoints(this)) {
        //	image->startUse();
        //}



        /*!
         * todo: we don't know what particles are points and what are images
         * 		 so are currently binding and unbinding the default image LOTS.
         * 		 TOO MUCH.
         */

        for (unsigned int i = 0; i < pool->particlesActive.size(); i++) {
            if (pool->particlesActive.at(i)->inUse()) {
                pool->particlesActive.at(i)->render();
            }
        }

        //if (!emitter->isOriented() && !emitter->usePoints(this)) {
        //	image->endUse();
        //}

        // reset additive blend mode
        if (emitter->useAdditive()) {
            //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            g->setBlendMode(Renderer::BLEND_NORMAL);
        }
        //std::cout << "render2" << std::endl;
    }
    //if (isUsePoints()) {
    //	glDisable(GL_POINT_SMOOTH);
    //}

    if (blendingMode == BLEND_ADDITIVE) {
        //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        g->setBlendMode(Renderer::BLEND_NORMAL);
    }

    g->setDrawColor(Color::white);
    //g->translate(x * -1, y * -1);
    g->popMatrix();
}