Browse Source

Consolidated some duplicate code in SpriteBatch:add.

Alex Szpakowski 11 years ago
parent
commit
c6896dfc4e

+ 5 - 0
src/common/Matrix.cpp

@@ -37,6 +37,11 @@ Matrix::Matrix()
 	setIdentity();
 }
 
+Matrix::Matrix(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
+{
+	setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
+}
+
 Matrix::~Matrix()
 {
 }

+ 5 - 0
src/common/Matrix.h

@@ -41,6 +41,11 @@ public:
 	 **/
 	Matrix();
 
+	/**
+	 * Creates a new matrix set to a transformation.
+	 **/
+	Matrix(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
+
 	/**
 	 * Destructor.
 	 **/

+ 15 - 30
src/modules/graphics/opengl/SpriteBatch.cpp

@@ -99,20 +99,9 @@ int SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, fl
 	if ((index == -1 && next >= size) || index < -1 || index >= size)
 		return -1;
 
-	Vertex sprite[4];
+	Matrix t(x, y, a, sx, sy, ox, oy, kx, ky);
 
-	// Needed for colors.
-	memcpy(sprite, texture->getVertices(), sizeof(Vertex) * 4);
-
-	// Transform.
-	Matrix t;
-	t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
-	t.transform(sprite, sprite, 4);
-
-	if (color)
-		setColorv(sprite, *color);
-
-	addv(sprite, (index == -1) ? next : index);
+	addv(texture->getVertices(), t, (index == -1) ? next : index);
 
 	// Increment counter.
 	if (index == -1)
@@ -127,19 +116,9 @@ int SpriteBatch::addq(Quad *quad, float x, float y, float a, float sx, float sy,
 	if ((index == -1 && next >= size) || index < -1 || index >= next)
 		return -1;
 
-	Vertex sprite[4];
-
-	// Needed for colors.
-	memcpy(sprite, quad->getVertices(), sizeof(Vertex) * 4);
+	Matrix t(x, y, a, sx, sy, ox, oy, kx, ky);
 
-	Matrix t;
-	t.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
-	t.transform(sprite, sprite, 4);
-
-	if (color)
-		setColorv(sprite, *color);
-
-	addv(sprite, (index == -1) ? next : index);
+	addv(quad->getVertices(), t, (index == -1) ? next : index);
 
 	// Increment counter.
 	if (index == -1)
@@ -254,8 +233,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
 	if (next == 0)
 		return;
 
-	static Matrix t;
-	t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
+	Matrix t(x, y, angle, sx, sy, ox, oy, kx, ky);
 
 	OpenGL::TempTransform transform(gl);
 	transform.get() *= t;
@@ -299,9 +277,16 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
 	texture->postdraw();
 }
 
-void SpriteBatch::addv(const Vertex *v, int index)
+void SpriteBatch::addv(const Vertex *v, const Matrix &m, int index)
 {
-	static const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
+	// Needed for colors.
+	Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
+	const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
+
+	m.transform(sprite, sprite, 4);
+
+	if (color)
+		setColorv(sprite, *color);
 
 	VertexBuffer::Bind bind(*array_buf);
 
@@ -309,7 +294,7 @@ void SpriteBatch::addv(const Vertex *v, int index)
 	// on draw.)
 	array_buf->map();
 
-	array_buf->fill(index * sprite_size, sprite_size, v);
+	array_buf->fill(index * sprite_size, sprite_size, sprite);
 
 	buffer_used_offset = std::min(buffer_used_offset, index * sprite_size);
 	buffer_used_size = std::max(buffer_used_size, (index + 1) * sprite_size - buffer_used_offset);

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

@@ -113,7 +113,7 @@ public:
 
 private:
 
-	void addv(const Vertex *v, int index);
+	void addv(const Vertex *v, const Matrix &m, int index);
 
 	/**
 	 * Set the color for vertices.