From d4e701b434bdf40946cce65211e9f5079d9c1808 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 5 Mar 2016 12:59:00 -0400 Subject: [PATCH] SpriteBatch:add will now automatically increase the maximum size of the SpriteBatch if there's no more room. --HG-- branch : minor --- src/modules/graphics/opengl/SpriteBatch.cpp | 24 +++++++++++-------- src/modules/graphics/opengl/SpriteBatch.h | 14 +++++------ .../graphics/opengl/wrap_SpriteBatch.cpp | 9 ------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/modules/graphics/opengl/SpriteBatch.cpp b/src/modules/graphics/opengl/SpriteBatch.cpp index 7c5f4fc6b..5edcad539 100644 --- a/src/modules/graphics/opengl/SpriteBatch.cpp +++ b/src/modules/graphics/opengl/SpriteBatch.cpp @@ -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*/) { - // Only do this if there's a free slot. - if ((index == -1 && next >= size) || index < -1 || index >= size) - return -1; + if (index < -1 || index >= size) + throw love::Exception("Invalid sprite index: %d", index + 1); + + if (index == -1 && next >= size) + setBufferSize(size * 2); 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*/) { - // Only do this if there's a free slot. - if ((index == -1 && next >= size) || index < -1 || index >= next) - return -1; + if (index < -1 || index >= size) + throw love::Exception("Invalid sprite index: %d", index + 1); + + if (index == -1 && next >= size) + setBufferSize(size * 2); 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 newattrib = {}; - if (mesh->getVertexCount() < (size_t) getBufferSize() * 4) - throw love::Exception("Mesh has too few vertices to be attached to this SpriteBatch (at least %d vertices are required)", 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)", next*4); auto it = attached_attributes.find(name); 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(); - // 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. - 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()); int location = mesh->bindAttributeToShaderInput(it.second.index, it.first); diff --git a/src/modules/graphics/opengl/SpriteBatch.h b/src/modules/graphics/opengl/SpriteBatch.h index 660406ec7..8a40581dd 100644 --- a/src/modules/graphics/opengl/SpriteBatch.h +++ b/src/modules/graphics/opengl/SpriteBatch.h @@ -91,13 +91,7 @@ public: int getCount() const; /** - * Sets the total number of sprites this SpriteBatch can hold. - * Leaves existing sprite data intact when possible. - **/ - void setBufferSize(int newsize); - - /** - * Get the total number of sprites this SpriteBatch can hold. + * Get the total number of sprites this SpriteBatch can currently hold. **/ int getBufferSize() const; @@ -118,6 +112,12 @@ private: 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); /** diff --git a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp index eeb695e7c..325a6949b 100644 --- a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp +++ b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp @@ -188,14 +188,6 @@ int w_SpriteBatch_getCount(lua_State *L) 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) { SpriteBatch *t = luax_checkspritebatch(L, 1); @@ -224,7 +216,6 @@ static const luaL_Reg w_SpriteBatch_functions[] = { "setColor", w_SpriteBatch_setColor }, { "getColor", w_SpriteBatch_getColor }, { "getCount", w_SpriteBatch_getCount }, - { "setBufferSize", w_SpriteBatch_setBufferSize }, { "getBufferSize", w_SpriteBatch_getBufferSize }, { "attachAttribute", w_SpriteBatch_attachAttribute }, { 0, 0 }