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
@@ -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);