Commits

Alex Szpakowski committed cf85183

Removed SpriteBatch:remove(id) (reverted commit 2ca82dd)
The implementation had issues with sprite draw order when SpriteBatch:add used indices in the vertex buffer previously freed with SpriteBatch:remove

  • Participants
  • Parent commits 9337115

Comments (0)

Files changed (4)

src/modules/graphics/opengl/SpriteBatch.cpp

 
 #include "SpriteBatch.h"
 
-// STD
-#include <algorithm> // std::find
-
 // OpenGL
 #include "OpenGL.h"
 
 int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index /*= -1*/)
 {
 	// Only do this if there's a free slot.
-	if ((index == -1 && next >= size && gaps.size() == 0) || index < -1 || index >= size)
+	if ((index == -1 && next >= size) || index < -1 || index >= size)
 		return -1;
 
-	// Determine where in the vertex buffer to insert into, using the gap list
-	// if possible.
-	int realindex;
-	if (index == -1 && gaps.size() > 0)
-	{
-		realindex = gaps.front();
-		gaps.pop_front();
-	}
-	else
-	{
-		realindex = (index == -1) ? next : index;
-		std::deque<int>::iterator it = std::find(gaps.begin(), gaps.end(), realindex);
-		if (it != gaps.end())
-			gaps.erase(it); // This index is no longer a gap.
-	}
-
 	// Needed for colors.
 	memcpy(sprite, image->getVertices(), sizeof(vertex)*4);
 
 		setColorv(sprite, *color);
 	
 
-	addv(sprite, realindex);
+	addv(sprite, (index == -1) ? next : index);
 
 	// Increment counter.
-	if (index == -1 && realindex == next)
+	if (index == -1)
 		return next++;
 
-	return realindex;
+	return index;
 }
 
 int SpriteBatch::addq(Quad *quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index /*= -1*/)
 {
 	// Only do this if there's a free slot.
-	if ((index == -1 && next >= size && gaps.size() == 0) || index < -1 || index >= next)
+	if ((index == -1 && next >= size) || index < -1 || index >= next)
 		return -1;
 
-	// Determine where in the vertex buffer to insert into, using the gap list
-	// if possible.
-	int realindex;
-	if (index == -1 && gaps.size() > 0)
-	{
-		realindex = gaps.front();
-		gaps.pop_front();
-	}
-	else
-	{
-		realindex = (index == -1) ? next : index;
-		std::deque<int>::iterator it = std::find(gaps.begin(), gaps.end(), realindex);
-		if (it != gaps.end())
-			gaps.erase(it); // This index is no longer a gap.
-	}
-
 	// Needed for colors.
 	memcpy(sprite, quad->getVertices(), sizeof(vertex)*4);
 
 	if (color)
 		setColorv(sprite, *color);
 
-	addv(sprite, realindex);
+	addv(sprite, (index == -1) ? next : index);
 
 	// Increment counter.
-	if (index == -1 && realindex == next)
+	if (index == -1)
 		return next++;
 
-	return realindex;
-}
-
-void SpriteBatch::remove(int index)
-{
-	if (index < 0 || index >= next || next <= 0)
-		return;
-
-	// If this is the last index in the sprite list, decrease the total sprite
-	// count instead of adding a dummy sprite.
-	if (index == next - 1)
-	{
-		int spritecount = index;
-
-		// Remove all consecutive gaps at the end of the sprite list.
-		std::deque<int>::iterator it;
-		while ((it = std::find(gaps.begin(), gaps.end(), --spritecount)) != gaps.end())
-			gaps.erase(it);
-
-		next = spritecount + 1;
-		return;
-	}
-	else if (std::find(gaps.begin(), gaps.end(), index) != gaps.end())
-		return; // Don't add the same index to the gap list twice.
-
-
-	// OpenGL won't render any primitive whose vertices are all identical.
-	for (int i = 0; i < 4; i++)
-		sprite[i].x = sprite[i].y = 0;
-
-	// Replace the existing sprite at this index with the dummy sprite.
-	addv(sprite, index);
-
-	gaps.push_back(index);
+	return index;
 }
 
 void SpriteBatch::clear()
 {
 	// Reset the position of the next index.
 	next = 0;
-	gaps.clear();
 }
 
 void *SpriteBatch::lock()

src/modules/graphics/opengl/SpriteBatch.h

 
 // C
 #include <cstring>
-#include <deque>
 
 // LOVE
 #include "common/math.h"
 
 	int add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index = -1);
 	int addq(Quad *quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky, int index = -1);
-	void remove(int index);
 	void clear();
 
 	void *lock();
 	VertexBuffer *array_buf;
 	VertexIndex *element_buf;
 
-	// List of gaps in the SpriteBatch. Checked when adding sprites, and added
-	// to when removing them.
-	std::deque<int> gaps;
-
 }; // SpriteBatch
 
 } // opengl

src/modules/graphics/opengl/wrap_SpriteBatch.cpp

 	return 0;
 }
 
-int w_SpriteBatch_remove(lua_State *L)
-{
-	SpriteBatch *t = luax_checkspritebatch(L, 1);
-	int id = luaL_checkinteger(L, 2);
-	t->remove(id);
-	return 0;
-}
-
 int w_SpriteBatch_clear(lua_State *L)
 {
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
 	{ "addq", w_SpriteBatch_addq },
 	{ "set", w_SpriteBatch_set },
 	{ "setq", w_SpriteBatch_setq },
-	{ "remove", w_SpriteBatch_remove },
 	{ "clear", w_SpriteBatch_clear },
 	{ "bind", w_SpriteBatch_bind },
 	{ "unbind", w_SpriteBatch_unbind },

src/modules/graphics/opengl/wrap_SpriteBatch.h

 int w_SpriteBatch_addq(lua_State *L);
 int w_SpriteBatch_set(lua_State *L);
 int w_SpriteBatch_setq(lua_State *L);
-int w_SpriteBatch_remove(lua_State *L);
 int w_SpriteBatch_clear(lua_State *L);
 int w_SpriteBatch_lock(lua_State *L);
 int w_SpriteBatch_unlock(lua_State *L);