]> git.tdb.fi Git - libs/gl.git/blobdiff - source/buffer.h
Keep track of buffer size
[libs/gl.git] / source / buffer.h
index e91524865e7fa449b318d16b64af586bba5ea5ba..d8a8419c0211fbc9b70429a95bcb7cc7c5065145 100644 (file)
@@ -1,14 +1,7 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_GL_BUFFER_H_
 #define MSP_GL_BUFFER_H_
 
-#include "types.h"
+#include "gl.h"
 
 namespace Msp {
 namespace GL {
@@ -45,6 +38,7 @@ private:
        BufferType type;
        BufferUsage usage;
        unsigned id;
+       unsigned size;
 
        static const Buffer *bound[4];
 
@@ -52,19 +46,10 @@ public:
        Buffer(BufferType);
        ~Buffer();
 
-       /** Binds the buffer in its default slot. */
-       void bind() const { bind(type); }
-
-       /** Binds the buffer in an alternate slot. */
-       void bind(BufferType) const;
-
 private:
-       void maybe_bind() const;
+       const Buffer *maybe_bind() const;
 
 public:
-       /** Unbinds the buffer from its default slot. */
-       void unbind() const { unbind(type); }
-
        /** Sets the usage hint of the buffer.  It will take effect the next time
        the buffer's contents are defined. */
        void set_usage(BufferUsage);
@@ -77,9 +62,39 @@ public:
        not be changed with this call. */
        void sub_data(unsigned, unsigned, const void *);
 
-       static void unbind(BufferType);
+       unsigned get_size() const { return size; }
+
+       /** Binds the buffer in its default slot. */
+       void bind() const { bind_to(type); }
+
+       /** Binds the buffer in an alternate slot. */
+       void bind_to(BufferType) const;
+
+       /** Unbinds the buffer from its default slot. */
+       void unbind() const { unbind_from(type); }
+
+       static const Buffer *current(BufferType t) { return binding(t); }
+       static void unbind_from(BufferType);
 private:
        static const Buffer *&binding(BufferType);
+       static void restore(const Buffer *, BufferType);
+};
+
+/**
+An adaptor for Buffer to make it compatible with Bind.
+*/
+template<BufferType T>
+class BufferAlias
+{
+private:
+       const Buffer &buffer;
+
+public:
+       BufferAlias(const Buffer &b): buffer(b) { }
+
+       void bind() const { buffer.bind_to(T); }
+       static const Buffer *current() { return Buffer::current(T); }
+       static void unbind() { Buffer::unbind_from(T); }
 };
 
 } // namespace GL