Browse Source

ParticleSystem:setQuads can now accept a table argument, cleaned up the ParticleSystem quad code

--HG--
branch : particlesystem-quads
Alex Szpakowski 12 years ago
parent
commit
318f335c7c

+ 11 - 20
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<Color> &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<love::graphics::Quad *> &newQuads)
+void ParticleSystem::setQuads(const std::vector<love::graphics::Quad *> &newQuads)
 {
 	for (size_t i = 0; i < quads.size(); i++)
 		quads[i]->release();
@@ -396,7 +387,7 @@ void ParticleSystem::setQuad(const std::vector<love::graphics::Quad *> &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;

+ 5 - 9
src/modules/graphics/opengl/ParticleSystem.h

@@ -297,19 +297,15 @@ public:
 	void setColor(const std::vector<Color> &newColors);
 
 	/**
-	 *
-	 **/
-	void setQuad(love::graphics::Quad *quad);
-
-	/**
-	 *
+	 * Sets the quads used when drawing the particles.
+	 * @param newQuads Array of quads.
 	 **/
-	void setQuad(const std::vector<love::graphics::Quad *> &newQuads);
+	void setQuads(const std::vector<love::graphics::Quad *> &newQuads);
 
 	/**
-	 *
+	 * Removes all quads from the particle system.
 	 **/
-	void setQuad();
+	void setQuads();
 
 	/**
 	 * Returns the x-coordinate of the emitter's position.

+ 22 - 10
src/modules/graphics/opengl/wrap_ParticleSystem.cpp

@@ -266,25 +266,37 @@ 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;
+	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;
 	}
-	else
+
+	std::vector<love::graphics::Quad *> quads(nQuads);
+
+	if (lua_istable(L, 2))
 	{
-		std::vector<love::graphics::Quad *> quads(nQuads);
-		for (size_t i = 0; i < nQuads; i++)
+		for (int i = 0; i < nQuads; i++)
 		{
-			love::graphics::Quad *q = luax_checkframe(L, i + 2);
-			quads[i] = q;
+			lua_pushnumber(L, i + 1); // array index
+			lua_gettable(L, 2);
+			quads[i] = luax_checkframe(L, -1);
+			lua_pop(L, 1);
 		}
-		t->setQuad(quads);
 	}
+	else
+	{
+		for (int i = 0; i < nQuads; i++)
+			quads[i] = luax_checkframe(L, i + 2);
+	}
+
+	t->setQuads(quads);
 
 	return 0;
 }