diff --git a/src/modules/graphics/opengl/ParticleSystem.cpp b/src/modules/graphics/opengl/ParticleSystem.cpp index fde1691e4..a753160b9 100644 --- a/src/modules/graphics/opengl/ParticleSystem.cpp +++ b/src/modules/graphics/opengl/ParticleSystem.cpp @@ -100,6 +100,9 @@ ParticleSystem::ParticleSystem(Image *sprite, unsigned int buffer) ParticleSystem::~ParticleSystem() { + for (size_t i = 0; i < quads.size(); i++) + quads[i]->release(); + if (this->sprite != 0) this->sprite->release(); @@ -370,19 +373,7 @@ void ParticleSystem::setColor(const std::vector &newColors) colors[i] = colorToFloat(newColors[i]); } -void ParticleSystem::setQuad(love::graphics::Quad *quad) -{ - - for (size_t i = 0; i < quads.size(); i++) - quads[i]->release(); - - quads.resize(1); - - quads[0] = quad; - quad->retain(); -} - -void ParticleSystem::setQuad(const std::vector &newQuads) +void ParticleSystem::setQuads(const std::vector &newQuads) { for (size_t i = 0; i < quads.size(); i++) quads[i]->release(); @@ -396,7 +387,7 @@ void ParticleSystem::setQuad(const std::vector &newQuads } } -void ParticleSystem::setQuad() +void ParticleSystem::setQuads() { for (size_t i = 0; i < quads.size(); i++) quads[i]->release(); @@ -515,8 +506,8 @@ void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, flo const vertex *imageVerts = sprite->getVertices(); const vertex *tVerts; + size_t numQuads = quads.size(); - size_t quadIndex = 0; // set the vertex data for each particle (transformation, texcoords, color) for (int i = 0; i < numParticles; i++) @@ -526,7 +517,7 @@ void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, flo if (numQuads > 0) { // Make sure the quad index is valid - quadIndex = (p->quadIndex >= numQuads) ? numQuads - 1 : p->quadIndex; + size_t quadIndex = (p->quadIndex >= numQuads) ? numQuads - 1 : p->quadIndex; tVerts = quads[quadIndex]->getVertices(); } else @@ -663,12 +654,12 @@ void ParticleSystem::update(float dt) p->color = colors[i] * (1.0f - s) + colors[k] * s; // Update quad index - if (quads.size() > 0) + k = quads.size(); + if (k > 0) { - k = quads.size() - 1; - s = t * (float) k; + s = t * (float) k; // [0:numquads-1] i = (s > 0) ? (size_t) s : 0; - p->quadIndex = (i <= k) ? i : k; + p->quadIndex = (i < k) ? i : k - 1; } else p->quadIndex = 0; diff --git a/src/modules/graphics/opengl/ParticleSystem.h b/src/modules/graphics/opengl/ParticleSystem.h index bce15f180..b1bea8322 100644 --- a/src/modules/graphics/opengl/ParticleSystem.h +++ b/src/modules/graphics/opengl/ParticleSystem.h @@ -297,19 +297,15 @@ public: void setColor(const std::vector &newColors); /** - * + * Sets the quads used when drawing the particles. + * @param newQuads Array of quads. **/ - void setQuad(love::graphics::Quad *quad); + void setQuads(const std::vector &newQuads); /** - * + * Removes all quads from the particle system. **/ - void setQuad(const std::vector &newQuads); - - /** - * - **/ - void setQuad(); + void setQuads(); /** * Returns the x-coordinate of the emitter's position. diff --git a/src/modules/graphics/opengl/wrap_ParticleSystem.cpp b/src/modules/graphics/opengl/wrap_ParticleSystem.cpp index 34467fad1..f5dacb897 100644 --- a/src/modules/graphics/opengl/wrap_ParticleSystem.cpp +++ b/src/modules/graphics/opengl/wrap_ParticleSystem.cpp @@ -266,26 +266,38 @@ int w_ParticleSystem_setColors(lua_State *L) int w_ParticleSystem_setQuads(lua_State *L) { ParticleSystem *t = luax_checkparticlesystem(L, 1); - int nQuads = lua_gettop(L) - 1; + int nQuads = lua_gettop(L) - 1; + if (lua_istable(L, 2)) + nQuads = lua_objlen(L, 2); + + // Remove all quads if no argument is given. if (nQuads == 0) - t->setQuad(); - else if (nQuads == 1) { - love::graphics::Quad *q = luax_checkframe(L, 2); - t->setQuad(q); + t->setQuads(); + return 0; + } + + std::vector quads(nQuads); + + if (lua_istable(L, 2)) + { + for (int i = 0; i < nQuads; i++) + { + lua_pushnumber(L, i + 1); // array index + lua_gettable(L, 2); + quads[i] = luax_checkframe(L, -1); + lua_pop(L, 1); + } } else { - std::vector quads(nQuads); - for (size_t i = 0; i < nQuads; i++) - { - love::graphics::Quad *q = luax_checkframe(L, i + 2); - quads[i] = q; - } - t->setQuad(quads); + for (int i = 0; i < nQuads; i++) + quads[i] = luax_checkframe(L, i + 2); } + t->setQuads(quads); + return 0; }