mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
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
This commit is contained in:
@@ -20,9 +20,6 @@
|
||||
|
||||
#include "SpriteBatch.h"
|
||||
|
||||
// STD
|
||||
#include <algorithm> // std::find
|
||||
|
||||
// OpenGL
|
||||
#include "OpenGL.h"
|
||||
|
||||
@@ -97,25 +94,9 @@ SpriteBatch::~SpriteBatch()
|
||||
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);
|
||||
|
||||
@@ -128,37 +109,21 @@ int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, fl
|
||||
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);
|
||||
|
||||
@@ -170,53 +135,19 @@ int SpriteBatch::addq(Quad *quad, float x, float y, float a, float sx, float sy,
|
||||
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()
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
// C
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
|
||||
// LOVE
|
||||
#include "common/math.h"
|
||||
@@ -64,7 +63,6 @@ public:
|
||||
|
||||
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();
|
||||
@@ -127,10 +125,6 @@ private:
|
||||
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
|
||||
|
||||
@@ -101,14 +101,6 @@ int w_SpriteBatch_setq(lua_State *L)
|
||||
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);
|
||||
@@ -198,7 +190,6 @@ static const luaL_Reg functions[] =
|
||||
{ "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 },
|
||||
|
||||
@@ -36,7 +36,6 @@ int w_SpriteBatch_add(lua_State *L);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user