Browse Source

Add VertexBuffer::Mapper for RAII vbo mapping

vrld 13 years ago
parent
commit
4947498488

+ 2 - 3
src/modules/graphics/opengl/SpriteBatch.cpp

@@ -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();
 		}
 		}
 	}
 	}
 
 

+ 27 - 0
src/modules/graphics/opengl/VertexBuffer.h

@@ -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.