diff --git a/src/modules/graphics/opengl/SpriteBatch.cpp b/src/modules/graphics/opengl/SpriteBatch.cpp index c78b76723..d7ce83a7f 100644 --- a/src/modules/graphics/opengl/SpriteBatch.cpp +++ b/src/modules/graphics/opengl/SpriteBatch.cpp @@ -69,8 +69,9 @@ namespace opengl // Fill element buffer. { VertexBuffer::Bind bind(*element_buf); + VertexBuffer::Mapper mapper(*element_buf, GL_WRITE_ONLY); - GLushort *indices = static_cast(element_buf->map(GL_WRITE_ONLY)); + GLushort *indices = static_cast(mapper.get()); if (indices) { @@ -85,8 +86,6 @@ namespace opengl indices[i*6+5] = 3+(i*4); } } - - element_buf->unmap(); } } diff --git a/src/modules/graphics/opengl/VertexBuffer.h b/src/modules/graphics/opengl/VertexBuffer.h index f439b82d7..030ad971f 100644 --- a/src/modules/graphics/opengl/VertexBuffer.h +++ b/src/modules/graphics/opengl/VertexBuffer.h @@ -173,6 +173,33 @@ namespace opengl VertexBuffer &buf; }; + class Mapper + { + public: + /** + * Memory-maps a VertexBuffer. + */ + Mapper(VertexBuffer& buffer, GLenum access) + : buf(buffer) + { elems = buf.map(access); } + + /** + * unmaps the buffer + */ + ~Mapper() + { buf.unmap(); } + + /** + * Get pointer to memory mapped region + */ + void* get() + { return elems; } + + private: + VertexBuffer &buf; + void* elems; + }; + private: // The size of the buffer, in bytes.