Browse Source

Changed SpriteBatch:add/set to do an internal bind, and love.graphics.draw(SpriteBatch) to do an internal unbind. Removed SpriteBatch:bind/unbind, and added SpriteBatch:flush.

Resolves issue #886.

--HG--
branch : minor
Alex Szpakowski 11 years ago
parent
commit
9c717cc3a3

+ 32 - 19
src/modules/graphics/opengl/SpriteBatch.cpp

@@ -160,17 +160,9 @@ void SpriteBatch::clear()
 	next = 0;
 }
 
-void *SpriteBatch::lock()
+void SpriteBatch::flush()
 {
 	VertexBuffer::Bind bind(*array_buf);
-
-	return array_buf->map();
-}
-
-void SpriteBatch::unlock()
-{
-	VertexBuffer::Bind bind(*array_buf);
-
 	array_buf->unmap();
 }
 
@@ -219,14 +211,18 @@ void SpriteBatch::setBufferSize(int newsize)
 	if (newsize == size)
 		return;
 
-	// Map (lock) the old VertexBuffer to get a pointer to its data.
-	void *old_data = lock();
+	// Map the old VertexBuffer to get a pointer to its data.
+	void *old_data = nullptr;
+	{
+		VertexBuffer::Bind bind(*array_buf);
+		old_data = array_buf->map();
+	}
 
 	size_t vertex_size = sizeof(Vertex) * 4 * newsize;
 
-	VertexBuffer *new_array_buf = 0;
-	VertexIndex *new_element_buf = 0;
-	void *new_data = 0;
+	VertexBuffer *new_array_buf = nullptr;
+	VertexIndex *new_element_buf = nullptr;
+	void *new_data = nullptr;
 
 	try
 	{
@@ -241,7 +237,12 @@ void SpriteBatch::setBufferSize(int newsize)
 	{
 		delete new_array_buf;
 		delete new_element_buf;
-		unlock();
+
+		{
+			VertexBuffer::Bind bind(*array_buf);
+			array_buf->unmap();
+		}
+
 		throw;
 	}
 
@@ -258,8 +259,11 @@ void SpriteBatch::setBufferSize(int newsize)
 
 	next = std::min(next, newsize);
 
-	// But we should unmap (unlock) the new one!
-	unlock();
+	// But we should unmap the new one!
+	{
+		VertexBuffer::Bind bind(*new_array_buf);
+		new_array_buf->unmap();
+	}
 }
 
 int SpriteBatch::getBufferSize() const
@@ -269,7 +273,7 @@ int SpriteBatch::getBufferSize() const
 
 void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
 {
-	const size_t vertex_offset = offsetof(Vertex, x);
+	const size_t pos_offset   = offsetof(Vertex, x);
 	const size_t texel_offset = offsetof(Vertex, s);
 	const size_t color_offset = offsetof(Vertex, r);
 
@@ -288,6 +292,9 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
 	VertexBuffer::Bind array_bind(*array_buf);
 	VertexBuffer::Bind element_bind(*element_buf->getVertexBuffer());
 
+	// Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
+	array_buf->unmap();
+
 	Color curcolor = gl.getColor();
 
 	// Apply per-sprite color, if a color is set.
@@ -298,7 +305,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
 	}
 
 	glEnableClientState(GL_VERTEX_ARRAY);
-	glVertexPointer(2, GL_FLOAT, sizeof(Vertex), array_buf->getPointer(vertex_offset));
+	glVertexPointer(2, GL_FLOAT, sizeof(Vertex), array_buf->getPointer(pos_offset));
 
 	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 	glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), array_buf->getPointer(texel_offset));
@@ -323,7 +330,13 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
 void SpriteBatch::addv(const Vertex *v, int index)
 {
 	static const int sprite_size = 4 * sizeof(Vertex); // bytecount
+
 	VertexBuffer::Bind bind(*array_buf);
+
+	// Always keep the VBO mapped when adding data for now (it'll be unmapped
+	// on draw.)
+	array_buf->map();
+
 	array_buf->fill(index * sprite_size, sprite_size, v);
 }
 

+ 1 - 2
src/modules/graphics/opengl/SpriteBatch.h

@@ -65,8 +65,7 @@ public:
 	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();
-	void unlock();
+	void flush();
 
 	void setTexture(Texture *newtexture);
 	Texture *getTexture();

+ 3 - 11
src/modules/graphics/opengl/wrap_SpriteBatch.cpp

@@ -118,17 +118,10 @@ int w_SpriteBatch_clear(lua_State *L)
 	return 0;
 }
 
-int w_SpriteBatch_bind(lua_State *L)
+int w_SpriteBatch_flush(lua_State *L)
 {
 	SpriteBatch *t = luax_checkspritebatch(L, 1);
-	EXCEPT_GUARD(t->lock();)
-	return 0;
-}
-
-int w_SpriteBatch_unbind(lua_State *L)
-{
-	SpriteBatch *t = luax_checkspritebatch(L, 1);
-	t->unlock();
+	t->flush();
 	return 0;
 }
 
@@ -239,8 +232,7 @@ static const luaL_Reg functions[] =
 	{ "add", w_SpriteBatch_add },
 	{ "set", w_SpriteBatch_set },
 	{ "clear", w_SpriteBatch_clear },
-	{ "bind", w_SpriteBatch_bind },
-	{ "unbind", w_SpriteBatch_unbind },
+	{ "flush", w_SpriteBatch_flush },
 	{ "setTexture", w_SpriteBatch_setTexture },
 	{ "getTexture", w_SpriteBatch_getTexture },
 	{ "setColor", w_SpriteBatch_setColor },

+ 1 - 2
src/modules/graphics/opengl/wrap_SpriteBatch.h

@@ -37,8 +37,7 @@ int w_SpriteBatch_addg(lua_State *L);
 int w_SpriteBatch_set(lua_State *L);
 int w_SpriteBatch_setg(lua_State *L);
 int w_SpriteBatch_clear(lua_State *L);
-int w_SpriteBatch_bind(lua_State *L);
-int w_SpriteBatch_unbind(lua_State *L);
+int w_SpriteBatch_flush(lua_State *L);
 int w_SpriteBatch_setTexture(lua_State *L);
 int w_SpriteBatch_getTexture(lua_State *L);
 int w_SpriteBatch_setColor(lua_State *L);