diff --git a/src/modules/graphics/opengl/SpriteBatch.cpp b/src/modules/graphics/opengl/SpriteBatch.cpp index 83f65c6c4..1f968658d 100644 --- a/src/modules/graphics/opengl/SpriteBatch.cpp +++ b/src/modules/graphics/opengl/SpriteBatch.cpp @@ -52,6 +52,22 @@ namespace opengl indices[i*6+5] = 3+(i*4); } + loadVolatile(); + } + + SpriteBatch::~SpriteBatch() + { + image->release(); + + if(vbo[0] != 0 && vbo[1] != 0) + glDeleteBuffers(2, vbo); + + delete [] vertices; + delete [] indices; + } + + bool SpriteBatch::loadVolatile() + { // Find out which OpenGL VBO usage hint to use. gl_usage = GL_STREAM_DRAW; gl_usage = (usage == USAGE_DYNAMIC) ? GL_DYNAMIC_DRAW : gl_usage; @@ -68,17 +84,21 @@ namespace opengl glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*size*6, indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + return true; } - SpriteBatch::~SpriteBatch() + void SpriteBatch::unloadVolatile() { - image->release(); + vertex * v = (vertex *)lock(); + // Copy to system memory. + memcpy(vertices, v, sizeof(vertex)*size*4); + + unlock(); + + // Delete the buffers. if(vbo[0] != 0 && vbo[1] != 0) glDeleteBuffers(2, vbo); - - delete [] vertices; - delete [] indices; } void SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy) @@ -135,6 +155,10 @@ namespace opengl void * SpriteBatch::lock() { + // If already locked, prevent from locking again. + if(lockp != 0) + return lockp; + glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); lockp = (vertex *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); return lockp; diff --git a/src/modules/graphics/opengl/SpriteBatch.h b/src/modules/graphics/opengl/SpriteBatch.h index bc4f59fd7..640442916 100644 --- a/src/modules/graphics/opengl/SpriteBatch.h +++ b/src/modules/graphics/opengl/SpriteBatch.h @@ -30,6 +30,7 @@ #include #include #include +#include // OpenGL #include "GLee.h" @@ -45,7 +46,7 @@ namespace opengl class Image; class Quad; - class SpriteBatch : public Drawable + class SpriteBatch : public Drawable, public Volatile { private: @@ -84,6 +85,10 @@ namespace opengl SpriteBatch(Image * image, int size, int usage); virtual ~SpriteBatch(); + // Implements Volatile. + bool loadVolatile(); + void unloadVolatile(); + void add(float x, float y, float a, float sx, float sy, float ox, float oy); void addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy); void clear();