]> git.tdb.fi Git - libs/gl.git/blobdiff - source/buffer.cpp
Rework exceptions
[libs/gl.git] / source / buffer.cpp
index 17ae494f72dc37a0892fdf282af33665d8723c49..fbb1e159ee564df8b2d7816e459adda6312cfc20 100644 (file)
@@ -1,14 +1,10 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <stdexcept>
 #include "arb_vertex_buffer_object.h"
 #include "extension.h"
 #include "buffer.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GL {
 
@@ -28,18 +24,6 @@ Buffer::~Buffer()
        glDeleteBuffersARB(1, &id);
 }
 
-void Buffer::bind(BufferType t) const
-{
-       glBindBufferARB(t, id);
-       binding(t) = this;
-}
-
-void Buffer::maybe_bind() const
-{
-       if(binding(type)!=this)
-               bind();
-}
-
 void Buffer::set_usage(BufferUsage u)
 {
        usage = u;
@@ -47,17 +31,31 @@ void Buffer::set_usage(BufferUsage u)
 
 void Buffer::data(unsigned size, const void *d)
 {
-       maybe_bind();
+       const Buffer *old = current(type);
+       bind();
        glBufferDataARB(type, size, d, usage);
+       restore(old, type);
 }
 
 void Buffer::sub_data(unsigned offset, unsigned size, const void *d)
 {
-       maybe_bind();
+       const Buffer *old = current(type);
+       bind();
        glBufferSubDataARB(type, offset, size, d);
+       restore(old, type);
 }
 
-void Buffer::unbind(BufferType type)
+void Buffer::bind_to(BufferType t) const
+{
+       const Buffer *&ptr = binding(t);
+       if(ptr!=this)
+       {
+               glBindBufferARB(t, id);
+               ptr = this;
+       }
+}
+
+void Buffer::unbind_from(BufferType type)
 {
        const Buffer *&ptr = binding(type);
        if(ptr)
@@ -75,7 +73,18 @@ const Buffer *&Buffer::binding(BufferType type)
        case ELEMENT_ARRAY_BUFFER: return bound[1];
        case PIXEL_PACK_BUFFER:    return bound[2];
        case PIXEL_UNPACK_BUFFER:  return bound[3];
-       default: throw InvalidParameterValue("Invalid buffer type");
+       default: throw invalid_argument("Buffer::binding");
+       }
+}
+
+void Buffer::restore(const Buffer *buf, BufferType type)
+{
+       if(buf!=current(type))
+       {
+               if(buf)
+                       buf->bind_to(type);
+               else
+                       unbind_from(type);
        }
 }