mirror of
https://github.com/love2d/love.git
synced 2026-08-19 20:19:42 +02:00
SpriteBatch:add will now automatically increase the maximum size of the SpriteBatch if there's no more room.
--HG-- branch : minor
This commit is contained in:
@@ -66,9 +66,11 @@ 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*/)
|
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 || index >= size)
|
||||||
if ((index == -1 && next >= size) || index < -1 || index >= size)
|
throw love::Exception("Invalid sprite index: %d", index + 1);
|
||||||
return -1;
|
|
||||||
|
if (index == -1 && next >= size)
|
||||||
|
setBufferSize(size * 2);
|
||||||
|
|
||||||
Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
|
Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
|
||||||
|
|
||||||
@@ -83,9 +85,11 @@ int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, fl
|
|||||||
|
|
||||||
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*/)
|
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 || index >= size)
|
||||||
if ((index == -1 && next >= size) || index < -1 || index >= next)
|
throw love::Exception("Invalid sprite index: %d", index + 1);
|
||||||
return -1;
|
|
||||||
|
if (index == -1 && next >= size)
|
||||||
|
setBufferSize(size * 2);
|
||||||
|
|
||||||
Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
|
Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
|
||||||
|
|
||||||
@@ -198,8 +202,8 @@ void SpriteBatch::attachAttribute(const std::string &name, Mesh *mesh)
|
|||||||
AttachedAttribute oldattrib = {};
|
AttachedAttribute oldattrib = {};
|
||||||
AttachedAttribute newattrib = {};
|
AttachedAttribute newattrib = {};
|
||||||
|
|
||||||
if (mesh->getVertexCount() < (size_t) getBufferSize() * 4)
|
if (mesh->getVertexCount() < (size_t) next * 4)
|
||||||
throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)", getBufferSize()*4);
|
throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)", next*4);
|
||||||
|
|
||||||
auto it = attached_attributes.find(name);
|
auto it = attached_attributes.find(name);
|
||||||
if (it != attached_attributes.end())
|
if (it != attached_attributes.end())
|
||||||
@@ -256,9 +260,9 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
|
|||||||
{
|
{
|
||||||
Mesh *mesh = it.second.mesh.get();
|
Mesh *mesh = it.second.mesh.get();
|
||||||
|
|
||||||
// We have to do this check here as well because setBufferSize can be
|
// We have to do this check here as wll because setBufferSize can be
|
||||||
// called after attachAttribute.
|
// called after attachAttribute.
|
||||||
if (mesh->getVertexCount() < (size_t) getBufferSize() * 4)
|
if (mesh->getVertexCount() < (size_t) next * 4)
|
||||||
throw love::Exception("Mesh with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str());
|
throw love::Exception("Mesh with attribute '%s' attached to this SpriteBatch has too few vertices", it.first.c_str());
|
||||||
|
|
||||||
int location = mesh->bindAttributeToShaderInput(it.second.index, it.first);
|
int location = mesh->bindAttributeToShaderInput(it.second.index, it.first);
|
||||||
|
|||||||
@@ -91,13 +91,7 @@ public:
|
|||||||
int getCount() const;
|
int getCount() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the total number of sprites this SpriteBatch can hold.
|
* Get the total number of sprites this SpriteBatch can currently hold.
|
||||||
* Leaves existing sprite data intact when possible.
|
|
||||||
**/
|
|
||||||
void setBufferSize(int newsize);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the total number of sprites this SpriteBatch can hold.
|
|
||||||
**/
|
**/
|
||||||
int getBufferSize() const;
|
int getBufferSize() const;
|
||||||
|
|
||||||
@@ -118,6 +112,12 @@ private:
|
|||||||
int index;
|
int index;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the total number of sprites this SpriteBatch can hold.
|
||||||
|
* Leaves existing sprite data intact when possible.
|
||||||
|
**/
|
||||||
|
void setBufferSize(int newsize);
|
||||||
|
|
||||||
void addv(const Vertex *v, const Matrix3 &m, int index);
|
void addv(const Vertex *v, const Matrix3 &m, int index);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -188,14 +188,6 @@ int w_SpriteBatch_getCount(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int w_SpriteBatch_setBufferSize(lua_State *L)
|
|
||||||
{
|
|
||||||
SpriteBatch *t = luax_checkspritebatch(L, 1);
|
|
||||||
int size = (int) luaL_checknumber(L, 2);
|
|
||||||
luax_catchexcept(L, [&]() {t->setBufferSize(size); });
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int w_SpriteBatch_getBufferSize(lua_State *L)
|
int w_SpriteBatch_getBufferSize(lua_State *L)
|
||||||
{
|
{
|
||||||
SpriteBatch *t = luax_checkspritebatch(L, 1);
|
SpriteBatch *t = luax_checkspritebatch(L, 1);
|
||||||
@@ -224,7 +216,6 @@ static const luaL_Reg w_SpriteBatch_functions[] =
|
|||||||
{ "setColor", w_SpriteBatch_setColor },
|
{ "setColor", w_SpriteBatch_setColor },
|
||||||
{ "getColor", w_SpriteBatch_getColor },
|
{ "getColor", w_SpriteBatch_getColor },
|
||||||
{ "getCount", w_SpriteBatch_getCount },
|
{ "getCount", w_SpriteBatch_getCount },
|
||||||
{ "setBufferSize", w_SpriteBatch_setBufferSize },
|
|
||||||
{ "getBufferSize", w_SpriteBatch_getBufferSize },
|
{ "getBufferSize", w_SpriteBatch_getBufferSize },
|
||||||
{ "attachAttribute", w_SpriteBatch_attachAttribute },
|
{ "attachAttribute", w_SpriteBatch_attachAttribute },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
|
|||||||
Reference in New Issue
Block a user