X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=src%2Fgraphics%2Fbuffer.hpp;fp=src%2Fgraphics%2Fbuffer.hpp;h=288cdc8c2d99b049b4f35e0d8c6a2f19ad353a1c;hb=0146c7b7f02ef5d74116546489aee85383bb4969;hp=0000000000000000000000000000000000000000;hpb=0fc4efc1800c07534c1e945618630910e2a8e87f;p=tacos.git diff --git a/src/graphics/buffer.hpp b/src/graphics/buffer.hpp new file mode 100644 index 0000000..288cdc8 --- /dev/null +++ b/src/graphics/buffer.hpp @@ -0,0 +1,60 @@ +#ifndef TACOS_GRAPHICS_BUFFER_HPP_ +#define TACOS_GRAPHICS_BUFFER_HPP_ + +#include "../app/error.hpp" + +#include +#include + + +namespace tacos { + +template +class MappedBuffer { + +public: + MappedBuffer(GLenum target, GLenum access) + : buf(reinterpret_cast(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 &&other) noexcept + : buf(other.buf) + , target(other.target) { + other.buf = nullptr; + } + MappedBuffer &operator =(MappedBuffer &&other) noexcept { + std::swap(buf, other.buf); + std::swap(target, other.target); + } + + MappedBuffer(const MappedBuffer &) = delete; + MappedBuffer &operator =(const MappedBuffer &) = 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