Slightly reduced the CPU usage of SpriteBatch:add, Text:add, and love.graphics.draw(ParticleSystem).

This commit is contained in:
Alex Szpakowski
2015-08-11 18:01:24 -03:00
parent 6d9409a337
commit bfef97c48a
8 changed files with 68 additions and 39 deletions
+24
View File
@@ -221,6 +221,11 @@ Matrix3::Matrix3(const Matrix4 &mat4)
e[8] = mat4elems[10];
}
Matrix3::Matrix3(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);
}
Matrix3::~Matrix3()
{
}
@@ -288,4 +293,23 @@ Matrix3 Matrix3::transposedInverse() const
return m;
}
void Matrix3::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
{
float c = cosf(angle), s = sinf(angle);
// matrix multiplication carried out on paper:
// |1 x| |c -s | |sx | | 1 ky | |1 -ox|
// | 1 y| |s c | | sy | |kx 1 | | 1 -oy|
// | 1| | 1| | 1| | 1| | 1 |
// move rotate scale skew origin
e[0] = c * sx - ky * s * sy; // = a
e[1] = s * sx + ky * c * sy; // = b
e[3] = kx * c * sx - s * sy; // = c
e[4] = kx * s * sx + c * sy; // = d
e[6] = x - ox * e[0] - oy * e[3];
e[7] = y - ox * e[1] - oy * e[4];
e[2] = e[5] = 0.0f;
e[8] = 1.0f;
}
} // love
+21
View File
@@ -185,6 +185,11 @@ public:
**/
Matrix3(const Matrix4 &mat4);
/**
* Creates a new matrix set to a transformation.
**/
Matrix3(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
~Matrix3();
/**
@@ -205,6 +210,22 @@ public:
**/
Matrix3 transposedInverse() const;
/**
* Creates a transformation with a certain position, orientation, scale
* and offset.
*
* @param x The translation along the x-axis.
* @param y The translation along the y-axis.
* @param angle The rotation (rad) around the center with offset (ox,oy).
* @param sx Scale along x-axis.
* @param sy Scale along y-axis.
* @param ox The offset for rotation along the x-axis.
* @param oy The offset for rotation along the y-axis.
* @param kx Shear along x-axis
* @param ky Shear along y-axis
**/
void setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
/**
* Transforms an array of vertices by this matrix.
**/
@@ -849,10 +849,8 @@ void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, flo
OpenGL::TempDebugGroup debuggroup("ParticleSystem draw");
Matrix4 t(x, y, angle, sx, sy, ox, oy, kx, ky);
OpenGL::TempTransform transform(gl);
transform.get() *= t;
transform.get() *= Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
const Vertex *textureVerts = texture->getVertices();
Vertex *pVerts = particleVerts;
@@ -860,6 +858,8 @@ void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, flo
bool useQuads = !quads.empty();
Matrix3 t;
// set the vertex data for each particle (transformation, texcoords, color)
while (p)
{
+4 -6
View File
@@ -86,7 +86,7 @@ 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;
Matrix4 t(x, y, a, sx, sy, ox, oy, kx, ky);
Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
addv(texture->getVertices(), t, (index == -1) ? next : index);
@@ -103,7 +103,7 @@ 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;
Matrix4 t(x, y, a, sx, sy, ox, oy, kx, ky);
Matrix3 t(x, y, a, sx, sy, ox, oy, kx, ky);
addv(quad->getVertices(), t, (index == -1) ? next : index);
@@ -222,10 +222,8 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
OpenGL::TempDebugGroup debuggroup("SpriteBatch draw");
Matrix4 t(x, y, angle, sx, sy, ox, oy, kx, ky);
OpenGL::TempTransform transform(gl);
transform.get() *= t;
transform.get() *= Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky);
gl.bindTexture(*(GLuint *) texture->getHandle());
@@ -254,7 +252,7 @@ void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float
gl.drawElements(GL_TRIANGLES, (GLsizei) quad_indices.getIndexCount(next), quad_indices.getType(), quad_indices.getPointer(0));
}
void SpriteBatch::addv(const Vertex *v, const Matrix4 &m, int index)
void SpriteBatch::addv(const Vertex *v, const Matrix3 &m, int index)
{
// Needed for colors.
Vertex sprite[4] = {v[0], v[1], v[2], v[3]};
+1 -1
View File
@@ -103,7 +103,7 @@ public:
private:
void addv(const Vertex *v, const Matrix4 &m, int index);
void addv(const Vertex *v, const Matrix3 &m, int index);
/**
* Set the color for vertices.
+5 -7
View File
@@ -170,7 +170,7 @@ void Text::set(const std::string &text)
if (text.empty())
return set();
addTextData({text, -1.0f, Font::ALIGN_MAX_ENUM, false, false, Matrix4()});
addTextData({text, -1.0f, Font::ALIGN_MAX_ENUM, false, false, Matrix3()});
}
void Text::set(const std::string &text, float wrap, Font::AlignMode align)
@@ -178,7 +178,7 @@ void Text::set(const std::string &text, float wrap, Font::AlignMode align)
if (text.empty())
return set();
addTextData({text, wrap, align, false, false, Matrix4()});
addTextData({text, wrap, align, false, false, Matrix3()});
}
void Text::set()
@@ -191,7 +191,7 @@ void Text::add(const std::string &text, float x, float y, float angle, float sx,
if (text.empty())
return;
Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
Matrix3 m(x, y, angle, sx, sy, ox, oy, kx, ky);
addTextData({text, -1.0f, Font::ALIGN_MAX_ENUM, true, true, m});
}
@@ -201,7 +201,7 @@ void Text::addf(const std::string &text, float wrap, Font::AlignMode align, floa
if (text.empty())
return;
Matrix4 m(x, y, angle, sx, sy, ox, oy, kx, ky);
Matrix3 m(x, y, angle, sx, sy, ox, oy, kx, ky);
addTextData({text, wrap, align, true, true, m});
}
@@ -230,10 +230,8 @@ void Text::draw(float x, float y, float angle, float sx, float sy, float ox, flo
const size_t tex_offset = offsetof(Font::GlyphVertex, s);
const size_t stride = sizeof(Font::GlyphVertex);
Matrix4 t(ceilf(x), ceilf(y), angle, sx, sy, ox, oy, kx, ky);
OpenGL::TempTransform transform(gl);
transform.get() *= t;
transform.get() *= Matrix4(ceilf(x), ceilf(y), angle, sx, sy, ox, oy, kx, ky);
{
GLBuffer::Bind bind(*vbo);
+1 -1
View File
@@ -74,7 +74,7 @@ private:
Font::AlignMode align;
bool use_matrix;
bool append_vertices;
Matrix4 matrix;
Matrix3 matrix;
};
void uploadVertices(const std::vector<Font::GlyphVertex> &vertices, size_t vertoffset);
@@ -512,15 +512,13 @@ int w_ParticleSystem_setColors(lua_State *L)
// push args[i+2][j+1] onto the stack
lua_rawgeti(L, i + 2, j + 1);
float r = (float) luaL_checknumber(L, -4);
float g = (float) luaL_checknumber(L, -3);
float b = (float) luaL_checknumber(L, -2);
float a = (float) luaL_optnumber(L, -1, 255);
colors[i].r = (float) luaL_checknumber(L, -4);
colors[i].g = (float) luaL_checknumber(L, -3);
colors[i].b = (float) luaL_checknumber(L, -2);
colors[i].a = (float) luaL_optnumber(L, -1, 255);
// pop the color components from the stack
lua_pop(L, 4);
colors[i] = Colorf(r, g, b, a);
}
t->setColor(colors);
@@ -538,22 +536,12 @@ int w_ParticleSystem_setColors(lua_State *L)
std::vector<Colorf> colors(nColors);
if (nColors == 1)
for (int i = 0; i < nColors; ++i)
{
colors[0].r = (float) luaL_checknumber(L, 2);
colors[0].g = (float) luaL_checknumber(L, 3);
colors[0].b = (float) luaL_checknumber(L, 4);
colors[0].a = (float) luaL_optnumber(L, 5, 255);
}
else
{
for (int i = 0; i < nColors; ++i)
{
colors[i].r = (float) luaL_checknumber(L, 1 + i*4 + 1);
colors[i].g = (float) luaL_checknumber(L, 1 + i*4 + 2);
colors[i].b = (float) luaL_checknumber(L, 1 + i*4 + 3);
colors[i].a = (float) luaL_checknumber(L, 1 + i*4 + 4);
}
colors[i].r = (float) luaL_checknumber(L, 1 + i*4 + 1);
colors[i].g = (float) luaL_checknumber(L, 1 + i*4 + 2);
colors[i].b = (float) luaL_checknumber(L, 1 + i*4 + 3);
colors[i].a = (float) luaL_checknumber(L, 1 + i*4 + 4);
}
t->setColor(colors);