]> git.tdb.fi Git - libs/gl.git/blobdiff - source/batch.cpp
Make use of Bufferable in Batch
[libs/gl.git] / source / batch.cpp
index 1d9b1d4cd4ab7081a3bf95522056f61b91375217..785fca826ae30e5c9c524b0a2f08a242a2aad15f 100644 (file)
@@ -56,12 +56,7 @@ Batch::Batch(PrimitiveType t):
        data_type(UNSIGNED_BYTE),
        min_index(0),
        max_index(0),
-       restart(false),
-       ibuf(0),
-       ibuf_offset(0),
-       next_in_ibuf(0),
-       prev_in_ibuf(0),
-       dirty(false)
+       restart(false)
 {
        /* XXX Should probably provide a fallback to glDrawElements since this class
        is pretty much required to render anything. */
@@ -70,7 +65,6 @@ Batch::Batch(PrimitiveType t):
 
 Batch::~Batch()
 {
-       unlink_from_ibuf();
 }
 
 void Batch::set_data_type(DataType t)
@@ -96,57 +90,10 @@ void Batch::set_data_type(DataType t)
                shrink<unsigned short, unsigned char>(data);
 
        data_type = t;
-       update_ibuf_offsets();
+       update_offset();
        dirty = true;
 }
 
-void Batch::use_index_buffer(Buffer *buf, Batch *prev)
-{
-       if(buf && prev && prev->ibuf!=buf)
-               throw invalid_argument("Batch::use_index_buffer");
-
-       if(!buf)
-       {
-               prev = 0;
-               unlink_from_ibuf();
-       }
-
-       ibuf = buf;
-       prev_in_ibuf = prev;
-       next_in_ibuf = 0;
-       if(prev)
-       {
-               prev->next_in_ibuf = this;
-               ibuf_offset = prev->ibuf_offset+prev->data.size();
-       }
-       else
-               ibuf_offset = 0;
-
-       dirty = true;
-}
-
-void Batch::unlink_from_ibuf()
-{
-       if(next_in_ibuf)
-               next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
-       if(prev_in_ibuf)
-       {
-               prev_in_ibuf->next_in_ibuf = next_in_ibuf;
-               prev_in_ibuf->update_ibuf_offsets();
-       }
-       else if(next_in_ibuf)
-       {
-               next_in_ibuf->ibuf_offset = 0;
-               next_in_ibuf->update_ibuf_offsets();
-       }
-}
-
-void Batch::update_ibuf_offsets()
-{
-       for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
-               b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
-}
-
 Batch &Batch::append(unsigned i)
 {
        if(data.empty())
@@ -169,7 +116,7 @@ Batch &Batch::append(unsigned i)
        else
                data.push_back(i);
        
-       update_ibuf_offsets();
+       update_offset();
        dirty = true;
 
        return *this;
@@ -213,6 +160,7 @@ void Batch::append(const vector<unsigned> &ind)
                for(unsigned i=0; i<ind.size(); ++i)
                        data[base+i] = ind[i];
        }
+       dirty = true;
 }
 
 void Batch::append(const Batch &other)
@@ -259,6 +207,11 @@ void Batch::append(const Batch &other)
                append(other.get_index(i));
 }
 
+void Batch::upload_data() const
+{
+       get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
+}
+
 unsigned Batch::get_index_size() const
 {
        if(data_type==UNSIGNED_SHORT)
@@ -304,30 +257,15 @@ void Batch::draw() const
                restart_index = 0;
        }
 
-       if(ibuf)
+       if(get_buffer())
        {
                if(dirty)
-               {
-                       const Batch *b = this;
-                       for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
-
-                       unsigned chain_size = 0;
-                       for(const Batch *a=b; a; a=a->next_in_ibuf)
-                               chain_size += a->data.size();
-
-                       ibuf->data(chain_size, 0);
-
-                       for(; b; b=b->next_in_ibuf)
-                       {
-                               ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
-                               b->dirty = false;
-                       }
-               }
+                       update_buffer();
 
-               BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
+               BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*get_buffer());
                Bind bind_ibuf(alias, true);
 
-               glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(ibuf_offset));
+               glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
        }
        else
                glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);