summaryrefslogtreecommitdiff
path: root/lib/glArrays.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-01-01 16:44:19 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-01-01 16:44:19 +0000
commit7c03d93c367b842c464dca30e121bc4c20547c36 (patch)
tree6df340dc3b0bd16f6ced2a051a68fc8a3c67a08c /lib/glArrays.h
parentFirst iteration with font/text support (diff)
downloadilt-7c03d93c367b842c464dca30e121bc4c20547c36.tar.bz2
ilt-7c03d93c367b842c464dca30e121bc4c20547c36.tar.xz
ilt-7c03d93c367b842c464dca30e121bc4c20547c36.zip
Generic solution for glGen/glDel arrays, then tidy-up the uses
Diffstat (limited to 'lib/glArrays.h')
-rw-r--r--lib/glArrays.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/glArrays.h b/lib/glArrays.h
new file mode 100644
index 0000000..ed05eeb
--- /dev/null
+++ b/lib/glArrays.h
@@ -0,0 +1,76 @@
+#pragma once
+
+#include <GL/glew.h>
+#include <algorithm> // IWYU pragma: keep
+#include <array>
+#include <cstddef>
+#include <special_members.hpp>
+
+template<size_t N> class glArraysBase {
+ static_assert(N > 0);
+
+public:
+ ~glArraysBase() = default;
+ NO_COPY(glArraysBase);
+ CUSTOM_MOVE(glArraysBase);
+
+ // NOLINTNEXTLINE(hicpp-explicit-conversions)
+ inline operator GLuint() const
+ {
+ static_assert(N == 1, "Implicit cast only if N == 1");
+ return ids.front();
+ }
+
+ inline auto
+ operator[](size_t n) const
+ {
+ return ids[n];
+ }
+
+ constexpr static auto size {N};
+
+protected:
+ glArraysBase() noexcept = default;
+ std::array<GLuint, N> ids {};
+};
+
+template<size_t N> inline glArraysBase<N>::glArraysBase(glArraysBase<N> && src) noexcept : ids {src.ids}
+{
+ std::fill(src.ids.begin(), src.ids.end(), 0);
+}
+
+template<size_t N>
+inline glArraysBase<N> &
+glArraysBase<N>::operator=(glArraysBase<N> && src) noexcept
+{
+ ids = src.ids;
+ std::fill(src.ids.begin(), src.ids.end(), 0);
+ return *this;
+}
+
+template<size_t N, auto Gen, auto Del> class glArrays : public glArraysBase<N> {
+public:
+ using glArraysBase<N>::glArraysBase;
+ using glArraysBase<N>::operator=;
+
+ DEFAULT_MOVE_COPY(glArrays);
+
+ inline glArrays() noexcept
+ {
+ (*Gen)(N, this->ids.data());
+ }
+
+ inline ~glArrays() noexcept
+ {
+ if (this->ids.front()) {
+ (*Del)(N, this->ids.data());
+ }
+ }
+};
+
+template<size_t N> using glVertexArrays = glArrays<N, &glGenVertexArrays, &glDeleteVertexArrays>;
+using glVertexArray = glVertexArrays<1>;
+template<size_t N> using glBuffers = glArrays<N, &glGenBuffers, &glDeleteBuffers>;
+using glBuffer = glBuffers<1>;
+template<size_t N> using glTextures = glArrays<N, &glGenTextures, &glDeleteTextures>;
+using glTexture = glTextures<1>;