From b0ecc6c056297b77cab0bbdcb626fabd10fbac3f Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 4 Oct 2020 12:23:01 +0300 Subject: [PATCH] Add OpenGL array class --- libs/gfx/include/psemek/gfx/array.hpp | 34 +++++++++++++++++ libs/gfx/source/array.cpp | 54 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 libs/gfx/include/psemek/gfx/array.hpp create mode 100644 libs/gfx/source/array.cpp diff --git a/libs/gfx/include/psemek/gfx/array.hpp b/libs/gfx/include/psemek/gfx/array.hpp new file mode 100644 index 00000000..e9c45af7 --- /dev/null +++ b/libs/gfx/include/psemek/gfx/array.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include + +#include + +namespace psemek::gfx +{ + + struct array + { + array(); + array(array &&); + array & operator = (array &&); + ~array(); + + array(array const &) = delete; + array & operator = (array const &) = delete; + + static array null(); + + GLuint id() const { return id_; } + + void bind() const; + + void reset(); + + private: + GLuint id_; + + explicit array(std::nullptr_t); + }; + +} diff --git a/libs/gfx/source/array.cpp b/libs/gfx/source/array.cpp new file mode 100644 index 00000000..8cd0dba1 --- /dev/null +++ b/libs/gfx/source/array.cpp @@ -0,0 +1,54 @@ +#include + +#include + +namespace psemek::gfx +{ + + array::array() + { + gl::GenVertexArrays(1, &id_); + } + + array::array(array && other) + : id_{other.id_} + { + other.id_ = 0; + } + + array & array::operator = (array && other) + { + if (this == &other) return *this; + + reset(); + std::swap(id_, other.id_); + return *this; + } + + array::~array() + { + reset(); + } + + array array::null() + { + return array(nullptr); + } + + void array::bind() const + { + gl::BindVertexArray(id_); + } + + void array::reset() + { + if (id_ != 0) + gl::DeleteVertexArrays(1, &id_); + id_ = 0; + } + + array::array(std::nullptr_t) + : id_{0} + {} + +}