]> git.tdb.fi Git - libs/gl.git/blobdiff - source/bufferable.cpp
Alter the working logic of Bufferable to avoid some problems
[libs/gl.git] / source / bufferable.cpp
index 5c7cb9e0891a74a753987dd32735a128228c6def..4660e9c31b32f787bfc553326078da669063b0a5 100644 (file)
@@ -36,7 +36,7 @@ void Bufferable::use_buffer(Buffer *buf, Bufferable *prev)
                        prev_in_buffer->next_in_buffer = this;
        }
 
-       update_buffer_offsets();
+       update_buffer_offset();
 }
 
 void Bufferable::unlink_from_buffer()
@@ -46,13 +46,13 @@ void Bufferable::unlink_from_buffer()
        if(next_in_buffer)
        {
                next_in_buffer->prev_in_buffer = prev_in_buffer;
-               next_in_buffer->update_buffer_offsets();
+               next_in_buffer->update_buffer_offset();
        }
        prev_in_buffer = 0;
        next_in_buffer = 0;
 }
 
-void Bufferable::update_buffer_offsets()
+void Bufferable::update_buffer_offset()
 {
        unsigned offset = 0;
        if(prev_in_buffer)
@@ -62,10 +62,14 @@ void Bufferable::update_buffer_offsets()
        {
                buffer_offset = offset;
                dirty = true;
+               offset_changed();
        }
 
        if(next_in_buffer)
-               next_in_buffer->update_buffer_offsets();
+               next_in_buffer->update_buffer_offset();
+
+       /* Do not resize the buffer here, as the offsets may change multiple times
+       before the buffer is actually used */
 }
 
 void Bufferable::update_buffer_data() const
@@ -77,12 +81,16 @@ void Bufferable::update_buffer_data() const
        for(const Bufferable *b=first; b; b=b->next_in_buffer)
                total_size += b->get_data_size();
 
-       buffer->data(total_size, 0);
-       for(const Bufferable *b=first; b; b=b->next_in_buffer)
+       if(total_size!=buffer->get_size())
        {
-               buffer->sub_data(b->buffer_offset, b->get_data_size(), b->get_data());
-               b->dirty = false;
+               buffer->data(total_size, 0);
+               // Resizing the buffer invalidates its contents.
+               for(const Bufferable *b=first; b; b=b->next_in_buffer)
+                       b->dirty = true;
        }
+
+       upload_data();
+       dirty = false;
 }
 
 } // namespace GL