Added locking to SpriteBatch, and conf.lua.

This commit is contained in:
rude
2009-07-27 15:17:54 +02:00
parent 4f32377efa
commit 5c068db487
7 changed files with 396 additions and 248 deletions
+34 -9
View File
@@ -33,7 +33,7 @@ namespace graphics
namespace opengl
{
SpriteBatch::SpriteBatch(Image * image, int size, int usage)
: image(image), size(size), next(0), usage(usage)
: image(image), size(size), next(0), usage(usage), lockp(0)
{
image->retain();
@@ -92,17 +92,24 @@ namespace opengl
// Transform.
Matrix t;
t.translate(x, y);
t.scale(sx, sy);
t.rotate(a);
t.setTransformation(x, y, a, sx, sy, ox, oy);
t.transform(v, v, 4);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferSubData(GL_ARRAY_BUFFER, (next*4)*sizeof(vertex), sizeof(vertex)*4, v);
glBindBuffer(GL_ARRAY_BUFFER, 0);
if(lockp != 0)
{
// Copy into mapped memory if buffer is locked.
memcpy(lockp + (next*4), v, sizeof(vertex)*4);
}
else
{
// ... use glBufferSubData otherwise.
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferSubData(GL_ARRAY_BUFFER, (next*4)*sizeof(vertex), sizeof(vertex)*4, v);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// Increment counter.
next++;
next++;
}
}
@@ -112,10 +119,28 @@ namespace opengl
next = 0;
}
void * SpriteBatch::lock()
{
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
lockp = (vertex *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
return lockp;
}
void SpriteBatch::unlock()
{
glUnmapBuffer(GL_ARRAY_BUFFER);
lockp = 0;
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void SpriteBatch::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
{
static Matrix t;
glPushMatrix();
glTranslatef(x, y, 0);
t.setTransformation(x, y, angle, sx, sy, ox, oy);
glMultMatrixf((const GLfloat*)t.getElements());
image->bind();
@@ -68,6 +68,9 @@ namespace opengl
int usage;
int gl_usage;
// If the buffer is locked, this pointer is nonzero.
vertex * lockp;
public:
enum UsageHint
@@ -85,6 +88,9 @@ namespace opengl
//void addf(float x, float y, float a, float sx, float sy, float ox, float oy, Frame * frame);
void clear();
void * lock();
void unlock();
// Implements Drawable.
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
@@ -52,9 +52,25 @@ namespace opengl
return 0;
}
int _wrap_SpriteBatch_lock(lua_State * L)
{
SpriteBatch * t = luax_checkspritebatch(L, 1);
lua_pushlightuserdata(L, t->lock());
return 1;
}
int _wrap_SpriteBatch_unlock(lua_State * L)
{
SpriteBatch * t = luax_checkspritebatch(L, 1);
t->unlock();
return 0;
}
static const luaL_Reg wrap_SpriteBatch_functions[] = {
{ "add", _wrap_SpriteBatch_add },
{ "clear", _wrap_SpriteBatch_clear },
{ "lock", _wrap_SpriteBatch_lock },
{ "unlock", _wrap_SpriteBatch_unlock },
{ 0, 0 }
};
@@ -33,6 +33,8 @@ namespace opengl
SpriteBatch * luax_checkspritebatch(lua_State * L, int idx);
int _wrap_SpriteBatch_add(lua_State * L);
int _wrap_SpriteBatch_clear(lua_State * L);
int _wrap_SpriteBatch_lock(lua_State * L);
int _wrap_SpriteBatch_unlock(lua_State * L);
int wrap_SpriteBatch_open(lua_State * L);
} // opengl