]> git.localhorst.tv Git - tacos.git/blob - src/graphics/buffer.hpp
isolate some GL stuff
[tacos.git] / src / graphics / buffer.hpp
1 #ifndef TACOS_GRAPHICS_BUFFER_HPP_
2 #define TACOS_GRAPHICS_BUFFER_HPP_
3
4 #include "../app/error.hpp"
5
6 #include <algorithm>
7 #include <GL/glew.h>
8
9
10 namespace tacos {
11
12 template<class T>
13 class MappedBuffer {
14
15 public:
16         MappedBuffer(GLenum target, GLenum access)
17         : buf(reinterpret_cast<T *>(glMapBuffer(target, access)))
18         , target(target) {
19                 if (!buf) {
20                         throw GLError("failed to map buffer");
21                 }
22         }
23         MappedBuffer()
24         : buf(nullptr)
25         , target(0) {
26         }
27         ~MappedBuffer() noexcept {
28                 if (buf) {
29                         glUnmapBuffer(target);
30                 }
31         }
32
33         MappedBuffer(MappedBuffer<T> &&other) noexcept
34         : buf(other.buf)
35         , target(other.target) {
36                 other.buf = nullptr;
37         }
38         MappedBuffer<T> &operator =(MappedBuffer<T> &&other) noexcept {
39                 std::swap(buf, other.buf);
40                 std::swap(target, other.target);
41         }
42
43         MappedBuffer(const MappedBuffer<T> &) = delete;
44         MappedBuffer<T> &operator =(const MappedBuffer<T> &) = delete;
45
46         explicit operator bool() const noexcept { return buf; }
47
48 public:
49         T &operator [](std::size_t i) noexcept { return buf[i]; }
50         const T &operator [](std::size_t i) const noexcept { return buf[i]; }
51
52 private:
53         T *buf;
54         GLenum target;
55
56 };
57
58 }
59
60 #endif