From 494749848870438928a4a98792235818aac03335 Mon Sep 17 00:00:00 2001 From: vrld Date: Wed, 7 Mar 2012 22:59:23 +0100 Subject: [PATCH] Add VertexBuffer::Mapper for RAII vbo mapping --- src/modules/graphics/opengl/SpriteBatch.cpp | 5 ++-- src/modules/graphics/opengl/VertexBuffer.h | 27 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) 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.