]> git.localhorst.tv Git - tacos.git/blobdiff - src/graphics/buffer.hpp
isolate some GL stuff
[tacos.git] / src / graphics / buffer.hpp
diff --git a/src/graphics/buffer.hpp b/src/graphics/buffer.hpp
new file mode 100644 (file)
index 0000000..288cdc8
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef TACOS_GRAPHICS_BUFFER_HPP_
+#define TACOS_GRAPHICS_BUFFER_HPP_
+
+#include "../app/error.hpp"
+
+#include <algorithm>
+#include <GL/glew.h>
+
+
+namespace tacos {
+
+template<class T>
+class MappedBuffer {
+
+public:
+       MappedBuffer(GLenum target, GLenum access)
+       : buf(reinterpret_cast<T *>(glMapBuffer(target, access)))
+       , target(target) {
+               if (!buf) {
+                       throw GLError("failed to map buffer");
+               }
+       }
+       MappedBuffer()
+       : buf(nullptr)
+       , target(0) {
+       }
+       ~MappedBuffer() noexcept {
+               if (buf) {
+                       glUnmapBuffer(target);
+               }
+       }
+
+       MappedBuffer(MappedBuffer<T> &&other) noexcept
+       : buf(other.buf)
+       , target(other.target) {
+               other.buf = nullptr;
+       }
+       MappedBuffer<T> &operator =(MappedBuffer<T> &&other) noexcept {
+               std::swap(buf, other.buf);
+               std::swap(target, other.target);
+       }
+
+       MappedBuffer(const MappedBuffer<T> &) = delete;
+       MappedBuffer<T> &operator =(const MappedBuffer<T> &) = delete;
+
+       explicit operator bool() const noexcept { return buf; }
+
+public:
+       T &operator [](std::size_t i) noexcept { return buf[i]; }
+       const T &operator [](std::size_t i) const noexcept { return buf[i]; }
+
+private:
+       T *buf;
+       GLenum target;
+
+};
+
+}
+
+#endif