Add VertexBuffer::Mapper for RAII vbo mapping

This commit is contained in:
vrld
2012-03-07 22:59:23 +01:00
parent 17dc8760b9
commit 4947498488
2 changed files with 29 additions and 3 deletions
+2 -3
View File
@@ -69,8 +69,9 @@ namespace opengl
// Fill element buffer. // Fill element buffer.
{ {
VertexBuffer::Bind bind(*element_buf); VertexBuffer::Bind bind(*element_buf);
VertexBuffer::Mapper mapper(*element_buf, GL_WRITE_ONLY);
GLushort *indices = static_cast<GLushort*>(element_buf->map(GL_WRITE_ONLY)); GLushort *indices = static_cast<GLushort*>(mapper.get());
if (indices) if (indices)
{ {
@@ -85,8 +86,6 @@ namespace opengl
indices[i*6+5] = 3+(i*4); indices[i*6+5] = 3+(i*4);
} }
} }
element_buf->unmap();
} }
} }
@@ -173,6 +173,33 @@ namespace opengl
VertexBuffer &buf; 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: private:
// The size of the buffer, in bytes. // The size of the buffer, in bytes.