diff --git a/src/modules/graphics/opengl/SpriteBatch.cpp b/src/modules/graphics/opengl/SpriteBatch.cpp index 3976931b5..9889c0d8e 100644 --- a/src/modules/graphics/opengl/SpriteBatch.cpp +++ b/src/modules/graphics/opengl/SpriteBatch.cpp @@ -99,50 +99,54 @@ namespace opengl delete element_buf; } - void SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky) + 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(next < size) - { - // Needed for texture coordinates. - memcpy(sprite, image->getVertices(), sizeof(vertex)*4); + if((index == -1 && next >= size) || index < -1 || index >= size) + return -1; - // Transform. - Matrix t; - t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky); - t.transform(sprite, sprite, 4); + // Needed for colors. + memcpy(sprite, image->getVertices(), sizeof(vertex)*4); - if (color) - setColorv(sprite, *color); + // Transform. + Matrix t; + t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky); + t.transform(sprite, sprite, 4); - addv(sprite); + if (color) + setColorv(sprite, *color); - // Increment counter. - next++; - } + addv(sprite, (index == -1 ? next : index)); + + // Increment counter. + if (index == -1) + return next++; + return index; } - void SpriteBatch::addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky) + 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(next < size) - { - // Needed for colors. - memcpy(sprite, quad->getVertices(), sizeof(vertex)*4); + if((index == -1 && next >= size) || index < -1 || index >= next) + return -1; - // Transform. - Matrix t; - t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky); - t.transform(sprite, sprite, 4); + // Needed for colors. + memcpy(sprite, quad->getVertices(), sizeof(vertex)*4); - if (color) - setColorv(sprite, *color); + // Transform. + Matrix t; + t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky); + t.transform(sprite, sprite, 4); - addv(sprite); + if (color) + setColorv(sprite, *color); - // Increment counter. - next++; - } + addv(sprite, (index == -1 ? next : index)); + + // Increment counter. + if (index == -1) + return next++; + return index; } void SpriteBatch::clear() @@ -226,13 +230,13 @@ namespace opengl glPopMatrix(); } - void SpriteBatch::addv(const vertex * v) + void SpriteBatch::addv(const vertex * v, int index) { int sprite_size = sizeof(vertex) * 4; VertexArray::Bind bind(*array_buf); - array_buf->fill(next * sprite_size, sprite_size, v); + array_buf->fill(index * sprite_size, sprite_size, v); } void SpriteBatch::setColorv(vertex * v, const Color & color) diff --git a/src/modules/graphics/opengl/SpriteBatch.h b/src/modules/graphics/opengl/SpriteBatch.h index 40300ce9a..9bab62b9a 100644 --- a/src/modules/graphics/opengl/SpriteBatch.h +++ b/src/modules/graphics/opengl/SpriteBatch.h @@ -83,8 +83,8 @@ namespace opengl SpriteBatch(Image * image, int size, int usage); virtual ~SpriteBatch(); - void add(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky); - void addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky); + 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 clear(); void * lock(); @@ -115,7 +115,7 @@ namespace opengl private: - void addv(const vertex * v); + void addv(const vertex * v, int index); /** * Set the color for vertices. diff --git a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp index ac22ad18d..f1df5043f 100644 --- a/src/modules/graphics/opengl/wrap_SpriteBatch.cpp +++ b/src/modules/graphics/opengl/wrap_SpriteBatch.cpp @@ -1,133 +1,171 @@ -/** -* Copyright (c) 2006-2011 LOVE Development Team -* -* This software is provided 'as-is', without any express or implied -* warranty. In no event will the authors be held liable for any damages -* arising from the use of this software. -* -* Permission is granted to anyone to use this software for any purpose, -* including commercial applications, and to alter it and redistribute it -* freely, subject to the following restrictions: -* -* 1. The origin of this software must not be misrepresented; you must not -* claim that you wrote the original software. If you use this software -* in a product, an acknowledgment in the product documentation would be -* appreciated but is not required. -* 2. Altered source versions must be plainly marked as such, and must not be -* misrepresented as being the original software. -* 3. This notice may not be removed or altered from any source distribution. -**/ - -#include "wrap_SpriteBatch.h" - -namespace love -{ -namespace graphics -{ -namespace opengl -{ - SpriteBatch * luax_checkspritebatch(lua_State * L, int idx) - { - return luax_checktype(L, idx, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T); - } - - int w_SpriteBatch_add(lua_State * L) - { - SpriteBatch * t = luax_checkspritebatch(L, 1); - float x = (float)luaL_optnumber(L, 2, 0.0f); - float y = (float)luaL_optnumber(L, 3, 0.0f); - float angle = (float)luaL_optnumber(L, 4, 0.0f); - float sx = (float)luaL_optnumber(L, 5, 1.0f); - float sy = (float)luaL_optnumber(L, 6, sx); - float ox = (float)luaL_optnumber(L, 7, 0); - float oy = (float)luaL_optnumber(L, 8, 0); - float kx = (float)luaL_optnumber(L, 10, 0); - float ky = (float)luaL_optnumber(L, 11, 0); - t->add(x, y, angle, sx, sy, ox, oy, kx, ky); - return 0; - } - - int w_SpriteBatch_addq(lua_State * L) - { - SpriteBatch * t = luax_checkspritebatch(L, 1); - Quad * q = luax_checktype(L, 2, "Quad", GRAPHICS_QUAD_T); - float x = (float)luaL_optnumber(L, 3, 0.0f); - float y = (float)luaL_optnumber(L, 4, 0.0f); - float angle = (float)luaL_optnumber(L, 5, 0.0f); - float sx = (float)luaL_optnumber(L, 6, 1.0f); - float sy = (float)luaL_optnumber(L, 7, sx); - float ox = (float)luaL_optnumber(L, 8, 0); - float oy = (float)luaL_optnumber(L, 9, 0); - float kx = (float)luaL_optnumber(L, 10, 0); - float ky = (float)luaL_optnumber(L, 11, 0); - t->addq(q, x, y, angle, sx, sy, ox, oy, kx, ky); - return 0; - } - - int w_SpriteBatch_clear(lua_State * L) - { - SpriteBatch * t = luax_checkspritebatch(L, 1); - t->clear(); - return 0; - } - - int w_SpriteBatch_bind(lua_State * L) - { - SpriteBatch * t = luax_checkspritebatch(L, 1); - t->lock(); - return 0; - } - - int w_SpriteBatch_unbind(lua_State * L) - { - SpriteBatch * t = luax_checkspritebatch(L, 1); - t->unlock(); - return 0; - } - - int w_SpriteBatch_setImage(lua_State * L) - { - SpriteBatch * t = luax_checkspritebatch(L, 1); - Image * image = luax_checktype(L, 2, "Image", GRAPHICS_IMAGE_T); - t->setImage(image); - return 0; - } - - int w_SpriteBatch_setColor(lua_State * L) - { - SpriteBatch * t = luax_checkspritebatch(L, 1); - - if (lua_gettop(L) <= 1) - t->setColor(); - else - { - Color c; +/** +* Copyright (c) 2006-2011 LOVE Development Team +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any damages +* arising from the use of this software. +* +* Permission is granted to anyone to use this software for any purpose, +* including commercial applications, and to alter it and redistribute it +* freely, subject to the following restrictions: +* +* 1. The origin of this software must not be misrepresented; you must not +* claim that you wrote the original software. If you use this software +* in a product, an acknowledgment in the product documentation would be +* appreciated but is not required. +* 2. Altered source versions must be plainly marked as such, and must not be +* misrepresented as being the original software. +* 3. This notice may not be removed or altered from any source distribution. +**/ + +#include "wrap_SpriteBatch.h" + +namespace love +{ +namespace graphics +{ +namespace opengl +{ + SpriteBatch * luax_checkspritebatch(lua_State * L, int idx) + { + return luax_checktype(L, idx, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T); + } + + int w_SpriteBatch_add(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + float x = (float)luaL_optnumber(L, 2, 0.0f); + float y = (float)luaL_optnumber(L, 3, 0.0f); + float angle = (float)luaL_optnumber(L, 4, 0.0f); + float sx = (float)luaL_optnumber(L, 5, 1.0f); + float sy = (float)luaL_optnumber(L, 6, sx); + float ox = (float)luaL_optnumber(L, 7, 0); + float oy = (float)luaL_optnumber(L, 8, 0); + float kx = (float)luaL_optnumber(L, 9, 0); + float ky = (float)luaL_optnumber(L, 10, 0); + lua_pushnumber(L, t->add(x, y, angle, sx, sy, ox, oy, kx, ky)); + return 1; + } + + int w_SpriteBatch_addq(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + Quad * q = luax_checktype(L, 2, "Quad", GRAPHICS_QUAD_T); + float x = (float)luaL_optnumber(L, 3, 0.0f); + float y = (float)luaL_optnumber(L, 4, 0.0f); + float angle = (float)luaL_optnumber(L, 5, 0.0f); + float sx = (float)luaL_optnumber(L, 6, 1.0f); + float sy = (float)luaL_optnumber(L, 7, sx); + float ox = (float)luaL_optnumber(L, 8, 0); + float oy = (float)luaL_optnumber(L, 9, 0); + float kx = (float)luaL_optnumber(L, 10, 0); + float ky = (float)luaL_optnumber(L, 11, 0); + lua_pushnumber(L, t->addq(q, x, y, angle, sx, sy, ox, oy, kx, ky)); + return 1; + } + + int w_SpriteBatch_set(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + int index = luaL_checkinteger(L, 2); + float x = (float)luaL_optnumber(L, 3, 0.0f); + float y = (float)luaL_optnumber(L, 4, 0.0f); + float angle = (float)luaL_optnumber(L, 5, 0.0f); + float sx = (float)luaL_optnumber(L, 6, 1.0f); + float sy = (float)luaL_optnumber(L, 7, sx); + float ox = (float)luaL_optnumber(L, 8, 0); + float oy = (float)luaL_optnumber(L, 9, 0); + float kx = (float)luaL_optnumber(L, 10, 0); + float ky = (float)luaL_optnumber(L, 11, 0); + t->add(x, y, angle, sx, sy, ox, oy, kx, ky, index); + return 0; + } + + int w_SpriteBatch_setq(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + int index = luaL_checkinteger(L, 2); + Quad * q = luax_checktype(L, 3, "Quad", GRAPHICS_QUAD_T); + float x = (float)luaL_optnumber(L, 4, 0.0f); + float y = (float)luaL_optnumber(L, 5, 0.0f); + float angle = (float)luaL_optnumber(L, 6, 0.0f); + float sx = (float)luaL_optnumber(L, 7, 1.0f); + float sy = (float)luaL_optnumber(L, 8, sx); + float ox = (float)luaL_optnumber(L, 9, 0); + float oy = (float)luaL_optnumber(L, 10, 0); + float kx = (float)luaL_optnumber(L, 11, 0); + float ky = (float)luaL_optnumber(L, 12, 0); + t->addq(q, x, y, angle, sx, sy, ox, oy, kx, ky, index); + return 0; + } + + + int w_SpriteBatch_clear(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + t->clear(); + return 0; + } + + int w_SpriteBatch_bind(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + t->lock(); + return 0; + } + + int w_SpriteBatch_unbind(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + t->unlock(); + return 0; + } + + int w_SpriteBatch_setImage(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + Image * image = luax_checktype(L, 2, "Image", GRAPHICS_IMAGE_T); + t->setImage(image); + return 0; + } + + int w_SpriteBatch_setColor(lua_State * L) + { + SpriteBatch * t = luax_checkspritebatch(L, 1); + + if (lua_gettop(L) <= 1) + t->setColor(); + else + { + Color c; c.r = (unsigned char)luaL_checkint(L, 2); c.g = (unsigned char)luaL_checkint(L, 3); c.b = (unsigned char)luaL_checkint(L, 4); - c.a = (unsigned char)luaL_optint(L, 5, 255); - t->setColor(c); - } - - return 0; - } - - static const luaL_Reg functions[] = { - { "add", w_SpriteBatch_add }, - { "addq", w_SpriteBatch_addq }, - { "clear", w_SpriteBatch_clear }, - { "bind", w_SpriteBatch_bind }, - { "unbind", w_SpriteBatch_unbind }, - { "setImage", w_SpriteBatch_setImage }, - { "setColor", w_SpriteBatch_setColor }, - { 0, 0 } - }; - - int luaopen_spritebatch(lua_State * L) - { - return luax_register_type(L, "SpriteBatch", functions); - } - -} // opengl -} // graphics -} // love + c.a = (unsigned char)luaL_optint(L, 5, 255); + t->setColor(c); + } + + return 0; + } + + static const luaL_Reg functions[] = { + { "add", w_SpriteBatch_add }, + { "addq", w_SpriteBatch_addq }, + { "set", w_SpriteBatch_set }, + { "setq", w_SpriteBatch_setq }, + { "clear", w_SpriteBatch_clear }, + { "bind", w_SpriteBatch_bind }, + { "unbind", w_SpriteBatch_unbind }, + { "setImage", w_SpriteBatch_setImage }, + { "setColor", w_SpriteBatch_setColor }, + { 0, 0 } + }; + + int luaopen_spritebatch(lua_State * L) + { + return luax_register_type(L, "SpriteBatch", functions); + } + +} // opengl +} // graphics +} // love diff --git a/src/modules/graphics/opengl/wrap_SpriteBatch.h b/src/modules/graphics/opengl/wrap_SpriteBatch.h index d2ced3d63..0a38477e5 100644 --- a/src/modules/graphics/opengl/wrap_SpriteBatch.h +++ b/src/modules/graphics/opengl/wrap_SpriteBatch.h @@ -33,6 +33,8 @@ namespace opengl SpriteBatch * luax_checkspritebatch(lua_State * L, int idx); 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_clear(lua_State * L); int w_SpriteBatch_lock(lua_State * L); int w_SpriteBatch_unlock(lua_State * L);